-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.js
198 lines (180 loc) · 4.24 KB
/
kernel.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var express = require('express');
global.app = express();
global.appRoot = require('app-root-path');
global.util = require('./lib/util');
global.ROUTER = express.Router();
var bodyParser = require('body-parser');
var path = require('path');
var validator = require('express-validator');
var mysql = require('mysql');
require('dotenv').config();
/**
* The kernel of the app
*/
class Kernel {
/**
* Constructor
*
* @return void
*/
constructor() {
this.setUpServer();
}
/**
* Initial Setup of the server
*
* @return void
*/
setUpServer() {
this.setUpBodyParser(); // Body Parser
this.setUpRouter(); // Router
this.setUpStaticPath(); // Static Path (Public)
this.setUpTemplateEngine(); // Template Engine (Handlebars)
this.setUpMysql(); // Mysql
}
/**
* Call this function to serve the app
*
* @return void
*/
serve() {
var port = this.getServerPort();
app.listen(port, function() {
console.log(`Server started on http://localhost:${port}`);
});
}
/**
* Set up Mysql and make a global variable the instance of connection
*
* @return void
*/
setUpMysql() {
var connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
global.db = connection;
}
/**
* Set up two different kind of routers
*
* @return void
*/
setUpRouter() {
if (this.getRouter() == 'json') {
this.setUpJsonRouter();
} else {
require(appRoot + '/router/routes.js');
}
}
/**
* Set Static Path
*
* @return void
*/
setUpStaticPath() {
app.use(express.static(appRoot + '/public'));
}
/**
* Set Template Engine (handlebars)
*
* @return void
*/
setUpTemplateEngine() {
app.set('view engine', 'hbs');
app.set('views', (appRoot + '/views'));
}
/**
* In case of json Router (Setup)
*
* @return void
*/
setUpJsonRouter() {
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync(appRoot + '/router/routes.json', 'utf8'));
obj.routes.forEach(function(el) {
if (el.method == 'GET') {
var controller = require(appRoot + '/controllers/' + el.controller.filename);
app.get(el.path, controller[el.controller.function]);
}
});
}
/**
* Body Parser
*
* @return void
*/
setUpBodyParser() {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
}
/**
* Establish the connection with mysql
*
* @return void
*/
mysqlConnect() {
connection.connect(function(err) {
if (err) throw err;
});
}
/**
* Setup Validator
*
* @return void
*/
validator() {
app.use(validator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while (namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
}));
}
/**
* GETTER: Mysql connection instance
*
* @return {connection}
*/
getMysqlConnection() {
return connection;
}
/**
* GETTER: The expressJs instance (server)
*
* @return {express}
*/
getServer() {
return app;
}
/**
* GETTER: Router preference of the user
*
* @return {string}
*/
getRouter() {
return process.env.SERVER_ROUTER;
}
/**
* GETTER: Port specified by user
*
* @return {string}
*/
getServerPort() {
return process.env.SERVER_PORT || 8000;
}
}
module.exports = Kernel;