From bb2a0787b11791d65a5257a58a15a19d1af2ff45 Mon Sep 17 00:00:00 2001 From: April Hall Date: Sun, 23 Feb 2025 21:42:17 -0500 Subject: [PATCH] feat: Contanerization via Docker & Docker Compose --- .dockerignore | 42 ++++++++++++++++++++++ .gitignore | 4 +-- Dockerfile | 42 ++++++++++++++++++++++ TODO.md | 2 +- compose.yaml | 49 ++++++++++++++++++++++++++ package.json | 2 +- prodServer.ts | 5 +-- src/lib/server/db/index.ts | 4 ++- src/lib/server/storage/minio-client.ts | 3 +- 9 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4a1be89 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,42 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +**/src/lib/server/db/users.db* +**/minio-storage +**/vite.config.js.timestamp-* +**/vite.config.ts.timestamp-* +.output +.vercel +.netlify +.wrangler +/.svelte-kit +/build +/drizzle +.DS_Store +Thumbs.db +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore index d2ae1c0..a0e84f5 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,7 @@ vite.config.js.timestamp-* vite.config.ts.timestamp-* # Sqlite -src/lib/server/db/users.db* +**/src/lib/server/db/users.db* # Storage -minio-storage +**/minio-storage diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..28718c8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +ARG NODE_VERSION=23.8.0 +FROM node:${NODE_VERSION} AS base +WORKDIR /app + +############### +# Dep Stage # +############### +FROM base AS deps +COPY package.json . + +RUN npm install --omit-dev + +############### +# Build Stage # +############### +FROM deps AS build +COPY . . + +# Run DB migrations for SQLite +RUN npm run migrate + +RUN npm install +RUN npm run build + +############### +# Application # +############### +FROM base AS final + +COPY package.json . + +# Stuff needed by the app +COPY --from=build /app/build build/ +COPY --from=build /app/prodServer.ts . +COPY --from=build /app/package.json . +COPY --from=build /app/src/lib/server/db src/lib/server/db +COPY --from=deps /app/node_modules node_modules/ + +ENV NODE_ENV=production + +EXPOSE 3000 +CMD ["npm", "run", "production"] \ No newline at end of file diff --git a/TODO.md b/TODO.md index b928d01..6950a26 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,7 @@ A more complex version of this list is available [here](https://trello.com/b/kJw - [x] Account / Profile management - [ ] Channel context menus -- [ ] Containerization with docker and docker-compose +- [X] Containerization with docker and docker-compose - [ ] Documentation - [ ] Emoji picker - [ ] Images/Attatchments support diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..76187d4 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,49 @@ +services: + chat-server: + container_name: svchat-server + build: + context: . + environment: + NODE_ENV: docker_production + ORIGIN: http://localhost:3000 + ports: + - 3000:3000 + depends_on: + - cassandra + volumes: + - svchat-users:/app/src/lib/server/db + + cassandra: + container_name: svchat-db + image: cassandra + restart: always + ports: + - 9042:9042 + environment: + # TODO Change default passwords + - CASSANDRA_USER=admin + - CASSANDRA_PASSWORD=admin + volumes: + - svchat-cassandra:/var/lib/cassandra + + minio: + container_name: svchat-s3 + image: docker.io/bitnami/minio + restart: always + ports: + - 9000:9000 + - 9001:9001 + environment: + # TODO Change default passwords + - MINIO_ROOT_USER=minioadmin + - MINIO_ROOT_PASSWORD=minioadmin + volumes: + - svchat-s3:/data + +volumes: + svchat-cassandra: + driver: local + svchat-s3: + driver: local + svchat-users: + driver: local diff --git a/package.json b/package.json index ceb9e32..31a0c7c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "vite dev", "format": "prettier --write .", "lint": "eslint .", - "migrate": "npx @better-auth/cli migrate --config './src/lib/server/db/auth'", + "migrate": "npx @better-auth/cli migrate --config './src/lib/server/db/auth' -y", "minio": "minio server minio-storage", "preview": "vite preview", "prepare": "svelte-kit sync || echo ''", diff --git a/prodServer.ts b/prodServer.ts index ab76610..8ba1ba1 100644 --- a/prodServer.ts +++ b/prodServer.ts @@ -9,6 +9,7 @@ import { v4 as uuidv4 } from 'uuid'; const app = express(); const server = createServer(app); const io = new Server(server); +const port = 3000; io.on('connection', async (socket) => { // Runs on client connection @@ -44,6 +45,6 @@ app.use((req, res, next) => { } }); -server.listen(3005, () => { - console.log('Listening on http://0.0.0.0:3005'); +server.listen(port, () => { + console.log(`Listening on http://localhost:${port}`); }); diff --git a/src/lib/server/db/index.ts b/src/lib/server/db/index.ts index 76de053..5b6eb5d 100644 --- a/src/lib/server/db/index.ts +++ b/src/lib/server/db/index.ts @@ -18,9 +18,11 @@ function sanitizeChannelName(channelName: string) { } class Db { + private clientUrl: string = process.env.NODE_ENV === 'docker_production' ? 'cassandra' : 'localhost'; private client: cassandra.Client = new cassandra.Client({ - contactPoints: ['localhost'], + contactPoints: [this.clientUrl], localDataCenter: 'datacenter1', + authProvider: new cassandra.auth.PlainTextAuthProvider('admin', 'admin'), }); // Initalize and connect diff --git a/src/lib/server/storage/minio-client.ts b/src/lib/server/storage/minio-client.ts index 45f3cae..6721151 100644 --- a/src/lib/server/storage/minio-client.ts +++ b/src/lib/server/storage/minio-client.ts @@ -71,7 +71,8 @@ let fsClient: MinioClient | undefined; if (process.env.BUILDING !== 'true') { fsClient = new MinioClient({ - endPoint: 'localhost', + // Endpoint is 'minio' in compose, 'localhost' everywhere else + endPoint: process.env.NODE_ENV === 'docker_production' ? 'minio' : 'localhost', port: 9000, accessKey: 'minioadmin', secretKey: 'minioadmin',