-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.ts
117 lines (94 loc) · 3.53 KB
/
server.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import * as express from "express";
import * as compression from "compression";
import * as bodyParser from "body-parser";
import * as passport from "passport";
import * as config from "./config/config";
import * as http from "http";
import {Auth} from "./auth/auth";
import {Models} from "./models";
import {Router} from "./routes/index";
import {errorHandler} from "./errors/ErrorHandler";
import {Roles} from "./auth/roles";
import {MessageManager} from "./managers/MessageManager";
import {InternalServerError} from "./errors/InternalServerError";
import {logger} from "./lib/logger";
import morgan = require("morgan");
const amqplib = require('amqplib');
export class Server {
public static app: express.Express;
// TODO Make all of this async
constructor() {}
public static async initializeApp(): Promise<http.Server> {
try {
require('dotenv').config();
Server.app = express();
// Configure application
Server.configureApp();
// Initialize OAuth
Server.initializeAuth();
// Initialize role based security
Server.initializeRoles();
// Initialize Routes
Router.initializeRoutes(Server.app);
// Initialize RabbitMQ Connection
amqplib.connect('amqp://localhost').then(connection => {
MessageManager.connection = connection;
}, () => {
logger.error("Could not initialize RabbitMQ Server");
throw new InternalServerError("Cannot connect to RabbitMQ Server");
});
Server.app.use(errorHandler);
process.on('unhandledRejection', (reason, p) => {
logger.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
// Initialize Database then bootstrap application
try {
await Server.initializeDatabase();
} catch(error) {
logger.error("Failed to initialize database", error);
}
return Server.app.listen(Server.app.get("port"));
} catch(error) {
throw new InternalServerError(error.message);
}
}
private static initializeDatabase() {
const nodeEnv = process.env.NODE_ENV;
if(nodeEnv) {
const sequelizeConfig = config[nodeEnv];
const models = new Models(sequelizeConfig);
return models.initModels();
} else {
throw new InternalServerError("No NODE ENV set");
}
}
private static initializeAuth() {
Server.app.use(passport.initialize());
Auth.serializeUser();
Auth.useBasicStrategy();
Auth.useBearerStrategy();
Auth.useLocalStrategy();
Auth.useFacebookTokenStrategy();
}
private static initializeRoles() {
Roles.buildRoles();
Server.app.use(Roles.middleware());
}
private static configureApp() {
// all environments
Server.app.set("port", process.env.PORT || 3000);
Server.app.use(bodyParser.urlencoded({ extended: true }));
Server.app.use(bodyParser.json());
Server.app.use(compression());
Server.app.use(morgan('dev', {
skip: function (req, res) {
return res.statusCode < 400;
}, stream: process.stderr
}));
Server.app.use(morgan('dev', {
skip: function (req, res) {
return res.statusCode >= 400;
}, stream: process.stdout
}));
}
}