diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile index ed5801a..26ef579 100644 --- a/apps/api/Dockerfile +++ b/apps/api/Dockerfile @@ -8,10 +8,11 @@ COPY . . RUN npm install # Generate Prisma client -RUN npm run prisma:generate +RUN cd apps/api && npm run prisma:generate # Build the project -RUN npm run api:build +RUN npx turbo run build --filter=api +EXPOSE 4000 # Start the server -ENTRYPOINT [ "npm", "run", "api:start" ] +ENTRYPOINT [ "npm", "run", "start:api" ] diff --git a/apps/api/README.md b/apps/api/README.md index 195b602..783e3c9 100644 --- a/apps/api/README.md +++ b/apps/api/README.md @@ -42,7 +42,7 @@ npm run prisma:migrate 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 diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index fc49ed8..c0c32ba 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -11,19 +11,21 @@ import { z } from 'zod'; dotenv.config({ path: '../../.env' }); -const envs = z +const { allowedOrigins, port, host } = z .object({ - ALLOWED_ORIGINS: z.string().default('http://localhost:5173'), + ALLOWED_ORIGINS: z.string(), + API_HOST: z.string(), + API_PORT: z.coerce.number(), }) .transform((env) => { return { allowedOrigins: env.ALLOWED_ORIGINS.split(','), + port: env.API_PORT, + host: env.API_HOST, }; }) .parse(process.env); -console.log(envs); - const app = fastify({ logger: true, exposeHeadRoutes: true, @@ -41,8 +43,7 @@ const context = async (req: FastifyRequest) => { app.register(fastifyCors, { origin: (origin, callback) => { - if (envs.allowedOrigins.includes(origin || '*')) - return callback(null, true); + if (!origin || allowedOrigins.includes(origin)) return callback(null, true); return callback(new Error('Not allowed'), false); }, methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], @@ -52,7 +53,6 @@ app.register(fastifyCors, { app.register(mercurius, { schema, subscription: true, - graphiql: true, context, }); @@ -75,7 +75,7 @@ mercuriusCodegen(app, { }, }); -app.listen({ port: 8080 }, (err, address) => { +app.listen({ host, port }, (err, address) => { if (err) { console.error(err); process.exit(1); diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index fe5b8b8..b104fba 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -21,7 +21,7 @@ COPY . . ARG NODE_ENV ARG VITE_API_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 FROM base AS runner diff --git a/docker-compose.yml b/docker-compose.yml index 204797d..c52df99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,11 +6,12 @@ services: container_name: unreal-chat-api restart: unless-stopped environment: + - NODE_ENV=production - DATABASE_URL=${DATABASE_URL} - JWT_SECRET=your-secret-key - - PORT=4000 - ports: - - "4000:4000" + - API_PORT=4000 + - ALLOWED_ORIGINS=${ALLOWED_ORIGINS} + - API_HOST=${API_HOST} networks: - default-network diff --git a/package.json b/package.json index 8c67fd4..8fa7b12 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "scripts": { "build": "turbo run build", "dev": "turbo run dev", + "start:api": "turbo run start --filter=api", "lint": "turbo run lint", "format": "prettier --write \"**/*.{ts,tsx,md}\"", "check-types": "turbo run check-types" diff --git a/turbo.json b/turbo.json index 31dae79..c46ed61 100644 --- a/turbo.json +++ b/turbo.json @@ -2,7 +2,15 @@ "$schema": "https://turbo.build/schema.json", "ui": "tui", "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": { "build": { "dependsOn": ["^build"],