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 = {};
|
|
|
|
|
2020-08-14 11:20:19 +00:00
|
|
|
var cfg = require(global['__dirname']+'/bin/config');
|
|
|
|
|
2019-06-18 22:44:35 +00:00
|
|
|
/**
|
|
|
|
* start web server
|
|
|
|
* @author Ruben Meyer
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
methods.start = () => {
|
|
|
|
// init express framework
|
|
|
|
let express = require('express');
|
|
|
|
let session_handler = require('express-session');
|
2020-08-17 11:57:10 +00:00
|
|
|
let session_store = require('connect-mongo')(session_handler);
|
2019-06-18 22:44:35 +00:00
|
|
|
|
|
|
|
// 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({
|
2020-08-14 11:20:19 +00:00
|
|
|
'X-Powered-By': 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) => {
|
|
|
|
|
|
|
|
let dir = global['__dirname'] + '/res/web';
|
|
|
|
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
|
|
|
|
|
2020-08-31 07:35:18 +00:00
|
|
|
fs.exists(joined_path, (exists) => {
|
|
|
|
if(exists) {
|
|
|
|
let contentType = mime.contentType(path.extname(joined_path));
|
|
|
|
res.setHeader('Content-Type', contentType);
|
2019-06-18 22:44:35 +00:00
|
|
|
|
2020-08-31 07:35:18 +00:00
|
|
|
fs.createReadStream(joined_path).pipe(res);
|
|
|
|
} else {
|
|
|
|
res.status(404).end();
|
|
|
|
global['logs'].info("[web] (404) path not found: "+joined_path);
|
|
|
|
}
|
|
|
|
});
|
2019-06-18 22:44:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// BodyParser & CookieParser
|
|
|
|
app.use(bp.json());
|
|
|
|
app.use(bp.urlencoded({
|
|
|
|
extended: true
|
|
|
|
}));
|
2020-08-14 11:20:19 +00:00
|
|
|
app.use(cp(cfg.web.cookieKey));
|
2019-06-18 22:44:35 +00:00
|
|
|
|
2019-09-25 18:22:25 +00:00
|
|
|
// Pretty print
|
|
|
|
if(app.get('env') === 'debug')
|
|
|
|
app.locals.pretty = true;
|
|
|
|
|
2020-08-14 11:20:19 +00:00
|
|
|
(async function() {
|
2020-08-17 11:57:10 +00:00
|
|
|
// mongooseConnection
|
|
|
|
let db = global['requireModule']('database');
|
|
|
|
await db.connect();
|
|
|
|
let con = db.getConnection();
|
|
|
|
|
|
|
|
// Sessions
|
|
|
|
session_options = {
|
|
|
|
secret: cfg.web.sessionKey,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
cookie: {
|
|
|
|
maxAge: cfg.web.cookieMaxAge
|
|
|
|
},
|
|
|
|
store: new session_store({
|
|
|
|
mongooseConnection: con,
|
|
|
|
dbName: cfg.mongoose.db,
|
|
|
|
ttl: cfg.web.cookieMaxAge,
|
|
|
|
secret: (app.get('env') === 'production') ? true : false
|
|
|
|
})
|
|
|
|
};
|
|
|
|
if(app.get('env') === 'production') {
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
session_options.cookie.secure = true;
|
|
|
|
}
|
|
|
|
app.use(session_handler(session_options));
|
|
|
|
|
|
|
|
// web routes
|
2020-08-14 11:20:19 +00:00
|
|
|
let mRoutes = require(global['__dirname']+'/bin/web/routes/static');
|
|
|
|
let mainRoutes = await mRoutes.getRoutes();
|
|
|
|
app.use('/', mainRoutes);
|
|
|
|
let rAPI = require(global['__dirname']+'/bin/web/routes/api');
|
|
|
|
let restAPI = await rAPI.getRoutes();
|
|
|
|
app.use('/api', restAPI);
|
|
|
|
|
|
|
|
// start server
|
2020-08-17 11:57:10 +00:00
|
|
|
if(app.get('env') === 'production' && cfg.web.host && typeof cfg.web.host == "string")
|
|
|
|
app.listen(cfg.web.port, cfg.web.host, () => {
|
|
|
|
global['logs'].log("Server is listening on port: "+cfg.web.port);
|
|
|
|
});
|
|
|
|
else if(app.get('env') === 'debug' || !cfg.web.host || typeof cfg.web.host !== "string")
|
|
|
|
app.listen(cfg.web.port, () => {
|
|
|
|
global['logs'].log("Server is listening on port: "+cfg.web.port);
|
|
|
|
});
|
2020-08-14 11:20:19 +00:00
|
|
|
})();
|
2019-06-18 22:44:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = methods;
|