2019-09-08 17:43:52 +00:00
|
|
|
// init
|
2020-10-01 20:13:09 +00:00
|
|
|
const methods = {};
|
|
|
|
|
|
|
|
const cfg = require(global['__dirname']+'/bin/config');
|
2019-09-08 17:43:52 +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');
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
|
|
|
|
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-10-01 20:13:09 +00:00
|
|
|
'X-Powered-By': cfg.web.poweredBy
|
2019-09-08 17:43:52 +00:00
|
|
|
});
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// BodyParser & CookieParser
|
|
|
|
app.use(bp.json());
|
|
|
|
app.use(bp.urlencoded({
|
|
|
|
extended: true
|
|
|
|
}));
|
2020-10-01 20:13:09 +00:00
|
|
|
app.use(cp(cfg.web.cookieKey));
|
2019-09-08 17:43:52 +00:00
|
|
|
|
|
|
|
// Pretty print
|
|
|
|
app.locals.pretty = true;
|
|
|
|
|
|
|
|
// Sessions
|
|
|
|
session_options = {
|
2020-10-01 20:13:09 +00:00
|
|
|
secret: cfg.web.sessionKey,
|
2019-09-08 17:43:52 +00:00
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false, cookie: {}};
|
|
|
|
if(app.get('env') === 'production') {
|
|
|
|
session_options.cookie.secure = true;
|
|
|
|
}
|
|
|
|
app.use(session_handler(session_options));
|
|
|
|
|
|
|
|
//static files
|
|
|
|
app.use('/res', (req, res, next) => {
|
|
|
|
let dir = global['__dirname'] + '/res/web';
|
|
|
|
|
|
|
|
|
|
|
|
if(!req.session || !req.session.user) dir += '/auth';
|
|
|
|
else dir += '/app';
|
|
|
|
|
|
|
|
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
|
2020-10-01 20:13:09 +00:00
|
|
|
fs.exists(joined_path, (exists) => {
|
|
|
|
if(exists) {
|
|
|
|
let contentType = mime.contentType(path.extname(joined_path));
|
|
|
|
res.setHeader('Content-Type', contentType);
|
2019-09-08 17:43:52 +00:00
|
|
|
|
2020-10-01 20:13:09 +00:00
|
|
|
fs.createReadStream(joined_path).pipe(res);
|
|
|
|
} else {
|
|
|
|
res.status(404).end();
|
|
|
|
}
|
|
|
|
});
|
2019-09-08 17:43:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// web routes
|
|
|
|
app.use('/auth', require(global['__dirname']+'/bin/web/auth/routes/main'));
|
|
|
|
app.use('/', require(global['__dirname']+'/bin/web/app/routes/main'));
|
|
|
|
|
|
|
|
// start server
|
2020-10-01 20:13:09 +00:00
|
|
|
app.listen(cfg.web.port, () => {
|
|
|
|
global['logs'].log("Server is listening on port: "+cfg.web.port);
|
2019-09-08 17:43:52 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = methods;
|