UNPKG

ernest

Version:

Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis

474 lines (414 loc) 9.81 kB
"use strict"; var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var session = require('express-session'); var cookieParser = require('cookie-parser'); var ErnestDB = require('./ErnestDB'); var fs = require('fs'); var redis = require('socket.io-redis'); const Ernest_Permission = require('./Ernest_Permission'); const Ernest_Session = require('./Ernest_Session'); const Ernest_Users = require('./Ernest_Users'); const Ernest_CLI = require('./Ernest_CLI'); const Ernest_ASR = require('./Ernest_AutoServerResponse'); const Templater = require('./Templater'); const Ernest_CSI = require('./Ernest_CSI'); const EventEmitter = require('events'); class EEmitter extends EventEmitter {} class Ernest { constructor(db_name,iport,jsonlimt_mb,session_name,session_secret,logpath) { process.stdout.write('\x1Bc'); if(iport !== undefined) { this.PORT = iport; }else { this.PORT = 80; }; if(db_name !== undefined) { if((db_name != null)&&( db_name != "")) { this.dbc = new ErnestDB(db_name); this.db_name = db_name; } else { this.dbc = null; this.db_name = "Ernest is not setup with a DB"; } }else { this.dbc = null; this.db_name = "Ernest is not setup with a DB"; }; if(jsonlimt_mb !== undefined ) { this.json_limit = {limit:jsonlimt_mb+'mb',type: ['json', 'application/csp-report']}; this.json_urlencoded = {limit:jsonlimt_mb+'mb', extended: false, parameterLimit:(jsonlimt_mb*1000)}; } else { this.json_limit = {limit:'90mb',type: ['json', 'application/csp-report']}; this.json_urlencoded = {limit:'90mb', extended: false, parameterLimit:(90000)}; }; if(session_name !== undefined) { this.session_name = session_name; } else { this.session_name = (((new Date()) * 1) + ""); }; if(session_secret !== undefined) { this.session_secret = session_secret; } else { this.session_secret = (((new Date()) * 1) + ""); }; this.app = app; this.Users = null; this.Permission = new Ernest_Permission(app,this.dbc); this.Session = null; this.CLI = new Ernest_CLI(this,logpath); this.public = null; this.emitter = new EEmitter(); this.status = "offline"; this.deniedtext = "Access Denied"; this.easr = new Ernest_ASR(this.app,this.dbc,this.messenger_io,this); this.templaters = []; this.CSI = new Ernest_CSI(this,logpath,"ernestvserver","05102017"); }; setAdmin(adminDB) { if(this.dbc != null) { this.dbc.setAdmin(adminDB); }; return this; }; setDB(db_name) { if(db_name !== undefined) { if((db_name != null)&&( db_name != "")) { this.dbc = new ErnestDB(db_name); this.db_name = db_name; } else { this.dbc = null; this.db_name = "Ernest is not setup with a DB"; } }else { this.dbc = null; this.db_name = "Ernest is not setup with a DB"; }; }; listen(let_otherorigins,key,cert,socketio_redis_port, isocketio_redis_server) { var _this = this; var io = null; var https = null; var http = null; var socketio_redis_server = 'localhost'; if(isocketio_redis_server !== null) { socketio_redis_server = isocketio_redis_server; }; if(key !== undefined) { if(key != null) { var privatekey = fs.readFileSync(key,'utf8'); var certificate = fs.readFileSync(cert,'utf8'); var ssl_credentials = {'key': privatekey,'cert': certificate }; https = require('https').Server(ssl_credentials,app); if(socketio_redis_port !== undefined ) { io = require('socket.io')(https); io.adapter(redis( { host: socketio_redis_server, port:socketio_redis_port })); }; https.listen(this.PORT, function() { _this.status = '\x1b[32mErnest is Running on HTTPS - Port: ' + _this.PORT + "\x1b[0m"; console.log(_this.status); _this.emitter.emit('server_running'); }).on('error',function(err) { if(err.code == 'EADDRINUSE') { console.log('Port'+ err.port + ' in use. Service not Running'); } }); } else { http = require('http').Server(app); if(socketio_redis_port !== undefined ) { io = require('socket.io')(http); io.adapter(redis( { host:socketio_redis_server, port:socketio_redis_port })); }; http.listen(this.PORT, function() { _this.status = '\x1b[32mErnest is Running on HTTP - Port: ' + _this.PORT + "\x1b[0m"; console.log(_this.status); _this.emitter.emit('server_running'); }).on('error',function(err) { if(err.code == 'EADDRINUSE') { console.log('Port'+ err.port + ' in use. Service not Running'); } }); }; } else { http = require('http').Server(app); if(socketio_redis_port !== undefined ) { io = require('socket.io')(http); io.adapter(redis( { host:socketio_redis_server, port:socketio_redis_port })); }; http.listen(this.PORT, function() { _this.status = '\x1b[32mErnest is Running on HTTP - Port: ' + _this.PORT + "\x1b[0m"; console.log(_this.status); _this.emitter.emit('server_running'); }).on('error',function(err) { if(err.code == 'EADDRINUSE') { console.log('Port'+ err.port + ' in use. Service not Running'); } }); }; if(socketio_redis_port !== undefined ) { _this.messenger_io = require('socket.io-emitter')({host:socketio_redis_server, port:socketio_redis_port}); }; app.use(bodyParser.json(_this.json_limit)); app.use(bodyParser.urlencoded(_this.json_urlencoded)); app.use(cookieParser()); if(let_otherorigins !== undefined) { if(let_otherorigins) { app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); res.setHeader('Access-Control-Allow-Credentials', true); next(); }); }; }; return this; }; Use(mod) { if(mod !== undefined) { app.use(mod); }; return this; }; Set(mod) { if(mod !== undefined) { app.set(mod); }; return this; }; Apply(command,mod) { if(command !== undefined) { if(mod !== undefined) { app[command](mod); }; }; return this; }; Sesion(features) { var _this = this; var sessionSettings = {name:_this.session_name, secret:_this.session_secret, resave: false, saveUninitialized: true }; if(features !== undefined) { if(typeof(features) == 'object') { for(var prop in features) { sessionSettings[prop] = features[prop]; }; }; }; app.use(session(sessionSettings)); return this; }; DefaultURLRedirect(defaultUrl) { app.get('/', function (req,res,next) { if(defaultUrl !== undefined) { res.redirect(defaultUrl); } else { res.redirect("/index.html"); }; }); return this; }; EnableCLI(eventActivator) { var _this = this; this.emitter.on(eventActivator, () => { _this.CLI.MainMenu(); _this.CSI.Start(); }); return this; }; EnableASR(comm_url) { this.easr.Enable(comm_url); return this; }; RewriteASRC(native,custom) { this.easr.RewriteCommand(native,custom); return this; }; LoadModules(callback) { var _this = this; if(callback !== undefined) { callback(_this.app,_this.dbc,_this.messenger_io,this); } return _this; }; NoLoginError(text) { this.deniedtext = text; return this; }; Permission_Session(iaccess,ilogin_url,ilogin_html,iboard_html,show_log) { this.Users = new Ernest_Users(this.dbc,iaccess); this.Session = new Ernest_Session(this.app,this.Users); let login_url = (ilogin_url === undefined) ? "/login":ilogin_url; this.Permission.SetPermissionCollection(iaccess,login_url,this,show_log); this.Session.InitSessionUrl(login_url); let login_html = (ilogin_html === undefined) ? "/login.html":ilogin_html; let board_html = (iboard_html === undefined) ? "/board.html":iboard_html; this.Session.OnLoginRedirect(login_html,board_html); return this; }; SetTemplater(viewsFolder,templatesFolder,extension,urlContains,production,callback) { var _this = this; var templater = new Templater(viewsFolder,templatesFolder,extension,_this.app); templater.Start(function(e,r) { e ? callback(e,r):(()=> { templater.Apply(urlContains,production,(e,d)=> { _this.templaters.push(templater); callback(e,d); }); })(); }); return this; }; ReloadTemplaters(callback) { var result = true; var error = null; var _this = this; var len = _this.templaters.length; if(len > 0) { var till = len - 1; _this.templaters.map(function(item,index) { item.Reload(function(e,r) { if(e) { error = e; result = r; }; if(index == till) { callback(error,result); }; }); }); } else { callback(error,result); }; }; Static(ipublic) { var _this = this; if(ipublic !== undefined) { if(ipublic === null) { this.public = __dirname + '/public'; } else { if(ipublic === false) { this.public = null; } else { this.public = ipublic ; }; }; } else { this.public = __dirname + '/public'; }; _this.app.use(express.static(_this.public)); return _this; }; TextOnError(text) { this.deniedtext = text; this.app.use(function(req,res,next) {res.send(text); }); return this; }; }; module.exports = Ernest;