-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
38 lines (33 loc) · 1.24 KB
/
index.ts
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
import express, { Application, json, urlencoded } from "express";
import cors from "cors";
import { wire } from "./init/wire";
import { initConfig } from "./init/config";
import logger from "./logging";
import { unwrap } from "./utils/unwrap";
import { initBailoutHandler } from "./handler/bailout";
import { initJson404Handler } from "./errors/json404";
// suppressing for now, typically, there is async initialization code
// eslint-disable-next-line @typescript-eslint/require-await
async function main(): Promise<void> {
const app: Application = express();
// if the config is invalid just throw, i.e. exit at startup
const config = unwrap(initConfig());
const { loggingMiddleware, errorMiddleware, helloHandler } = wire(config);
app.use(initBailoutHandler());
app.use(loggingMiddleware);
app.use(cors());
app.use(json());
app.use(urlencoded({ extended: true }));
app.use("/hello-world", helloHandler);
app.use("/", (_req, res) => res.redirect("/hello-world"));
app.use(errorMiddleware);
app.use(initJson404Handler());
app.listen(config.port, (): void => {
logger.info(`Server started on port ${config.port}`);
});
}
if (require.main === module) {
main().catch((err) => {
logger.error(err, "Error executing main()");
});
}