-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (53 loc) · 1.98 KB
/
server.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
class Server {
constructor(config) {
this.express = require('express');
this.server = this.express();
this.config = config.webserver;
this.ttl = config.gotController.cacheTTL/1000;
this.EOL = require('os').EOL;
this.Got = new (require('./controllers/got-controller'))(config.gotController);
this.server.set('views', __dirname + '/routes');
this.server.set('view engine', 'jsx');
this.server.engine('jsx', require('express-react-views').createEngine());
this.server.use('/public',this.express.static('public'));
this.server.use(this.headers.bind(this));
this.setupRoutes();
this.server.listen(this.config.port, this.serverStartMessage.bind(this));
}
headers(req,res,next) {
res.set('Cache-Control', 's-maxage=' + this.ttl);
next();
}
setupRoutes() {
this.server.get('/', async (req,res) => {
let characters = await this.Got.getCharacterStatuses(),
scores = await this.Got.getScores(),
stats = await this.Got.getStatistics();
res.render('GET/scoreboard', {
characters,
scores,
stats,
title: 'Game of thrones S08, Deathpool'
});
});
this.server.get('*', (req, res) => {
res.render('GET/404');
});
}
createRouteEngine(method, route) {
return (req, res) => {
require(`./routes/${method}/${route}`);
res.render();
}
}
// todo: mode stdout logic to seprate logger class
serverStartMessage() {
let messageText = `webserver is up and running on port ${this.config.port}`;
let consoleWidth = process.stdout.columns;
let barrier = '='.repeat(consoleWidth)+this.EOL;
process.stdout.write(barrier);
process.stdout.write(messageText+this.EOL);
process.stdout.write(barrier);
}
}
module.exports = Server;