Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerize the server #12

Open
wants to merge 2 commits into
base: webmentions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
docs
node_modules
.prettierrc
.gitignore
shell.nix
render.yaml
plugin
.direnv
pruned
.env
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM node:22-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY . /app
WORKDIR /app


FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm prisma-generate
RUN pnpm -F obsidian-sync-server build:prod

FROM build AS pruned
RUN pnpm -F obsidian-sync-server --prod deploy pruned

# entrypoint container with dumb-init
FROM node:22-alpine
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
WORKDIR /app
RUN apk update && apk add --no-cache dumb-init
RUN pnpm install -g pm2
ENV NODE_ENV=production
ENV PORT=8000
ENV DATABASE_URL="file://./prisma.db"
COPY --from=pruned --chown=node:node /app/pruned /app
RUN pnpx prisma generate --schema=./db/schema.prisma
RUN pnpx pnpm prisma db push --schema=./db/schema.prisma --skip-generate
EXPOSE 8000

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD [ "pm2-runtime", "--", "./build/server.js" ]
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is a monorepo made with pnpm workspaces.
- pnpm
- node
- npm
- podman

### Technologies Used

Expand All @@ -15,6 +16,7 @@ This is a monorepo made with pnpm workspaces.
- Prisma
- sqlite - this is the default. You may use any DB supported by Prisma
- obsidian-sample-plugin (used for the plugin template)
- Docker/podman

## To get started for local development:

Expand Down Expand Up @@ -64,6 +66,20 @@ The server is needed to talk to the database (in this case, sqlite). The server

Media is not currently supported.

### Dockerfile

To build the server into a Docker image (use docker if you prefer):

```sh
podman build -f Dockerfile -t obsidian-server
```

and to run it, use the `.env` file created earlier. Note: you may want to mount the sqlite db file from a volume

```sh
podman run -p 8000:8000 -d --name obsidian-server --env-file=.env obsidian-server:latest
```

### The Blog Route

There is also a route at `/api/blog` that is not blocked by cors by default. Given a query string parameter of a vault name, you can fetch all nodes that have frontmatter `published: true` or a #published hashtag. (The #published hashtag is removed from the response.)
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "0.1.0",
"description": "",
"main": "./build/server.js",
"files": [
"build",
"db/schema.prisma"
],
"scripts": {
"build:prod": "tsc",
"dev": "pnpm watch & NODE_ENV=development pnpm nodemon ./build/server.js",
Expand All @@ -18,6 +22,7 @@
"@prisma/client": "^4.9.0",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"jsonwebtoken": "^9.0.0",
"morgan": "^1.10.0",
Expand All @@ -35,7 +40,6 @@
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"@vitest/coverage-c8": "^0.25.7",
"dotenv": "^16.0.1",
"eslint": "^8.20.0",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
Expand Down