diff --git a/apps/dispatch-server/Dockerfile b/apps/dispatch-server/Dockerfile index ae216d30..233b8c34 100644 --- a/apps/dispatch-server/Dockerfile +++ b/apps/dispatch-server/Dockerfile @@ -1,29 +1,48 @@ -FROM node:22-alpine +FROM node:22-alpine AS base + +ENV PNPM_HOME="/usr/local/pnpm" +ENV PATH="${PNPM_HOME}:${PATH}" +RUN corepack enable && corepack prepare pnpm@latest --activate + +RUN pnpm add -g turbo@^2.5 + +FROM base AS builder +RUN apk update +RUN apk add --no-cache libc6-compat -# Set the working directory WORKDIR /usr/app -# Copy package.json and package-lock.json -COPY package*.json ./ - -# Install dependencies -RUN npm install - -# Change ownership to the non-root user -RUN chown -R node:node /usr/app - -# Copy the rest of the application code COPY . . -# Build the application -RUN npm run build +RUN turbo prune dispatch-server --docker + +FROM base AS installer +RUN apk update +RUN apk add --no-cache libc6-compat + +WORKDIR /usr/app + +COPY --from=builder /usr/app/out/json/ . +RUN pnpm install --frozen-lockfile + +# Build the project +COPY --from=builder /usr/app/out/full/ . + +RUN turbo run build + +FROM base AS runner +WORKDIR /usr/app + +# Don't run production as root +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs +USER nextjs + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=installer --chown=nextjs:nodejs /usr/app/ ./ # Expose the application port EXPOSE 3002 -# Run container as non-root (unprivileged) user -# The "node" user is provided in the Node.js Alpine base image -USER node - -# Command to run the application -CMD ["node", "index.js"] \ No newline at end of file +CMD ["pnpm", "--dir", "apps/dispatch-server", "run", "start"] \ No newline at end of file diff --git a/apps/dispatch-server/index.ts b/apps/dispatch-server/index.ts index bac7492e..09581c3e 100644 --- a/apps/dispatch-server/index.ts +++ b/apps/dispatch-server/index.ts @@ -7,11 +7,11 @@ import { jwtMiddleware } from "modules/socketJWTmiddleware"; import { pubClient, subClient } from "modules/redis"; import { handleConnectDispatch } from "socket-events/connect-dispatch"; import router from "routes/router"; -import cors from "cors"; import { handleSendMessage } from "socket-events/send-message"; import { handleConnectPilot } from "socket-events/connect-pilot"; import { handleConnectDesktop } from "socket-events/connect-desktop"; import cookieParser from "cookie-parser"; +import cors from "cors"; import { authMiddleware } from "modules/expressMiddleware"; import { prisma, User } from "@repo/db"; import { Request, Response, NextFunction } from "express"; diff --git a/apps/dispatch-server/modules/redis.ts b/apps/dispatch-server/modules/redis.ts index 48b2e034..e31cab05 100644 --- a/apps/dispatch-server/modules/redis.ts +++ b/apps/dispatch-server/modules/redis.ts @@ -1,7 +1,7 @@ -import { createClient } from "redis"; +import { createClient, RedisClientType } from "redis"; -export const pubClient = createClient(); -export const subClient = pubClient.duplicate(); +export const pubClient: RedisClientType = createClient(); +export const subClient: RedisClientType = pubClient.duplicate(); Promise.all([pubClient.connect(), subClient.connect()]).then(() => { console.log("Redis connected"); diff --git a/apps/dispatch-server/package.json b/apps/dispatch-server/package.json index b5c753aa..24189b39 100644 --- a/apps/dispatch-server/package.json +++ b/apps/dispatch-server/package.json @@ -5,12 +5,14 @@ }, "scripts": { "dev": "nodemon --signal SIGINT", + "start": "node index.js", "build": "tsc" }, "devDependencies": { "@repo/db": "*", "@repo/typescript-config": "*", "@types/cookie-parser": "^1.4.8", + "@types/cors": "^2.8.18", "@types/express": "^5.0.0", "@types/node": "^22.13.5", "@types/nodemailer": "^6.4.17", diff --git a/apps/dispatch-server/routes/aircraft.ts b/apps/dispatch-server/routes/aircraft.ts index 86e7b59c..e3e97c21 100644 --- a/apps/dispatch-server/routes/aircraft.ts +++ b/apps/dispatch-server/routes/aircraft.ts @@ -1,14 +1,8 @@ -import { - ConnectedAircraft, - getPublicUser, - MissionLog, - Prisma, - prisma, -} from "@repo/db"; +import { ConnectedAircraft, getPublicUser, MissionLog, Prisma, prisma } from "@repo/db"; import { Router } from "express"; import { io } from "../index"; -const router = Router(); +const router: Router = Router(); // Get all connectedAircrafts router.post("/", async (req, res) => { @@ -97,10 +91,7 @@ router.patch("/:id", async (req, res) => { }); } - io.to("dispatchers").emit( - "update-connectedAircraft", - updatedConnectedAircraft, - ); + io.to("dispatchers").emit("update-connectedAircraft", updatedConnectedAircraft); io.to(`user:${updatedConnectedAircraft.userId}`).emit( "aircraft-update", updatedConnectedAircraft, diff --git a/apps/dispatch-server/routes/dispatcher.ts b/apps/dispatch-server/routes/dispatcher.ts index b56eab9b..ab38ab3a 100644 --- a/apps/dispatch-server/routes/dispatcher.ts +++ b/apps/dispatch-server/routes/dispatcher.ts @@ -2,7 +2,7 @@ import { prisma } from "@repo/db"; import { Router } from "express"; import { pubClient } from "modules/redis"; -const router = Router(); +const router: Router = Router(); router.get("/", async (req, res) => { const user = await prisma.connectedDispatcher.findMany({ diff --git a/apps/dispatch-server/routes/livekit.ts b/apps/dispatch-server/routes/livekit.ts index f48b97cd..1fb3acc9 100644 --- a/apps/dispatch-server/routes/livekit.ts +++ b/apps/dispatch-server/routes/livekit.ts @@ -2,8 +2,7 @@ import { Router } from "express"; import { AccessToken } from "livekit-server-sdk"; if (!process.env.LIVEKIT_API_KEY) throw new Error("LIVEKIT_API_KEY not set"); -if (!process.env.LIVEKIT_API_SECRET) - throw new Error("LIVEKIT_API_SECRET not set"); +if (!process.env.LIVEKIT_API_SECRET) throw new Error("LIVEKIT_API_SECRET not set"); const createToken = async (roomName: string) => { // If this room doesn't exist, it'll be automatically created when the first @@ -11,24 +10,19 @@ const createToken = async (roomName: string) => { // Identifier to be used for participant. // It's available as LocalParticipant.identity with livekit-client SDK // TODO: Move function to dispatch nextjs app as API route to use authentication of nextAuth - const participantName = - "quickstart-username" + Math.random().toString(36).substring(7); + const participantName = "quickstart-username" + Math.random().toString(36).substring(7); - const at = new AccessToken( - process.env.LIVEKIT_API_KEY, - process.env.LIVEKIT_API_SECRET, - { - identity: participantName, - // Token to expire after 10 minutes - ttl: "10m", - }, - ); + const at = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, { + identity: participantName, + // Token to expire after 10 minutes + ttl: "10m", + }); at.addGrant({ roomJoin: true, room: roomName }); return await at.toJwt(); }; -const router = Router(); +const router: Router = Router(); router.get("/token", async (req, res) => { const roomName = req.query.roomName as string; diff --git a/apps/dispatch-server/routes/mission.ts b/apps/dispatch-server/routes/mission.ts index 5d1b4852..11785685 100644 --- a/apps/dispatch-server/routes/mission.ts +++ b/apps/dispatch-server/routes/mission.ts @@ -12,7 +12,7 @@ import { io } from "../index"; import { sendNtfyMission } from "modules/ntfy"; import { sendAlert } from "modules/mission"; -const router = Router(); +const router: Router = Router(); // Get all missions router.post("/", async (req, res) => { diff --git a/apps/dispatch-server/routes/report.ts b/apps/dispatch-server/routes/report.ts index 00ea447c..325ffb92 100644 --- a/apps/dispatch-server/routes/report.ts +++ b/apps/dispatch-server/routes/report.ts @@ -2,7 +2,7 @@ import { Router } from "express"; import { prisma } from "@repo/db"; -const router = Router(); +const router: Router = Router(); router.put("/", async (req, res) => { try { diff --git a/apps/dispatch-server/routes/router.ts b/apps/dispatch-server/routes/router.ts index 968cf6cd..695e3d60 100644 --- a/apps/dispatch-server/routes/router.ts +++ b/apps/dispatch-server/routes/router.ts @@ -6,7 +6,7 @@ import statusRouter from "./status"; import aircraftsRouter from "./aircraft"; import reportRouter from "./report"; -const router = Router(); +const router: Router = Router(); router.use("/livekit", livekitRouter); router.use("/dispatcher", dispatcherRotuer); diff --git a/apps/dispatch-server/routes/status.ts b/apps/dispatch-server/routes/status.ts index b6beb8bc..539b1e8b 100644 --- a/apps/dispatch-server/routes/status.ts +++ b/apps/dispatch-server/routes/status.ts @@ -1,7 +1,7 @@ import { prisma } from "@repo/db"; import { Router } from "express"; -const router = Router(); +const router: Router = Router(); router.get("/connected-users", async (req, res) => { const connectedDispatcher = await prisma.connectedDispatcher.findMany({ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eae1dacd..d69b54a9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -205,6 +205,9 @@ importers: '@types/cookie-parser': specifier: ^1.4.8 version: 1.4.8(@types/express@5.0.2) + '@types/cors': + specifier: ^2.8.18 + version: 2.8.18 '@types/express': specifier: ^5.0.0 version: 5.0.2