File size: 1,510 Bytes
32b70a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import express, { type Express } from "express";
import cors from "cors";
import pinoHttp from "pino-http";
import path from "path";
import fs from "fs";
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);

const possiblePaths = [
  path.resolve(process.cwd(), "artifacts/io-ui/dist"),
  path.resolve(process.cwd(), "artifacts/io-ui/dist/public"),
  path.resolve(__dirname, "../../io-ui/dist"),
  path.resolve(__dirname, "../../io-ui/dist/public"),
  path.resolve(__dirname, "../artifacts/io-ui/dist"),
  path.resolve(__dirname, "../artifacts/io-ui/dist/public"),
];

const publicPath = possiblePaths.find(p => fs.existsSync(p)) || possiblePaths[0];

if (fs.existsSync(publicPath)) {
  logger.info({ publicPath }, "Serving static frontend assets");
  app.use(express.static(publicPath));
  app.get("/{*path}", (_req, res) => {
    res.sendFile(path.join(publicPath, "index.html"));
  });
} else {
  logger.warn({ publicPath, attempted: possiblePaths }, "Frontend build folder not found. API-only mode active.");
}

export default app;