This repository has been archived by the owner on Jan 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
authweb.js
75 lines (62 loc) · 2.24 KB
/
authweb.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
/*
* Copyright (C) 2016-Present The MoonLake ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
/** properties */
var config = require('./config');
var AuthError = require('./util/AuthError');
var Logger = require('./util/Logger');
var MySQL = require('./util/MySQL');
app.set('config', config);
app.set('mysql', MySQL);
/** MySQL Test */
MySQL.initialize()
.then(function () {
Logger.info('The MySQL is successfully connected.');
})
.catch(function (err) {
if(err) {
Logger.error('Unable to connect to the MySQL.', err);
process.exit(1);
}
});
/** Base */
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(Logger.log4js.connectLogger(Logger.authweb, { level: 'auto', format: ':remote-addr :method :url :status :response-timems' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));
/** Routes */
app.use('/', require('./routes/index'));
app.use('/sessionserver', require('./routes/sessionserver/index'));
app.use('/authserver', require('./routes/authserver/index'));
/** Error Handler */
app.use(function(err, req, res, next) {
if(err instanceof AuthError) {
if(err.status === undefined)
err.status = 500;
AuthError.response(res, err);
} else {
var error = new AuthError(err.status || 500, err.message);
error.status = err.status;
AuthError.response(res, err);
}
});
module.exports = app;