-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (72 loc) · 1.64 KB
/
app.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
84
import path from 'path';
import express from 'express';
import * as bodyParser from 'body-parser';
import { startClient } from './database/connect';
import { LoggerService } from './services/logger.service';
import { ExampleModule } from './modules/example/example.module';
class AppFactory {
constructor () {
this.initDatabaseConnection();
this.app = express();
/** Set config & middlewares */
this.config();
/** Add API routes */
this.initializeModules();
this.getIndex();
this.app.use('*', (req, res) => {
res.status(404).json({
statusCode: 404,
error: 'Not found',
});
})
}
/**
* Initialize database connection
*
* @private
*/
initDatabaseConnection () {
startClient()
.then(() => {
LoggerService.log('Successfully connected to database', 'DatabaseConnection');
})
.catch(error => {
LoggerService.error(error.message, 'DatabaseConnection');
});
}
/**
* Initialize application modules
*
* @private
*/
initializeModules () {
this.addModule('examples', ExampleModule);
}
/**
* Inject Module
*
* @param {string} prefix
* @param {Router} Module
*
* @private
*/
addModule (prefix, Module) {
this.app.use(`${__API_PREFIX}/${prefix}`, Module);
}
/**
* Set app configuration
*
* @private
*/
config () {
this.app.use(bodyParser.json());
this.app.use(new LoggerService().logger);
}
getIndex () {
this.app.get(__API_PREFIX, (req, res) => {
const { name, version, author, description } = require(path.join(__BASEDIR, 'package.json'));
res.status(200).json({ name, version, author, description });
});
}
}
export const App = new AppFactory().app;