aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: dcfc0117ca337be0e99429f7f6c2459e17cd2c7b (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import dotenv from "dotenv";
import express from "express";
import winston from "winston";
import expressWinston from "express-winston";
import path from "path";

import routes from "./routes";
import { EnvConfig } from "./metadata";
import loggerConfig from "./util/logger";
import { configOrError, handleStartup, handleShutdown } from "./config";

dotenv.config();

const LOG_LEVEL = process.env.LOG_LEVEL ?? "info";
const logger = winston.createLogger(loggerConfig("ROOT", LOG_LEVEL));

// Application-wide configuration settings
const ENV_CONFIG: EnvConfig = {
  // Express configuration
  bindAddress: process.env.BIND_ADDRESS ?? "localhost",
  port: Number(process.env.PORT ?? 3000),
  uploadLimit: Number(process.env.UPLOAD_LIMIT ?? 4194304),

  // Database connection information
  dbName: process.env.MONGO_DB ?? "ao-coverage",
  dbUri: configOrError("MONGO_URI", logger),

  // Where the server should say it's located
  targetUrl: process.env.TARGET_URL ?? "http://localhost:3000",

  // Directories used for serving static or uploaded files
  publicDir: path.join(__dirname, "..", "public"),
  hostDir: configOrError("HOST_DIR", logger),

  // Application configuration
  stage1: Number(process.env.STAGE_1 ?? 95),
  stage2: Number(process.env.STAGE_2 ?? 80),
  logLevel: LOG_LEVEL,
};

handleStartup(ENV_CONFIG, process.env.TOKEN, logger).then((metadata) => {
  const app: express.Application = express();

  app.use(
    expressWinston.logger({
      ...loggerConfig("HTTP", ENV_CONFIG.logLevel),
      colorize: true,
      // filter out token query param from URL
      msg: '{{req.method}} {{req.url.replace(/token=[-\\w.~]*(&*)/, "token=$1")}} - {{res.statusCode}} {{res.responseTime}}ms',
    })
  );

  // actual app routes
  app.use(routes(metadata));

  app.use(
    expressWinston.errorLogger(loggerConfig("_ERR", ENV_CONFIG.logLevel))
  );

  const server = app.listen(ENV_CONFIG.port, ENV_CONFIG.bindAddress, () => {
    logger.info(
      "Express has started: http://%s:%d/",
      ENV_CONFIG.bindAddress,
      ENV_CONFIG.port
    );
  });

  // application exit handling
  const signalCodes: NodeJS.Signals[] = ["SIGTERM", "SIGHUP", "SIGINT"];
  signalCodes.map((code: NodeJS.Signals) => {
    process.on(code, handleShutdown(metadata, server, logger));
  });
});