Skip to content

Commit

Permalink
api: Use pm2
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Sep 7, 2024
1 parent 8eb639b commit b23a28f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 56 deletions.
1 change: 0 additions & 1 deletion apps/whattheduck/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"dev": "concurrently -n vite,vue-tsc \"vite --port 8003 --host\" \"vue-tsc --noEmit --watch\"",
"build": "vite build && vue-tsc --noEmit",
"build-capgo": "capgo bundle zip",
"i-next": "for dep in $(jq -r '.dependencies | to_entries[] | select(.value == \"next\") | .key' package.json); do pnpm install \"${dep}@latest\"; done",
"ionic:build": "npm run build",
"preview": "vite preview",
"test:e2e": "cypress run",
Expand Down
4 changes: 3 additions & 1 deletion packages/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ COPY packages/prisma-schemas/client_dm/libquery_engine-debian-openssl-3.0.x.so.n
COPY packages/api/.env /app/
COPY packages/api/bundle.mjs /app/

RUN npm install pm2@latest -g

EXPOSE 3000
CMD ["node", "bundle.mjs"]
CMD ["pm2", "start", "--no-daemon", "-i", "max", "bundle.mjs"]
121 changes: 67 additions & 54 deletions packages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { instrument } from "@socket.io/admin-ui";
import dotenv from "dotenv";
import { createServer } from "http";
import { Server } from "socket.io";
import cluster from "cluster";
import { cpus } from "os";

import type { SessionUser } from "~dm-types/SessionUser";

Expand Down Expand Up @@ -84,63 +86,74 @@ const httpServer = createServer(async (req, res) => {
res.end();
});

httpServer.listen(3000);
console.log("WebSocket open on port 3000");

const io = new ServerWithUser(httpServer, {
cors: {
origin: true,
},
});

instrument(io, {
auth: false,
});
if (cluster.isPrimary) {
for (let i = 0; i < cpus().length; i++) {
cluster.fork();
}

io.use(OptionalAuthMiddleware);
io.use((_socket, next) => {
process.on("unhandledRejection", (reason: Error) => {
console.error(reason);
next(reason);
cluster.on("exit", (worker) => {
console.log(`Worker ${worker.process.pid} died, starting a new one`);
cluster.fork();
});

process.on("uncaughtException", (error: Error) => {
console.error(error);
next(error);
} else {
httpServer.listen(3000);
console.log("WebSocket open on port 3000");

const io = new ServerWithUser(httpServer, {
cors: {
origin: true,
},
});
next();

// app.all(
// /^\/(edgecreator\/(publish|edgesprites)|notifications)|(edges\/(published))|(\/demo\/reset)|(bookstores\/(approve|refuse))|(presentation-text\/(approve|refuse))/,
// [checkUserIsAdmin]
// );

// app.all(/^\/edgecreator\/(.+)/, [
// authenticateToken,
// checkUserIsEdgeCreatorEditor,
// ]);

// app.all(/^\/global-stats\/user\/list$/, [
// authenticateToken,
// checkUserIsEdgeCreatorEditor,
// ]);
instrument(io, {
auth: false,
});

// app.all(/^\/collection\/(.+)/, authenticateToken);
// app.all("/global-stats/user/collection/rarity", authenticateToken);
});
io.use(OptionalAuthMiddleware);
io.use((_socket, next) => {
process.on("unhandledRejection", (reason: Error) => {
console.error(reason);
next(reason);
});

process.on("uncaughtException", (error: Error) => {
console.error(error);
next(error);
});
next();

// app.all(
// /^\/(edgecreator\/(publish|edgesprites)|notifications)|(edges\/(published))|(\/demo\/reset)|(bookstores\/(approve|refuse))|(presentation-text\/(approve|refuse))/,
// [checkUserIsAdmin]
// );

// app.all(/^\/edgecreator\/(.+)/, [
// authenticateToken,
// checkUserIsEdgeCreatorEditor,
// ]);

// app.all(/^\/global-stats\/user\/list$/, [
// authenticateToken,
// checkUserIsEdgeCreatorEditor,
// ]);

// app.all(/^\/collection\/(.+)/, authenticateToken);
// app.all("/global-stats/user/collection/rarity", authenticateToken);
});

auth(io);
bookcase(io);
bookstores(io);
coa(io);
collection(io);
coverId(io);
edgecreator(io);
edges(io);
events(io);
feedback(io);
globalStats(io);
login(io);
presentationText(io);
publicCollection(io);
stats(io);
auth(io);
bookcase(io);
bookstores(io);
coa(io);
collection(io);
coverId(io);
edgecreator(io);
edges(io);
events(io);
feedback(io);
globalStats(io);
login(io);
presentationText(io);
publicCollection(io);
stats(io);
}

0 comments on commit b23a28f

Please sign in to comment.