-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
75 lines (67 loc) · 2.64 KB
/
app.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
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
import dotenv from 'dotenv';
const result = dotenv.config({ path: `.env.${process.env.DEBUG ? 'dev' : 'prod'}` });
if (result.error) {
throw result.error;
}
import express from 'express';
import * as http from 'http';
import * as winston from 'winston';
import * as expressWinston from 'express-winston';
import cors from 'cors';
import debug from 'debug';
import helmet from 'helmet';
import { CommonRoutesConfig } from './common/common.routes.config';
import { AuthRoutes } from './auth/auth.routes.config';
import { UsersRoutes } from './users/users.routes.config';
import { NewsRoutes } from './news/news.routes.config';
import { BackupsRoutes } from './backups/backups.routes.config';
import { DesignsRoutes } from './designs/designs.routes.config';
import { GamesRoutes } from './games/games.routes.config'; // big mess
import { InfosRoutes } from './infos/infos.routes.config';
import { DonationsRoutes } from './donations/donations.routes.config';
const app: express.Application = express();
const server: http.Server = http.createServer(app);
const routes: Array<CommonRoutesConfig> = [];
const debugLog: debug.IDebugger = debug('app');
app.use(express.json({limit: '4MB'}));
app.use(express.urlencoded({ extended: true,
verify: function(req, res, buf, encoding) {
// @ts-ignore
req.textBody = buf.toString(encoding);
}
}))
app.use(cors());
app.use(helmet());
const loggerOptions: expressWinston.LoggerOptions = {
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.json(),
winston.format.prettyPrint(),
winston.format.colorize({ all: true })
),
msg: "[{{req.method}}({{res.statusCode}})] \"{{req.headers['cf-connecting-ip']}}\" requested \"{{req.url}}\" in {{res.responseTime}}ms",
};
if (!process.env.DEBUG) {
loggerOptions.meta = false; // when not debugging, make terse
loggerOptions.format = winston.format.combine(
winston.format.timestamp({
format: 'MMM-DD-YYYY HH:mm:ss'
}),
winston.format.printf(info => `${[info.timestamp]}: ${info.message}`),
);
}
app.use(expressWinston.logger(loggerOptions));
routes.push(new AuthRoutes(app));
routes.push(new UsersRoutes(app));
routes.push(new NewsRoutes(app));
routes.push(new BackupsRoutes(app));
routes.push(new DesignsRoutes(app));
routes.push(new GamesRoutes(app));
routes.push(new InfosRoutes(app));
routes.push(new DonationsRoutes(app));
export default server.listen(3000, '0.0.0.0', () => {
debugLog(`Server running and listening on port 3000`);
routes.forEach((route: CommonRoutesConfig) => {
debugLog(`Routes configured for ${route.getName()}`);
});
});