-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
158 lines (136 loc) · 3.89 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import express from 'express';
import { get as config } from 'config';
import { init, Handlers } from "@sentry/node";
import jwt from 'express-jwt';
import { DatabaseSeed } from './temp/db.seeds';
import { ActuatorsRoute } from '@modules/actuators/actuators.route';
import { readFileSync } from 'fs';
import cors from 'cors';
import { version } from './package.json';
// Routes
import { AuthRoute } from './modules/auth/auth.route';
import { UsersRoute } from './modules/users/users.route';
import { NodesRoute } from './modules/nodes/nodes.route';
import { SensorsRoute } from './modules/sensors/sensors.route';
import { MqttRoute } from './modules/mqtt/mqtt.route';
// Middleware
import { injectUser } from '@modules/auth/auth.middleware';
// Services
import { AuthService } from '@modules/auth/auth.service';
import { UsersService } from '@modules/users/users.service';
import { NodesService } from '@modules/nodes/nodes.service';
import { SensorsService } from '@modules/sensors/sensors.service';
import { ActuatorsService } from '@modules/actuators/actuators.service';
import { MqttService } from '@modules/mqtt/mqtt.service';
export class App {
private readonly app: express.Application;
private readonly port: number;
constructor() {
this.app = express();
this.port = config('port');
this.app.use(express.json());
this.app.use(Handlers.requestHandler({
user: ['email']
}));
this.app.use(jwt({
secret: readFileSync("keys/private.key"),
requestProperty: 'token'
}).unless({
path: [
'/auth/login',
'/auth/refresh',
'/auth/register',
'/public'
]
}));
this.app.use(cors(this.corsOptionDelegate));
this.app.use(this.unauthorizedException);
this.app.use(injectUser);
this.initSentry();
this.initServices();
this.initRoutes();
this.seedDB();
}
/**
* Initialize Sentry
*/
private initSentry() {
if (!config('sentry')) return;
init({
dsn: config('sentry'),
environment: process.env.NODE_ENV,
release: version
});
}
/**
* Initialize Routes
*/
private initRoutes() {
this.app.use('/auth', new AuthRoute().getRouter());
this.app.use('/users', new UsersRoute().getRouter());
this.app.use('/nodes', new NodesRoute().getRouter());
this.app.use('/sensors', new SensorsRoute().getRouter());
this.app.use('/actuators', new ActuatorsRoute().getRouter());
this.app.use('/mqtt', new MqttRoute().getRouter());
}
/**
* Initialize Services
*/
private initServices() {
global.services = {
authService: new AuthService(),
usersService: new UsersService(),
nodesService: new NodesService(),
sensorsService: new SensorsService(),
actuatorsService: new ActuatorsService(),
mqttService: new MqttService()
};
}
/**
* Cors Option Delegate
*/
private corsOptionDelegate(req, callback) {
let whitelist: string[] = config('cors');
let corsOptions;
if (whitelist.indexOf(req.header("Origin")) !== -1) {
corsOptions = { origin: true };
} else {
corsOptions = { origin: false };
}
callback(null, corsOptions);
}
/**
* InvalidToken Exception
*/
private unauthorizedException(err, req, res, next) {
if (err.name === "UnauthorizedError") {
res
.status(401)
.header('Token-Expired', true)
.send({
success: false,
error: 'Invalid Token'
});
}
}
/**
* Start Listening
*/
public listen(): Promise<void> {
return new Promise((resolve, reject) => {
this.app.listen(this.port, () => {
console.log('HTTP Server Running');
resolve();
});
});
}
/**
* Seed Database
*/
public seedDB(): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
const seed = new DatabaseSeed();
seed.seed().then(result => resolve);
});
}
}