From 58016ee4eadc7dd5ac34c3ea044e53634533ea0c Mon Sep 17 00:00:00 2001 From: creations Date: Sun, 5 Jan 2025 20:48:29 -0500 Subject: [PATCH] add docker support, move to env file, add example --- .example.env | 7 +++++++ .gitignore | 1 + Dockerfile | 15 +++++++++++++++ compose.yml | 30 ++++++++++++++++++++++++++++++ config/environment.ts | 24 +++++++++++++++++++----- package.json | 1 + src/database/redis.ts | 3 ++- 7 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 .example.env create mode 100644 Dockerfile create mode 100644 compose.yml diff --git a/.example.env b/.example.env new file mode 100644 index 0000000..792cbd2 --- /dev/null +++ b/.example.env @@ -0,0 +1,7 @@ +PORT=6600 +HOST=0.0.0.0 +#NODE_ENV=development + +#REDIS_HOST=127.0.0.1 +#REDIS_PORT=6379 +REDIS_PASSWORD=pasw0rd diff --git a/.gitignore b/.gitignore index ca963d0..afefd98 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules bun.lockb /config/secrets.ts +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f6b3b8b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +# docker/dev.Dockerfile +FROM oven/bun:latest + +WORKDIR /app/booru-api + +COPY package.json ./ +COPY bun.lockb ./ + +RUN bun install + +COPY . . + +ENV NEXT_TELEMETRY_DISABLED 1 + +CMD bun run start diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..b981391 --- /dev/null +++ b/compose.yml @@ -0,0 +1,30 @@ +services: + booru-api: + container_name: booru-api + build: + context: . + volumes: + - .:/app/booru-api + restart: unless-stopped + ports: + - "${PORT:-6600}:${PORT:-6600}" + env_file: + - .env + depends_on: + - dragonfly-redis + networks: + - booru-network + + dragonfly-redis: + container_name: dragonfly-redis + image: docker.dragonflydb.io/dragonflydb/dragonfly + restart: unless-stopped + environment: + REDIS_PASSWORD: ${redis_password:-pasw0rd} + command: ["--requirepass", "${redis_password:-pasw0rd}"] + networks: + - booru-network + +networks: + booru-network: + driver: bridge diff --git a/config/environment.ts b/config/environment.ts index f5de457..d469948 100644 --- a/config/environment.ts +++ b/config/environment.ts @@ -1,7 +1,21 @@ +import dotenv from "dotenv"; + +import { logger } from "@/helpers/logger"; + +try { + dotenv.config(); +} catch { + logger.error("No .env file found consider creating one"); +} + export const environment: Environment = { - port: 6600, - host: "127.0.0.1", - development: - process.argv.includes("--dev") || - process.argv.includes("--development"), + port: parseInt(process.env.PORT || "6600", 10), + host: process.env.HOST || "0.0.0.0", + development: process.env.NODE_ENV === "development", +}; + +export const redisConfig: RedisConfig = { + host: process.env.REDIS_HOST || "dragonfly-redis", + port: parseInt(process.env.REDIS_PORT || "6379", 10), + password: process.env.REDIS_PASSWORD || undefined, }; diff --git a/package.json b/package.json index 4896e0d..f26058c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "type": "module", "dependencies": { + "dotenv": "^16.4.7", "redis": "^4.7.0" } } diff --git a/src/database/redis.ts b/src/database/redis.ts index 1eec95d..d401695 100644 --- a/src/database/redis.ts +++ b/src/database/redis.ts @@ -1,4 +1,4 @@ -import { redisConfig } from "@config/secrets"; +import { redisConfig } from "@config/environment"; import { logger } from "@helpers/logger"; import { createClient, type RedisClientType } from "redis"; @@ -23,6 +23,7 @@ class RedisJson { RedisJson.instance.client.on("error", (err: Error) => { logger.error("Redis connection error:"); logger.error((err as Error) || "Unknown error"); + logger.error(redisConfig.host); process.exit(1); });