Initial commit
This commit is contained in:
34
artifacts/api-server/src/app.ts
Normal file
34
artifacts/api-server/src/app.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import express, { type Express } from "express";
|
||||
import cors from "cors";
|
||||
import pinoHttp from "pino-http";
|
||||
import router from "./routes";
|
||||
import { logger } from "./lib/logger";
|
||||
|
||||
const app: Express = express();
|
||||
|
||||
app.use(
|
||||
pinoHttp({
|
||||
logger,
|
||||
serializers: {
|
||||
req(req) {
|
||||
return {
|
||||
id: req.id,
|
||||
method: req.method,
|
||||
url: req.url?.split("?")[0],
|
||||
};
|
||||
},
|
||||
res(res) {
|
||||
return {
|
||||
statusCode: res.statusCode,
|
||||
};
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.use("/api", router);
|
||||
|
||||
export default app;
|
||||
25
artifacts/api-server/src/index.ts
Normal file
25
artifacts/api-server/src/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import app from "./app";
|
||||
import { logger } from "./lib/logger";
|
||||
|
||||
const rawPort = process.env["PORT"];
|
||||
|
||||
if (!rawPort) {
|
||||
throw new Error(
|
||||
"PORT environment variable is required but was not provided.",
|
||||
);
|
||||
}
|
||||
|
||||
const port = Number(rawPort);
|
||||
|
||||
if (Number.isNaN(port) || port <= 0) {
|
||||
throw new Error(`Invalid PORT value: "${rawPort}"`);
|
||||
}
|
||||
|
||||
app.listen(port, (err) => {
|
||||
if (err) {
|
||||
logger.error({ err }, "Error listening on port");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
logger.info({ port }, "Server listening");
|
||||
});
|
||||
0
artifacts/api-server/src/lib/.gitkeep
Normal file
0
artifacts/api-server/src/lib/.gitkeep
Normal file
20
artifacts/api-server/src/lib/logger.ts
Normal file
20
artifacts/api-server/src/lib/logger.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import pino from "pino";
|
||||
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
|
||||
export const logger = pino({
|
||||
level: process.env.LOG_LEVEL ?? "info",
|
||||
redact: [
|
||||
"req.headers.authorization",
|
||||
"req.headers.cookie",
|
||||
"res.headers['set-cookie']",
|
||||
],
|
||||
...(isProduction
|
||||
? {}
|
||||
: {
|
||||
transport: {
|
||||
target: "pino-pretty",
|
||||
options: { colorize: true },
|
||||
},
|
||||
}),
|
||||
});
|
||||
0
artifacts/api-server/src/middlewares/.gitkeep
Normal file
0
artifacts/api-server/src/middlewares/.gitkeep
Normal file
11
artifacts/api-server/src/routes/health.ts
Normal file
11
artifacts/api-server/src/routes/health.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Router, type IRouter } from "express";
|
||||
import { HealthCheckResponse } from "@workspace/api-zod";
|
||||
|
||||
const router: IRouter = Router();
|
||||
|
||||
router.get("/healthz", (_req, res) => {
|
||||
const data = HealthCheckResponse.parse({ status: "ok" });
|
||||
res.json(data);
|
||||
});
|
||||
|
||||
export default router;
|
||||
8
artifacts/api-server/src/routes/index.ts
Normal file
8
artifacts/api-server/src/routes/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Router, type IRouter } from "express";
|
||||
import healthRouter from "./health";
|
||||
|
||||
const router: IRouter = Router();
|
||||
|
||||
router.use(healthRouter);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user