chore: update environment configuration and deployment scripts

- Added production environment variables to docker-compose.yml
- Updated Dockerfile for API and web to use Turbo build commands
- Modified turbo.json to include new environment variables
- Updated API index.ts to use new environment configuration
- Updated README.md with correct API port
- Added start:api script to package.json
- Improved deployment and configuration management
This commit is contained in:
Juan Sebastián Montoya 2025-03-06 23:08:10 -05:00
parent 9a2e69921e
commit 1e5a035d33
7 changed files with 28 additions and 17 deletions

View file

@ -8,10 +8,11 @@ COPY . .
RUN npm install RUN npm install
# Generate Prisma client # Generate Prisma client
RUN npm run prisma:generate RUN cd apps/api && npm run prisma:generate
# Build the project # Build the project
RUN npm run api:build RUN npx turbo run build --filter=api
EXPOSE 4000
# Start the server # Start the server
ENTRYPOINT [ "npm", "run", "api:start" ] ENTRYPOINT [ "npm", "run", "start:api" ]

View file

@ -42,7 +42,7 @@ npm run prisma:migrate
npm run dev npm run dev
``` ```
The API will be available at http://localhost:8080/graphql. The API will be available at http://localhost:4000/graphql.
## Available Scripts ## Available Scripts

View file

@ -11,19 +11,21 @@ import { z } from 'zod';
dotenv.config({ path: '../../.env' }); dotenv.config({ path: '../../.env' });
const envs = z const { allowedOrigins, port, host } = z
.object({ .object({
ALLOWED_ORIGINS: z.string().default('http://localhost:5173'), ALLOWED_ORIGINS: z.string(),
API_HOST: z.string(),
API_PORT: z.coerce.number(),
}) })
.transform((env) => { .transform((env) => {
return { return {
allowedOrigins: env.ALLOWED_ORIGINS.split(','), allowedOrigins: env.ALLOWED_ORIGINS.split(','),
port: env.API_PORT,
host: env.API_HOST,
}; };
}) })
.parse(process.env); .parse(process.env);
console.log(envs);
const app = fastify({ const app = fastify({
logger: true, logger: true,
exposeHeadRoutes: true, exposeHeadRoutes: true,
@ -41,8 +43,7 @@ const context = async (req: FastifyRequest) => {
app.register(fastifyCors, { app.register(fastifyCors, {
origin: (origin, callback) => { origin: (origin, callback) => {
if (envs.allowedOrigins.includes(origin || '*')) if (!origin || allowedOrigins.includes(origin)) return callback(null, true);
return callback(null, true);
return callback(new Error('Not allowed'), false); return callback(new Error('Not allowed'), false);
}, },
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
@ -52,7 +53,6 @@ app.register(fastifyCors, {
app.register(mercurius, { app.register(mercurius, {
schema, schema,
subscription: true, subscription: true,
graphiql: true,
context, context,
}); });
@ -75,7 +75,7 @@ mercuriusCodegen(app, {
}, },
}); });
app.listen({ port: 8080 }, (err, address) => { app.listen({ host, port }, (err, address) => {
if (err) { if (err) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);

View file

@ -21,7 +21,7 @@ COPY . .
ARG NODE_ENV ARG NODE_ENV
ARG VITE_API_URL ARG VITE_API_URL
ARG VITE_WS_URL ARG VITE_WS_URL
RUN npm run web:build RUN npx turbo run build --filter=web
# Production image, copy all the files and run the server # Production image, copy all the files and run the server
FROM base AS runner FROM base AS runner

View file

@ -6,11 +6,12 @@ services:
container_name: unreal-chat-api container_name: unreal-chat-api
restart: unless-stopped restart: unless-stopped
environment: environment:
- NODE_ENV=production
- DATABASE_URL=${DATABASE_URL} - DATABASE_URL=${DATABASE_URL}
- JWT_SECRET=your-secret-key - JWT_SECRET=your-secret-key
- PORT=4000 - API_PORT=4000
ports: - ALLOWED_ORIGINS=${ALLOWED_ORIGINS}
- "4000:4000" - API_HOST=${API_HOST}
networks: networks:
- default-network - default-network

View file

@ -4,6 +4,7 @@
"scripts": { "scripts": {
"build": "turbo run build", "build": "turbo run build",
"dev": "turbo run dev", "dev": "turbo run dev",
"start:api": "turbo run start --filter=api",
"lint": "turbo run lint", "lint": "turbo run lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\"", "format": "prettier --write \"**/*.{ts,tsx,md}\"",
"check-types": "turbo run check-types" "check-types": "turbo run check-types"

View file

@ -2,7 +2,15 @@
"$schema": "https://turbo.build/schema.json", "$schema": "https://turbo.build/schema.json",
"ui": "tui", "ui": "tui",
"globalDependencies": [".env"], "globalDependencies": [".env"],
"globalEnv": ["NODE_ENV", "DATABASE_URL", "VITE_API_URL", "VITE_WS_URL"], "globalEnv": [
"ALLOWED_ORIGINS",
"API_HOST",
"API_PORT",
"DATABASE_URL",
"NODE_ENV",
"VITE_API_URL",
"VITE_WS_URL"
],
"tasks": { "tasks": {
"build": { "build": {
"dependsOn": ["^build"], "dependsOn": ["^build"],