2019-06-18 22:44:35 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the authRXBN single sign-on package.
|
|
|
|
*
|
|
|
|
* (c) Ruben Meyer <contact@rxbn.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// init
|
|
|
|
var methods = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start web server
|
|
|
|
* @author Ruben Meyer
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
methods.start = () => {
|
|
|
|
// init express framework
|
|
|
|
let express = require('express');
|
|
|
|
let session_handler = require('express-session');
|
|
|
|
|
|
|
|
// utilities
|
|
|
|
let fs = require('fs');
|
|
|
|
let path = require('path');
|
|
|
|
let mime = require('mime-types');
|
|
|
|
|
|
|
|
// app variable
|
|
|
|
let app = express();
|
|
|
|
app.set('view engine', 'pug'); // page engine
|
|
|
|
app.set('views', global['__dirname']+'/bin/web/views');
|
|
|
|
|
|
|
|
let bp = require('body-parser'); // POST Body parser
|
|
|
|
let cp = require('cookie-parser'); // Cookie handler
|
|
|
|
|
|
|
|
// Access Control Headers
|
|
|
|
app.use( (req, res, next) => {
|
|
|
|
res.set({
|
2019-09-25 18:22:25 +00:00
|
|
|
'X-Powered-By': global['gds'].cfg.web.poweredBy
|
2019-06-18 22:44:35 +00:00
|
|
|
});
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//static files
|
|
|
|
app.use('/res', (req, res, next) => {
|
|
|
|
if(typeof global['gds'].cache.web == 'undefined') global['gds'].cache.web = {};
|
|
|
|
|
|
|
|
let dir = global['__dirname'] + '/res/web';
|
|
|
|
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
|
|
|
|
|
|
|
|
// path already cached; not exist
|
|
|
|
if(global['gds'].cache.web[joined_path] == false) {
|
|
|
|
res.status(404).end();
|
|
|
|
// path already cached; exist
|
|
|
|
} else if(global['gds'].cache.web[joined_path] == true){
|
|
|
|
let contentType = mime.contentType(path.extname(joined_path));
|
|
|
|
res.setHeader('Content-Type', contentType);
|
|
|
|
fs.createReadStream(joined_path).pipe(res);
|
|
|
|
// check path
|
|
|
|
} else {
|
|
|
|
fs.exists(joined_path, (exists) => {
|
|
|
|
global['gds'].cache.web[joined_path] = exists;
|
|
|
|
if(exists) {
|
|
|
|
let contentType = mime.contentType(path.extname(joined_path));
|
|
|
|
res.setHeader('Content-Type', contentType);
|
|
|
|
|
|
|
|
fs.createReadStream(joined_path).pipe(res);
|
|
|
|
} else {
|
|
|
|
res.status(404).end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// BodyParser & CookieParser
|
|
|
|
app.use(bp.json());
|
|
|
|
app.use(bp.urlencoded({
|
|
|
|
extended: true
|
|
|
|
}));
|
|
|
|
app.use(cp(global['gds'].cfg.web.cookieKey));
|
|
|
|
|
2019-09-25 18:22:25 +00:00
|
|
|
// Pretty print
|
|
|
|
if(app.get('env') === 'debug')
|
|
|
|
app.locals.pretty = true;
|
|
|
|
|
2019-06-18 22:44:35 +00:00
|
|
|
// Sessions
|
|
|
|
session_options = {
|
|
|
|
secret: global['gds'].cfg.web.sessionKey,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false, cookie: {}};
|
|
|
|
if(app.get('env') === 'production') {
|
|
|
|
session_options.cookie.secure = true;
|
|
|
|
}
|
|
|
|
app.use(session_handler(session_options));
|
|
|
|
|
|
|
|
// web routes
|
|
|
|
app.use('/', require(global['__dirname']+'/bin/web/routes/static'));
|
|
|
|
app.use('/api', require(global['__dirname']+'/bin/web/routes/api'));
|
|
|
|
|
|
|
|
// start server
|
|
|
|
app.listen(global['gds'].cfg.web.port, () => {
|
2019-06-23 21:18:46 +00:00
|
|
|
global['modules'].logs.log("Server is listening on port: "+global['gds'].cfg.web.port);
|
2019-06-18 22:44:35 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = methods;
|