-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
70 lines (60 loc) · 2 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
import dotenv = require('dotenv');
dotenv.config();
import 'reflect-metadata';
import 'module-alias/register';
import * as express from 'express';
import { Cron } from '~/services/Cron';
import * as bodyParser from 'body-parser';
import * as compression from 'compression';
import { createConnection } from 'typeorm';
import { useExpressServer } from 'routing-controllers';
import { useMiddeware } from '~/utils/useMiddeware';
import { AuthorizationMiddleware } from '~/middlewares/Authorization';
import { ErrorHandlerMiddleware } from '~/middlewares/ErrorHandler';
import { MorganMiddleware } from '~/middlewares/Morgan';
import * as Sentry from '@sentry/node';
import config from '~/config';
import { Discord } from '~/services/Discord';
import * as cors from 'cors';
const start = async () => {
const app = express();
// db connection
await createConnection();
console.log('Database connection established.');
// init discord client
await Discord.initialize();
console.log('Discord client initialized.');
if (config.sentry.enabled) {
Sentry.init({ dsn: config.sentry.dsn });
app.use(Sentry.Handlers.requestHandler());
console.log('Sentry initialize.');
}
// middewares
app.use(bodyParser.json());
app.use(
cors({
credentials: true,
origin: [
'http://test.online',
'https://online.codingblocks.com',
'https://online.codingblocks.xyz',
'http://127.0.0.1:4200',
],
}),
);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(compression());
// add api controllers
app.use('/api', useMiddeware(AuthorizationMiddleware));
useExpressServer(app, {
controllers: [__dirname + '/controllers/**/*.js'],
middlewares: [MorganMiddleware, ErrorHandlerMiddleware],
defaultErrorHandler: false,
validation: true,
});
app.listen(config.app.port, config.app.host, async () => {
console.log(`Started server at http://${config.app.host}:${config.app.port}`);
});
config.cron.enabled && Cron.initialize();
};
start();