-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (71 loc) · 2.21 KB
/
index.js
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
import express from 'express';
import http from 'http';
import cors from 'cors';
import bodyParser from 'body-parser';
import compression from 'compression';
import dotenv from 'dotenv';
import morgan from 'morgan';
import Routes from './Controllers/index';
import cronJob from './jobs/index';
import Databases from '../db/index';
class App {
constructor() {
this.expressInstance = express();
this.envs = dotenv.config().parsed;
this.expressInstance.use(cors());
this.expressInstance.use(compression());
this.expressInstance.use(bodyParser.urlencoded({ extended: true }));
this.expressInstance.use(bodyParser.json());
this.expressInstance.disable('x-powered-by');
this.expressInstance.use('/static/', express.static('public'));
this.expressInstance.use(morgan('tiny'));
if (this.envs.DEVELOPMENT === 'on') {
this.expressInstance.get('/welcome', (req, res) => {
res.send('Welcome!');
});
}
this.expressInstance = Routes(this.expressInstance);
this.httpServer = http.createServer(this.expressInstance);
}
async start() {
await Databases(this);
const newServer = new Promise((resolve, reject) => {
this.httpServer
.listen(this.envs.PORT)
.on('listening', () => resolve(this.httpServer))
.on('error', (err) => reject(err));
}).then((server) => {
let address;
if (server.address().family === 'IPv6') {
if (server.address().address === '::') {
address = 'localhost';
} else {
address = `[${server.address().address}]`;
}
} else {
address = server.address().address;
}
return {
host: address,
address: server.address(),
port: server.address().port,
server,
};
});
cronJob(this);
return Promise.all([newServer]).then((res) => res[0]);
}
stop() {
this.httpServer.close();
return true;
}
}
new App().start()
.then((server) => {
console.info(`Inicializado: http://${server.host}:${server.port}/`);
}).catch((err) => console.error(err));
process.on('exit', () => console.info('Process exited'));
process.on('SIGINT', () => {
console.info('(Ctrl-C) fechando processo');
process.exit(1);
});