dispatch-server deploy

This commit is contained in:
nocnico
2025-05-26 23:03:04 +02:00
parent a8a94d032f
commit 03acaa052f
12 changed files with 64 additions and 55 deletions

View File

@@ -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"]
CMD ["pnpm", "--dir", "apps/dispatch-server", "run", "start"]

View File

@@ -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";

View File

@@ -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");

View File

@@ -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",

View File

@@ -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,

View File

@@ -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({

View File

@@ -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;

View File

@@ -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) => {

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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({

3
pnpm-lock.yaml generated
View File

@@ -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