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

fixed docker-compose config #19

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
3 changes: 0 additions & 3 deletions Dockerfile.development
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 0 additions & 3 deletions Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 2 additions & 3 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
12 changes: 6 additions & 6 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { config } from "$config";
import { env } from "bun";
import { config, env } from "$config";
import { connect, disconnect, set } from "mongoose";

export const connectDB = async () => {
try {
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);
Expand All @@ -17,14 +17,14 @@ 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);
}
};

export async function handleDisconnectDBWhenExit(signal: string) {
console.log(`Received ${signal}. Closing MongoDB connection...`);
await disconnectDB();
process.exit(0);
}
}
15 changes: 0 additions & 15 deletions src/db/migrate.ts

This file was deleted.