Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ RUN yarn build
FROM node:22-slim
RUN apt-get update && apt-get install -y curl
WORKDIR /app
RUN yarn add serve
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production
COPY --from=builder /app/dist ./dist
CMD ["yarn", "serve", "-s", "dist", "-l", "3000"]
COPY server.mjs .
CMD ["node", "server.mjs"]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview",
"start": "node server.mjs",
"openapi-ts": "openapi-ts",
"prettier": "prettier",
"test": "output=$(vitest run 2>&1) || { printf '%s\\n' \"$output\"; exit 1; }",
Expand All @@ -27,7 +28,7 @@
"react-icons": "^5.5.0",
"react-markdown": "^10.1.0",
"react-router-dom": "^7.2.0",
"serve": "^14.2.4",
"serve-handler": "^6.1.6",
"tailwindcss": "^4.0.9",
"uplot": "^1.6.32"
},
Expand Down
63 changes: 63 additions & 0 deletions server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import http from "node:http";
import { performance } from "node:perf_hooks";
import process from "node:process";
import handler from "serve-handler";

const port = Number(process.env.PORT) || 3000;

function log(level, msg, fields = {}) {
process.stdout.write(
`${JSON.stringify({ ts: new Date().toISOString(), level, msg, ...fields })}\n`,
);
}

function clientIp(req) {
const forwarded = req.headers["x-forwarded-for"];
if (typeof forwarded === "string" && forwarded.length > 0) {
return forwarded.split(",")[0].trim();
}
if (Array.isArray(forwarded) && forwarded.length > 0) {
return forwarded[0].split(",")[0].trim();
}
return req.socket.remoteAddress ?? undefined;
}

const server = http.createServer(async (req, res) => {
const started = performance.now();

res.on("finish", () => {
log("info", "request", {
method: req.method,
url: req.url,
status: res.statusCode,
duration_ms: Math.round(performance.now() - started),
ip: clientIp(req),
});
});

try {
await handler(req, res, {
public: "dist",
rewrites: [{ source: "**", destination: "/index.html" }],
});
} catch (err) {
log("error", "request_handler_failed", {
method: req.method,
url: req.url,
ip: clientIp(req),
err: err instanceof Error ? err.message : String(err),
});
if (!res.headersSent) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
}
}
});

server.on("error", (err) => {
log("error", "server_error", { err: err.message });
});

server.listen(port, () => {
log("info", "listening", { port });
});
Loading
Loading