| 1 | import express from "express";
|
| 2 | import cors from "cors";
|
| 3 | import bodyParser from "body-parser";
|
| 4 | import morgan from "morgan";
|
| 5 | import mainRouter from "@app/routes/main";
|
| 6 | import userRouter from "@app/routes/users";
|
| 7 | import authRouter from "@app/routes/auth";
|
| 8 | import moodRouter from "@app/routes/moods";
|
| 9 | import triggerRouter from "@app/routes/triggers";
|
| 10 | import sleepRecordRouter from "@app/routes/sleepRecords";
|
| 11 | import insightRouter from "@app/routes/insights";
|
| 12 | import careActionsRouter from "@app/routes/careActions";
|
| 13 | import medicineRegimensRouter from "@app/routes/medicineRegimens";
|
| 14 | import exportsRouter from "@app/routes/exports";
|
| 15 | import reportRouter from "@app/reports/report.router";
|
| 16 | import { errorHandler } from "@app/middleware/errorHandler";
|
| 17 | import { PrismaClient } from "@prisma/client";
|
| 18 | import {
|
| 19 | bootWorkers,
|
| 20 | closeAllWorkers,
|
| 21 | closeAllQueues,
|
| 22 | scheduleInsights,
|
| 23 | } from "@app/lib/queue";
|
| 24 |
|
| 25 | import pino from "pino";
|
| 26 | import pinoHttp from "pino-http";
|
| 27 | import path from "path";
|
| 28 |
|
| 29 | export function createApp() {
|
| 30 | const app = express();
|
| 31 | bootWorkers();
|
| 32 | scheduleInsights();
|
| 33 |
|
| 34 | // Configure CORS
|
| 35 | app.use(
|
| 36 | cors({
|
| 37 | origin: process.env.FRONTEND_URL || "*",
|
| 38 | credentials: true,
|
| 39 | }),
|
| 40 | );
|
| 41 |
|
| 42 | app.use(bodyParser.json());
|
| 43 |
|
| 44 | const isDev = process.env.NODE_ENV !== "production";
|
| 45 |
|
| 46 | const logger = pino({
|
| 47 | level: process.env.LOG_LEVEL || "info",
|
| 48 | ...(isDev && {
|
| 49 | transport: {
|
| 50 | target: "pino-pretty",
|
| 51 | options: {
|
| 52 | colorize: true,
|
| 53 | singleLine: false,
|
| 54 | messageFormat: "{if levelLabel}{levelLabel} - {end}{msg}",
|
| 55 | },
|
| 56 | },
|
| 57 | }),
|
| 58 | });
|
| 59 |
|
| 60 | // Use pino-http for HTTP logging (better than Morgan for structured logs)
|
| 61 | app.use(pinoHttp({ logger }));
|
| 62 |
|
| 63 | app.use("/users", userRouter);
|
| 64 | app.use("/auth", authRouter);
|
| 65 | app.use("/moods", moodRouter);
|
| 66 | app.use("/sleep_records", sleepRecordRouter);
|
| 67 | app.use("/triggers", triggerRouter);
|
| 68 | app.use("/insights", insightRouter);
|
| 69 | app.use("/care-actions", careActionsRouter);
|
| 70 | app.use("/medicine-regimens", medicineRegimensRouter);
|
| 71 | app.use("/exports", exportsRouter);
|
| 72 | app.use("/reports", reportRouter);
|
| 73 |
|
| 74 | app.use(errorHandler); // always last
|
| 75 |
|
| 76 | // TODO: flesh the actual page here
|
| 77 | app.use(express.static(path.join(__dirname, "./static")));
|
| 78 | app.get(/.*/, (req, res) => {
|
| 79 | res.sendFile(path.join(__dirname, "./static/index.html"));
|
| 80 | });
|
| 81 |
|
| 82 | process.on("SIGTERM", shutdown);
|
| 83 | process.on("SIGINT", shutdown);
|
| 84 |
|
| 85 | return app;
|
| 86 | }
|
| 87 |
|
| 88 | const shutdown = async () => {
|
| 89 | await Promise.all([closeAllWorkers(), closeAllQueues()]);
|
| 90 | process.exit(0);
|
| 91 | };
|