diff --git a/app.js b/app.js index 736e66c..7ff6564 100644 --- a/app.js +++ b/app.js @@ -1,22 +1,15 @@ -// GDS: Global Data System -global['gds'] = { - debug: (process.env.NODE_ENV === 'debug') ? true : false, - db: null, - cache: {}, - cfg: require(__dirname+'/bin/config') -}; -global['modules'] = {}; +global['debug'] = (process.env.NODE_ENV === 'debug') ? true : false; global['__dirname'] = __dirname; /** * load modules */ -let load = (name) => { +const load = (name) => { return require(__dirname+'/bin/'+name+'/module'); }; // environment variable check -let env_vars = ["APP_ID", "APP_SECRET"]; +const env_vars = ["APP_ID", "APP_SECRET"]; let env_missing = false; env_vars.forEach((el) => { if(typeof process.env[el] == 'undefined') { @@ -26,14 +19,9 @@ env_vars.forEach((el) => { }); if(env_missing) process.exit(); -global['modules'].logs = load('logs'); // log handler -global['logs'] = global['modules'].logs; // alias - -global['modules'].web = load('web'); // web server -global['modules'].sso = load('sso'); // sso service - -// custom modules +global['logs'] = load('logs'); // log handler +const webServer = load('web'); // web server // start web server -global['modules'].web.start(); +webServer.start(); diff --git a/bin/web/auth/routes/static.js b/bin/web/auth/routes/static.js index 7144484..9d7c65c 100644 --- a/bin/web/auth/routes/static.js +++ b/bin/web/auth/routes/static.js @@ -1,6 +1,7 @@ var express = require('express'); var route = express.Router(); +const cfg = require(global['__dirname']+'/bin/config'); route.all('/', function(req, res, next) { // TODO: show login page or dashboard @@ -17,7 +18,9 @@ route.all('/*', (req, res, next) => { // TODO: role-based authorization // TODO: show login page or page - res.end('500 - LEL'); + res.render('auth/views/index', { + appName: cfg.app.name + }); }); module.exports = route; diff --git a/bin/web/auth/views/index.pug b/bin/web/auth/views/index.pug index 8146794..dbf611f 100644 --- a/bin/web/auth/views/index.pug +++ b/bin/web/auth/views/index.pug @@ -1,5 +1,5 @@ //- variables -- var appName = global['gds'].cfg.app.name || "SSObaseApp"; +- var appName = appName || "SSObaseApp"; - var title = "Dashboard"; mixin navItem(name, id, symbol, href) diff --git a/bin/web/module.js b/bin/web/module.js index 350f1f4..e0b4040 100644 --- a/bin/web/module.js +++ b/bin/web/module.js @@ -1,5 +1,7 @@ // init -var methods = {}; +const methods = {}; + +const cfg = require(global['__dirname']+'/bin/config'); /** * start web server @@ -27,7 +29,7 @@ methods.start = () => { // Access Control Headers app.use( (req, res, next) => { res.set({ - 'X-Powered-By': global['gds'].cfg + 'X-Powered-By': cfg.web.poweredBy }); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); @@ -39,14 +41,14 @@ methods.start = () => { app.use(bp.urlencoded({ extended: true })); - app.use(cp(global['gds'].cfg.web.cookieKey)); + app.use(cp(cfg.web.cookieKey)); // Pretty print app.locals.pretty = true; // Sessions session_options = { - secret: global['gds'].cfg.web.sessionKey, + secret: cfg.web.sessionKey, resave: false, saveUninitialized: false, cookie: {}}; if(app.get('env') === 'production') { @@ -56,8 +58,6 @@ methods.start = () => { //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'; @@ -65,29 +65,16 @@ methods.start = () => { else dir += '/app'; let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]); + fs.exists(joined_path, (exists) => { + if(exists) { + let contentType = mime.contentType(path.extname(joined_path)); + res.setHeader('Content-Type', contentType); - // 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(); - } - }); - } + fs.createReadStream(joined_path).pipe(res); + } else { + res.status(404).end(); + } + }); }); // web routes @@ -95,44 +82,9 @@ methods.start = () => { app.use('/', require(global['__dirname']+'/bin/web/app/routes/main')); // start server - app.listen(global['gds'].cfg.web.port, () => { - global['modules'].logs.log("Server is listening on port: "+global['gds'].cfg.web.port); + app.listen(cfg.web.port, () => { + global['logs'].log("Server is listening on port: "+cfg.web.port); }); - - // DEBUG OUTPUT: list all routes with HTTP method - setTimeout(function () { - function print (path, layer) { - if (layer.route) { - layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path)))) - } else if (layer.name === 'router' && layer.handle.stack) { - layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp)))) - } else if (layer.method) { - console.log('%s /%s', - layer.method.toUpperCase(), - path.concat(split(layer.regexp)).filter(Boolean).join('/') - ); - } - } - - function split (thing) { - if (typeof thing === 'string') { - return thing.split('/') - } else if (thing.fast_slash) { - return '' - } else { - var match = thing.toString() - .replace('\\/?', '') - .replace('(?=\\/|$)', '$') - .match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//) - - return match - ? match[1].replace(/\\(.)/g, '$1').split('/') - : '' - } - } - - app._router.stack.forEach(print.bind(null, [])) - }, 1500); }; module.exports = methods;