organized
This commit is contained in:
parent
21277a1aeb
commit
f96fb843de
8 changed files with 103 additions and 74 deletions
17
.env.example
17
.env.example
|
|
@ -1,8 +1,11 @@
|
||||||
DB_USER=postgres
|
PORT=3000
|
||||||
DB_PASSWORD=
|
SERVICE_NAME=<dash-seperated-lowercased-unique-accross-projects-name>
|
||||||
DB_NAME=postgres
|
DATABASE_URL="postgresql://..."
|
||||||
DB_HOST=localhost
|
MINIO_ACCESS_KEY=
|
||||||
DB_PORT=5432
|
MINIO_SECRET_KEY=
|
||||||
|
MINIO_ENDPOINT_URL=
|
||||||
|
MINIO_BUCKET_NAME=
|
||||||
|
BETTER_AUTH_SECRET=
|
||||||
|
|
||||||
|
# DO NOT CHANGE
|
||||||
DATABASE_URL='postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}'
|
BETTER_AUTH_URL=http://127.0.0.1:${PORT}
|
||||||
|
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -29,6 +29,7 @@ yarn-error.log*
|
||||||
.env.local
|
.env.local
|
||||||
.env.development.local
|
.env.development.local
|
||||||
.env.test.local
|
.env.test.local
|
||||||
|
.env.services
|
||||||
.env.production.local
|
.env.production.local
|
||||||
|
|
||||||
# vercel
|
# vercel
|
||||||
|
|
@ -43,4 +44,3 @@ package-lock.json
|
||||||
**/*.bun
|
**/*.bun
|
||||||
|
|
||||||
./server
|
./server
|
||||||
./.storage_data
|
|
||||||
|
|
|
||||||
48
docker-compose.services.yaml
Normal file
48
docker-compose.services.yaml
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
services:
|
||||||
|
tracing:
|
||||||
|
image: jaegertracing/all-in-one:latest
|
||||||
|
environment:
|
||||||
|
COLLECTOR_ZIPKIN_HOST_PORT: 9411
|
||||||
|
COLLECTOR_OTLP_ENABLED: true
|
||||||
|
ports:
|
||||||
|
- "16686:16686"
|
||||||
|
|
||||||
|
db:
|
||||||
|
image: postgres:latest
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: ${DB_USER}
|
||||||
|
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||||
|
POSTGRES_DB: ${DB_NAME}
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: minio/minio:latest
|
||||||
|
ports:
|
||||||
|
- "9000:9000"
|
||||||
|
- "9001:9001"
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
|
||||||
|
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
|
||||||
|
MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
|
||||||
|
MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
|
||||||
|
command: server /data --console-address ":9001"
|
||||||
|
volumes:
|
||||||
|
- minio_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "mc", "ready", "local"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 20s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
networks:
|
||||||
|
api-network:
|
||||||
|
driver: bridge
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
minio_data:
|
||||||
|
|
@ -4,67 +4,10 @@ services:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "${PORT}:${PORT}"
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
OTEL_EXPORTER_OTLP_ENDPOINT: http://tracing:4318
|
OTEL_EXPORTER_OTLP_ENDPOINT: http://tracing:4318
|
||||||
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
|
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
|
||||||
DATABASE_URL: ${DATABASE_URL}
|
DATABASE_URL: ${DATABASE_URL}
|
||||||
networks:
|
|
||||||
- api-network
|
|
||||||
depends_on:
|
|
||||||
- tracing
|
|
||||||
|
|
||||||
tracing:
|
|
||||||
image: jaegertracing/all-in-one:latest
|
|
||||||
environment:
|
|
||||||
COLLECTOR_ZIPKIN_HOST_PORT: 9411
|
|
||||||
COLLECTOR_OTLP_ENABLED: true
|
|
||||||
ports:
|
|
||||||
- "16686:16686"
|
|
||||||
networks:
|
|
||||||
- api-network
|
|
||||||
|
|
||||||
db:
|
|
||||||
image: postgres:latest
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: postgres
|
|
||||||
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
|
||||||
POSTGRES_DB: postgres
|
|
||||||
ports:
|
|
||||||
- "5432:5432"
|
|
||||||
volumes:
|
|
||||||
- postgres_data:/var/lib/postgresql/data
|
|
||||||
networks:
|
|
||||||
- api-network
|
|
||||||
|
|
||||||
minio:
|
|
||||||
image: minio/minio:latest
|
|
||||||
ports:
|
|
||||||
- "9000:9000"
|
|
||||||
- "9001:9001"
|
|
||||||
environment:
|
|
||||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
|
|
||||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
|
|
||||||
MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
|
|
||||||
MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
|
|
||||||
command: server /data --console-address ":9001"
|
|
||||||
volumes:
|
|
||||||
- minio_data:/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "mc", "ready", "local"]
|
|
||||||
interval: 30s
|
|
||||||
timeout: 20s
|
|
||||||
retries: 3
|
|
||||||
networks:
|
|
||||||
- api-network
|
|
||||||
|
|
||||||
networks:
|
|
||||||
api-network:
|
|
||||||
driver: bridge
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
postgres_data:
|
|
||||||
minio_data:
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import { drizzle } from "drizzle-orm/node-postgres";
|
import { drizzle } from "drizzle-orm/node-postgres";
|
||||||
|
import { getDbConfig } from "../lib/utils/env";
|
||||||
|
|
||||||
export const db = drizzle(process.env.DATABASE_URL!);
|
const dbConfig = getDbConfig()
|
||||||
|
|
||||||
|
export const db = drizzle(dbConfig.DATABASE_URL);
|
||||||
|
|
|
||||||
15
src/index.ts
15
src/index.ts
|
|
@ -6,11 +6,15 @@ import { cors } from '@elysiajs/cors'
|
||||||
import { note } from "./api/note/note.route";
|
import { note } from "./api/note/note.route";
|
||||||
import { betterAuthView } from "./lib/auth/auth-view";
|
import { betterAuthView } from "./lib/auth/auth-view";
|
||||||
import { userMiddleware, userInfo } from "./middlewares/auth-middleware";
|
import { userMiddleware, userInfo } from "./middlewares/auth-middleware";
|
||||||
import { validateEnv } from "./lib/utils/env";
|
import { getBaseConfig, validateEnv } from "./lib/utils/env";
|
||||||
|
|
||||||
|
const baseConfig = getBaseConfig()
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(cors())
|
.use(cors())
|
||||||
.use(opentelemetry())
|
.use(opentelemetry({
|
||||||
|
"serviceName": baseConfig.SERVICE_NAME
|
||||||
|
}))
|
||||||
.use(swagger({
|
.use(swagger({
|
||||||
path: "/docs",
|
path: "/docs",
|
||||||
}))
|
}))
|
||||||
|
|
@ -22,8 +26,9 @@ const app = new Elysia()
|
||||||
.derive(({ request }) => userMiddleware(request))
|
.derive(({ request }) => userMiddleware(request))
|
||||||
.all("/api/auth/*", betterAuthView)
|
.all("/api/auth/*", betterAuthView)
|
||||||
.use(note)
|
.use(note)
|
||||||
.get("/user", ({ user, session }) => userInfo(user, session));
|
.get("/user", ({ user, session }) => userInfo(user, session))
|
||||||
|
.get("/", () => "Server is Running")
|
||||||
|
|
||||||
validateEnv();
|
validateEnv();
|
||||||
app.listen(3000);
|
app.listen(baseConfig.PORT);
|
||||||
console.log("Server is running on: http://localhost:3000")
|
console.log(`Server is running on: http://127.0.0.1:${baseConfig.PORT}`)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,10 @@ import { z } from 'zod';
|
||||||
|
|
||||||
// Define the environment schema
|
// Define the environment schema
|
||||||
const envSchema = z.object({
|
const envSchema = z.object({
|
||||||
|
// Base
|
||||||
|
PORT: z.string().max(5),
|
||||||
|
SERVICE_NAME: z.string(),
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
DATABASE_URL: z.string().url(),
|
DATABASE_URL: z.string().url(),
|
||||||
|
|
||||||
|
|
@ -9,7 +13,11 @@ const envSchema = z.object({
|
||||||
MINIO_ACCESS_KEY: z.string(),
|
MINIO_ACCESS_KEY: z.string(),
|
||||||
MINIO_SECRET_KEY: z.string().min(8),
|
MINIO_SECRET_KEY: z.string().min(8),
|
||||||
MINIO_ENDPOINT_URL: z.string().url(),
|
MINIO_ENDPOINT_URL: z.string().url(),
|
||||||
MINIO_BUCKET_NAME: z.string().url(),
|
MINIO_BUCKET_NAME: z.string(),
|
||||||
|
|
||||||
|
// Auth
|
||||||
|
BETTER_AUTH_SECRET: z.string().min(32),
|
||||||
|
BETTER_AUTH_URL: z.string().url(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a type from the schema
|
// Create a type from the schema
|
||||||
|
|
@ -42,7 +50,9 @@ export const validateEnv = (): EnvConfig => {
|
||||||
if (path.startsWith('MINIO_')) {
|
if (path.startsWith('MINIO_')) {
|
||||||
warningMessage += '\n ⚠️ File storage functionality may not work properly';
|
warningMessage += '\n ⚠️ File storage functionality may not work properly';
|
||||||
}
|
}
|
||||||
|
if (path.startsWith('BETTER_AUTH_')) {
|
||||||
|
warningMessage += '\n ⚠️ Authentication functionality may not work properly';
|
||||||
|
}
|
||||||
warnings.push(warningMessage);
|
warnings.push(warningMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -64,6 +74,15 @@ export const getConfig = (): EnvConfig => {
|
||||||
return validateEnv();
|
return validateEnv();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export const getBaseConfig = (): Pick<EnvConfig, 'PORT' | 'SERVICE_NAME'> => {
|
||||||
|
const config = getConfig();
|
||||||
|
return {
|
||||||
|
PORT: config.PORT,
|
||||||
|
SERVICE_NAME: config.SERVICE_NAME,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// Optional: Export individual config getters with type safety
|
// Optional: Export individual config getters with type safety
|
||||||
export const getDbConfig = (): Pick<EnvConfig, 'DATABASE_URL'> => {
|
export const getDbConfig = (): Pick<EnvConfig, 'DATABASE_URL'> => {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
@ -82,6 +101,14 @@ export const getMinioConfig = (): Pick<EnvConfig, 'MINIO_ACCESS_KEY' | 'MINIO_SE
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getAuthConfig = (): Pick<EnvConfig, 'BETTER_AUTH_SECRET' | 'BETTER_AUTH_URL'> => {
|
||||||
|
const config = getConfig();
|
||||||
|
return {
|
||||||
|
BETTER_AUTH_SECRET: config.BETTER_AUTH_SECRET,
|
||||||
|
BETTER_AUTH_URL: config.BETTER_AUTH_URL,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// Usage example:
|
// Usage example:
|
||||||
try {
|
try {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue