remove global variables
This commit is contained in:
parent
e1299d3857
commit
f1666311c8
24
app.js
24
app.js
@ -1,22 +1,15 @@
|
|||||||
// GDS: Global Data System
|
global['debug'] = (process.env.NODE_ENV === 'debug') ? true : false;
|
||||||
global['gds'] = {
|
|
||||||
debug: (process.env.NODE_ENV === 'debug') ? true : false,
|
|
||||||
db: null,
|
|
||||||
cache: {},
|
|
||||||
cfg: require(__dirname+'/bin/config')
|
|
||||||
};
|
|
||||||
global['modules'] = {};
|
|
||||||
global['__dirname'] = __dirname;
|
global['__dirname'] = __dirname;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* load modules
|
* load modules
|
||||||
*/
|
*/
|
||||||
let load = (name) => {
|
const load = (name) => {
|
||||||
return require(__dirname+'/bin/'+name+'/module');
|
return require(__dirname+'/bin/'+name+'/module');
|
||||||
};
|
};
|
||||||
|
|
||||||
// environment variable check
|
// environment variable check
|
||||||
let env_vars = ["APP_ID", "APP_SECRET"];
|
const env_vars = ["APP_ID", "APP_SECRET"];
|
||||||
let env_missing = false;
|
let env_missing = false;
|
||||||
env_vars.forEach((el) => {
|
env_vars.forEach((el) => {
|
||||||
if(typeof process.env[el] == 'undefined') {
|
if(typeof process.env[el] == 'undefined') {
|
||||||
@ -26,14 +19,9 @@ env_vars.forEach((el) => {
|
|||||||
});
|
});
|
||||||
if(env_missing) process.exit();
|
if(env_missing) process.exit();
|
||||||
|
|
||||||
global['modules'].logs = load('logs'); // log handler
|
global['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
|
|
||||||
|
|
||||||
|
const webServer = load('web'); // web server
|
||||||
|
|
||||||
// start web server
|
// start web server
|
||||||
global['modules'].web.start();
|
webServer.start();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
var express = require('express');
|
var express = require('express');
|
||||||
var route = express.Router();
|
var route = express.Router();
|
||||||
|
|
||||||
|
const cfg = require(global['__dirname']+'/bin/config');
|
||||||
|
|
||||||
route.all('/', function(req, res, next) {
|
route.all('/', function(req, res, next) {
|
||||||
// TODO: show login page or dashboard
|
// TODO: show login page or dashboard
|
||||||
@ -17,7 +18,9 @@ route.all('/*', (req, res, next) => {
|
|||||||
// TODO: role-based authorization
|
// TODO: role-based authorization
|
||||||
// TODO: show login page or page
|
// TODO: show login page or page
|
||||||
|
|
||||||
res.end('500 - LEL');
|
res.render('auth/views/index', {
|
||||||
|
appName: cfg.app.name
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = route;
|
module.exports = route;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//- variables
|
//- variables
|
||||||
- var appName = global['gds'].cfg.app.name || "SSObaseApp";
|
- var appName = appName || "SSObaseApp";
|
||||||
- var title = "Dashboard";
|
- var title = "Dashboard";
|
||||||
|
|
||||||
mixin navItem(name, id, symbol, href)
|
mixin navItem(name, id, symbol, href)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
// init
|
// init
|
||||||
var methods = {};
|
const methods = {};
|
||||||
|
|
||||||
|
const cfg = require(global['__dirname']+'/bin/config');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start web server
|
* start web server
|
||||||
@ -27,7 +29,7 @@ methods.start = () => {
|
|||||||
// Access Control Headers
|
// Access Control Headers
|
||||||
app.use( (req, res, next) => {
|
app.use( (req, res, next) => {
|
||||||
res.set({
|
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-Origin", "*");
|
||||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||||
@ -39,14 +41,14 @@ methods.start = () => {
|
|||||||
app.use(bp.urlencoded({
|
app.use(bp.urlencoded({
|
||||||
extended: true
|
extended: true
|
||||||
}));
|
}));
|
||||||
app.use(cp(global['gds'].cfg.web.cookieKey));
|
app.use(cp(cfg.web.cookieKey));
|
||||||
|
|
||||||
// Pretty print
|
// Pretty print
|
||||||
app.locals.pretty = true;
|
app.locals.pretty = true;
|
||||||
|
|
||||||
// Sessions
|
// Sessions
|
||||||
session_options = {
|
session_options = {
|
||||||
secret: global['gds'].cfg.web.sessionKey,
|
secret: cfg.web.sessionKey,
|
||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: false, cookie: {}};
|
saveUninitialized: false, cookie: {}};
|
||||||
if(app.get('env') === 'production') {
|
if(app.get('env') === 'production') {
|
||||||
@ -56,8 +58,6 @@ methods.start = () => {
|
|||||||
|
|
||||||
//static files
|
//static files
|
||||||
app.use('/res', (req, res, next) => {
|
app.use('/res', (req, res, next) => {
|
||||||
if(typeof global['gds'].cache.web == 'undefined') global['gds'].cache.web = {};
|
|
||||||
|
|
||||||
let dir = global['__dirname'] + '/res/web';
|
let dir = global['__dirname'] + '/res/web';
|
||||||
|
|
||||||
|
|
||||||
@ -65,19 +65,7 @@ methods.start = () => {
|
|||||||
else dir += '/app';
|
else dir += '/app';
|
||||||
|
|
||||||
let joined_path = path.join(dir, /^[^?]+/.exec(req.url)[0]);
|
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) => {
|
fs.exists(joined_path, (exists) => {
|
||||||
global['gds'].cache.web[joined_path] = exists;
|
|
||||||
if(exists) {
|
if(exists) {
|
||||||
let contentType = mime.contentType(path.extname(joined_path));
|
let contentType = mime.contentType(path.extname(joined_path));
|
||||||
res.setHeader('Content-Type', contentType);
|
res.setHeader('Content-Type', contentType);
|
||||||
@ -87,7 +75,6 @@ methods.start = () => {
|
|||||||
res.status(404).end();
|
res.status(404).end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// web routes
|
// web routes
|
||||||
@ -95,44 +82,9 @@ methods.start = () => {
|
|||||||
app.use('/', require(global['__dirname']+'/bin/web/app/routes/main'));
|
app.use('/', require(global['__dirname']+'/bin/web/app/routes/main'));
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
app.listen(global['gds'].cfg.web.port, () => {
|
app.listen(cfg.web.port, () => {
|
||||||
global['modules'].logs.log("Server is listening on port: "+global['gds'].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('/')
|
|
||||||
: '<complex:' + thing.toString() + '>'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app._router.stack.forEach(print.bind(null, []))
|
|
||||||
}, 1500);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = methods;
|
module.exports = methods;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user