-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (122 loc) · 3.71 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
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
/*
* Lighweight Web API publishing overwatch-js npm package functionnalities
*/
var express = require('express');
var http = require('http');
var owjs = require('overwatch-js');
var winston = require('winston');
var config = require('./config/default.json');
var package = require('./package.json');
var app = express();
var morgan = require('morgan');
let handle = (err, res) => {
switch (err.message) {
case 'PROFILE_NOT_FOUND':
res.sendStatus(404)
break;
default:
res.sendStatus(500)
break;
}
};
var search = (req, res) => {
var username = req.params.username;
owjs.search(username)
.then((data) => {
if (!data || data.length == 0)
return res.sendStatus(404);
res.json(data);
})
.catch((err) => handle(err, res));
}
var all = (req, res) => {
var tag = req.params.tag;
var platform = req.params.platform;
var region = req.params.region;
owjs.getAll(platform, region, tag)
.then((data) => {
if (!data || data.length == 0)
return res.sendStatus(404);
res.json(data);
})
.catch((err) => handle(err, res));
}
var overall = (req, res) => {
var tag = req.params.tag;
var platform = req.params.platform;
var region = req.params.region;
owjs.getAll(platform, region, tag)
.then((data) => {
if (!data || data.length == 0)
return res.sendStatus(404);
res.json(data);
})
.catch((err) =>
handle(err, res)
);
}
/* Logger */
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: 'main.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: true
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function (message, encoding) {
logger.info(message);
}
};
/* Configuration */
app.set('trust proxy', '127.0.0.1');
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(morgan('combined', { "stream": logger.stream }));
/* Configuration : SSL */
var privateKey = config.ssl.enabled ? fs.readFileSync(config.ssl.key, 'utf8') : '';
var certificate = config.ssl.enabled ? fs.readFileSync(config.ssl.cert, 'utf8') : '';
var credentials = { key: privateKey, cert: certificate };
/* Configuration : Routing */
var v1 = express.Router();
app.get('/', function (req, res) {
res.json({
name : package.name,
version : package.version,
description : package.description,
author : package.author,
repo : package.repository.url,
report : package.bugs.email,
help : "For all commands available, PLEASE CHECK THE README FROM GITHUB REPOSITORY"
});
});
v1.use('/search', express.Router()
.get('/:username', search));
v1.use('/all', express.Router()
.get('/:platform/:region/:tag', all));
v1.use('/overall', express.Router()
.get('/:platform/:region/:tag', overall));
/* Master switch */
app.use('/v1', v1);
app.use('/', v1); // Set the default version to latest.
// Setup server.
var port = process.env.PORT || 3000;
(config.ssl.enabled ? http.createServer(credentials, app) : http.createServer(app)).listen(port, function () {
logger.log('info', `Starting web server on port : ${port}.`);
});