From c6d7eb8664ef491973b9ca27c2885e1f44e0a00d Mon Sep 17 00:00:00 2001 From: believelody Date: Tue, 13 Aug 2024 11:03:03 +0200 Subject: [PATCH] fixed docker-compose config --- Dockerfile.development | 3 --- Dockerfile.production | 3 --- docker-compose.yml | 19 +++++++++++-------- package.json | 10 ++++++---- src/config/index.ts | 5 ++--- src/db/index.ts | 12 ++++++------ src/db/migrate.ts | 15 --------------- 7 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 src/db/migrate.ts diff --git a/Dockerfile.development b/Dockerfile.development index 833e938..8d9f38c 100644 --- a/Dockerfile.development +++ b/Dockerfile.development @@ -11,7 +11,4 @@ COPY --link bun.lockb package.json ./ RUN bun install -ENV SERVER_PORT 3000 -EXPOSE $SERVER_PORT - CMD ["bun", "run", "start:dev"] diff --git a/Dockerfile.production b/Dockerfile.production index e26402f..99589db 100644 --- a/Dockerfile.production +++ b/Dockerfile.production @@ -6,9 +6,6 @@ LABEL fly_launch_runtime="Bun" RUN npm install -g bun -# Set production environment -ENV NODE_ENV="production" - WORKDIR /app # Install node modules diff --git a/docker-compose.yml b/docker-compose.yml index 4594405..3682691 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,9 +5,10 @@ services: context: . # target: production dockerfile: Dockerfile.production - + env_file: + - .env.production ports: - - ${SERVER_PORT}:${SERVER_PORT} + - 3001:3001 volumes: - ./:/app - /app/node_modules @@ -21,23 +22,25 @@ services: context: . # target: development dockerfile: Dockerfile.development + env_file: + - .env.development ports: - - ${SERVER_PORT}:${SERVER_PORT} + - 3000:3000 volumes: - ./:/app - /app/node_modules - environment: - NODE_ENV: development depends_on: - mongo networks: - node-network mongo: - image: mongo:6-jammy - container_name: ${DATABASE_HOST} + image: mongo:latest + container_name: mongo_dev + env_file: + - .env.development ports: - - ${DATABASE_PORT}:${DATABASE_PORT} + - 27017:27017 volumes: - mongo-data:/data/db networks: diff --git a/package.json b/package.json index df69e0e..656b927 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,12 @@ "build-js:prod": "bun build src/script/app.js --outfile=public/script/app.js --minify", "start:dev": "concurrently \"bun dev\" \"bun tailwind:dev\" \"bun build-js:dev\"", "start:prod": "bun tailwind:prod && bun build-js:prod && NODE_ENV=production bun run src/index.tsx", - "docker:dev:up": "docker compose --env-file ./.env.development up --build mongo dev", - "docker:dev:down": "docker compose --env-file ./.env.development down mongo dev", - "docker:prod:up": "docker compose --env-file ./.env.production up --build app", - "docker:prod:down": "docker compose --env-file ./.env.production down app", + "docker:mongo:up": "docker-compose up --build mongo", + "docker:mongo:down": "docker-compose down mongo", + "docker:dev:up": "bun docker:mongo:up -d && docker-compose up --build dev", + "docker:dev:down": "docker-compose down mongo dev", + "docker:prod:up": "docker-compose up --build app", + "docker:prod:down": "docker-compose down app", "fly:deploy": "flyctl deploy --dockerfile Dockerfile.production" }, "dependencies": { diff --git a/src/config/index.ts b/src/config/index.ts index de769c3..e58c775 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -16,15 +16,14 @@ export const env = createEnv({ GOOGLE_REDIRECT_URI_PATH: z.string(), HOST_URL: z.string().min(1), SERVER_PORT: z.coerce.number(), + DATABASE_URL: z.string(), }, runtimeEnv: process.env, }); -const devMongoURI = `mongodb://${env.DATABASE_HOST}:${env.DATABASE_PORT}/?authSource=admin`; -const prodMongoURI = `mongodb+srv://${env.MONGO_INITDB_ROOT_USERNAME}:${env.MONGO_INITDB_ROOT_PASSWORD}@${env.DATABASE_HOST}/?retryWrites=true&w=majority&appName=${env.DATABASE_CLUSTER}`; export const config = { db: { - url: env.NODE_ENV === "production" ? prodMongoURI : devMongoURI, + url: env.DATABASE_URL, }, google: { credentials: { diff --git a/src/db/index.ts b/src/db/index.ts index e24af91..766158a 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,5 +1,4 @@ -import { config } from "$config"; -import { env } from "bun"; +import { config, env } from "$config"; import { connect, disconnect, set } from "mongoose"; export const connectDB = async () => { @@ -7,7 +6,8 @@ export const connectDB = async () => { if (env.NODE_ENV === "development") { set("debug", true); } - await connect(config.db.url); + await connect(config.db.url, { + }); console.log("Connected to MongoDB"); } catch (err) { console.error("Error connecting to MongoDB", err); @@ -17,9 +17,9 @@ export const connectDB = async () => { export const disconnectDB = async () => { try { await disconnect(); - console.log('Disconnected from MongoDB'); + console.log("Disconnected from MongoDB"); } catch (err) { - console.error('Error disconnecting from MongoDB', err); + console.error("Error disconnecting from MongoDB", err); } }; @@ -27,4 +27,4 @@ export async function handleDisconnectDBWhenExit(signal: string) { console.log(`Received ${signal}. Closing MongoDB connection...`); await disconnectDB(); process.exit(0); -} \ No newline at end of file +} diff --git a/src/db/migrate.ts b/src/db/migrate.ts deleted file mode 100644 index 5b57252..0000000 --- a/src/db/migrate.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { migrate } from "drizzle-orm/libsql/migrator"; -import { db } from "$db"; - -(async () => { - try { - await migrate(db, { - migrationsFolder: "./migrations", - }); - console.log("Tables migrated!"); - process.exit(0); - } catch (error) { - console.error("Error performing migration: ", error); - process.exit(1); - } -})()