-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
82 lines (68 loc) · 2.74 KB
/
Copy pathapp.ts
File metadata and controls
82 lines (68 loc) · 2.74 KB
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
74
75
76
77
78
79
80
81
82
import chalk from "chalk";
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import morgan from "morgan";
import multer from "multer";
import { AccountMiddleware } from "./src/middleware/AccountMiddleware";
import { PaginationMiddleware } from "./src/middleware/PaginationMiddleware";
import { RouterMiddleware } from "./src/middleware/RouterMiddleware";
// Add environment variables.
dotenv.config();
// Create a new express application instance.
const app = express();
// Add any actual middleware here.
app.use(cors());
app.use(express.json());
app.use(multer().any());
app.use(morgan((tokens, req, res) => {
let method = tokens.method(req, res);
switch (method) {
case "GET":
method = chalk.bgHex("#00ffaa").black(` ${method} `);
break;
case "POST":
method = chalk.bgHex("#ffc800").black(` ${method} `);
break;
case "PUT":
method = chalk.bgHex("#00aaff").black(` ${method} `);
break;
case "DELETE":
method = chalk.bgHex("#ff0040").black(` ${method} `);
break;
case "PATCH":
method = chalk.bgHex("#4000ff").black(` ${method} `);
break;
}
const status = parseInt(tokens.status(req, res) ?? "500");
let statusString = "";
let statusHeader;
if (status >= 200 && status < 400) {
statusString = chalk.bgHex("#00ffaa").black(` ${status} `);
statusHeader = chalk.hex("#00ffaa")(" SUCCESS");
} else if (status >= 400 && status < 500) {
statusString = chalk.bgHex("#ff0040").black(` ${status} `);
statusHeader = chalk.hex("#ff0040")(" FAILURE");
} else if (status >= 500 && status < 600) {
statusString = chalk.bgHex("#4000ff").black(` ${status} `);
statusHeader = chalk.hex("#4000ff")(" ERROR");
}
const url = tokens.url(req, res);
statusString = statusString.padStart(Math.max(1, 15 - (url?.length ?? 0)), "E");
const now = new Date();
const format = new Intl.DateTimeFormat();
return [`[${chalk.gray(format.format(now))}]`, statusHeader, method, tokens.url(req, res), statusString, "-", tokens["response-time"](req, res), "ms"].join(" ");
}));
// Our own middleware next.
app.use(AccountMiddleware.handle);
app.use(PaginationMiddleware.handle);
// Handle our own lower router-esque middleware.
RouterMiddleware.handle(app);
// Create a listener.
app.listen(process.env.CL_PORT, () => {
const now = new Date();
const format = new Intl.DateTimeFormat();
console.log(`[${chalk.gray(format.format(now))}] ${chalk.gray("Challenge List API: ")}`);
console.log(`[${chalk.gray(format.format(now))}] Server started on port ${process.env.CL_PORT}!`);
console.log("");
});