/** * @Author HonorLee (dev@honorlee.me) * @Version 1.0 (2018-05-04) * @License MIT */ 'use strict'; global.Core = {}; global.CACHE = {}; //Global Path global.Core.Path = { System : ROOTPATH + '/system', CoreLib : ROOTPATH + '/system/lib/core', ExtraLib : ROOTPATH + '/system/lib/extra', Helper : ROOTPATH + '/system/lib/helper', Work : ROOTPATH + '/app', Module : ROOTPATH + '/app/modules', Handler : ROOTPATH + '/app/handlers', View : ROOTPATH + '/app/view', Temp : ROOTPATH + '/temp', Session : ROOTPATH + '/temp/session', Log : ROOTPATH + '/log', Asset : ROOTPATH + '/' + Config.Asset.asset_path, Upload : ROOTPATH + '/' + Config.Asset.upload_path }; //Global Extension Require global.serverUID = (Math.ceil(Math.random()*61439+4096)).toString(16).toUpperCase(); global.Moment = require('moment-range').extendMoment(require('moment')); global.URL = require('url'); global.Querystring = require('querystring'); global.FILE = require('fs-extra'); global.EJS = require('ejs'); global.ASYNC = require('async'); global.Base64 = require('js-base64'); global.MD5 = require('md5'); global.Formidable = require('formidable'); global.MIME = require('mime-types'); global.Path = require('path'); global.Tracer = require('tracer').dailyfile({root:Core.Path.Log,format : "{{timestamp}} <{{title}}> {{file}}:{{line}} {{message}}", dateformat : "HH:MM:ss.L"}); String.random = require('string-random'); Core.Request = require('request'); FILE.walkSync = require('klaw-sync'); //System Library load let CoreLibFiles = FILE.readdirSync(Core.Path.CoreLib); CoreLibFiles.forEach(function(filename){ let nameWithOutMimeType = (filename.split('.')[0]).toUpperCase(); let coreClass; try { coreClass = require(Core.Path.CoreLib + '/' + filename); }catch(e){ console.log('[Core] Core library file ['+filename+'] load error!'); console.log(e); Tracer.error('[Core] Core library file ['+filename+'] load error!'); } if(coreClass.hasOwnProperty('_name') && coreClass['_name']){ global[coreClass['_name']] = coreClass; }else{ global[nameWithOutMimeType] = coreClass; } if(typeof coreClass == 'object' && coreClass.hasOwnProperty('__construct')) coreClass['__construct'](); }); CoreLibFiles = null; //System Library load end //Core Setting,just change it if necessary! global.Core.Setting = {}; //If Mysql on,load Mysql Extension if(Config && Config.Database.Mysql.on){ let MysqlPool = require(Core.Path.ExtraLib + '/mysql-pool.js').instance(Config.Database.Mysql); let testMysqlCon = MysqlPool.getConnection(function(err,connection){ connection.release(); if(!err){ connection.query('SELECT VERSION() as version',function(err,result,fields){ if(err){ LOGGER.error('Mysql Connect error,please recheck your config'); LOGGER.error(err); }else{ LOGGER.info('Mysql Connect success'); LOGGER.info('Mysql Version: ' + result[0]['version'] + ' | User: ' + Config.Database.Mysql.user + ' | Database: ' + Config.Database.Mysql.database); global.MysqlPool = MysqlPool; global.MysqlDB = require(Core.Path.Helper + '/mysqldb.js'); } }); } }); } //If Mongodb on,load Mongodb Extension if(Config && Config.Database.Mongodb.on && Config.Database.Mongodb.database){ let verify = Config.Database.Mongodb.user?Config.Database.Mongodb.user+':'+Config.Database.Mongodb.password+'@':''; let mongoConnect = 'mongodb://' + verify + Config.Database.Mongodb.host+':'+Config.Database.Mongodb.port+'/'+Config.Database.Mongodb.database; require('mongodb').MongoClient.connect(mongoConnect,function(err,db){ if(err) { LOGGER.error('MongoDB connect error!',true); LOGGER.error('Server start failed. Log has been saved!'); // Logger.out(err); return; } LOGGER.info('Mongodb Connect success'); global.MongoDB = {db:db}; global.MG = function(collection){ if(!collection) return null; return MongoDB.db.collection(Config.Database.Mongodb.prefix+collection); }; }); } //If Memcache on,load Memcache Extension if(Config && Config.Database.Memcache.on){ let Memclass = require('memcached'); let Memcache = new Memclass(Config.Database.Memcache.host+':'+Config.Database.Memcache.port); Memcache.version(function(err,data){ if(err){ LOGGER.error('Memcache Connect error,please recheck your config'); LOGGER.error(err); }else{ LOGGER.info('Memcache Connect success'); LOGGER.info('Memcache Version: ' + data[0]['version']); global.Memcache = Memcache; } }); } //Check File Paths for(let path in global.Core.Path){ try{ FILE.statSync(global.Core.Path[path]); }catch(e){ FILE.mkdirsSync(global.Core.Path[path]); } } if(Config && Config.Wechat.on){ global.Wechat = require(Core.Path.ExtraLib + '/wechat/wechat.js'); try{ FILE.statSync(Core.Path.Handler + Config.Wechat.handler_path +'/index.js'); }catch(e){ FILE.copy(Core.Path.ExtraLib + '/wechat/handler/wechat.js',Core.Path.Handler + Config.Wechat.handler_path +'/index.js') } } let HandlerFiles = FILE.walkSync(Core.Path.Handler, {nodir: true}); CACHE.Handlers = []; for(let i in HandlerFiles){ let path = HandlerFiles[i]['path']; CACHE.Handlers.push(path.replace(Core.Path.Handler,'')); } CACHE.HandlersString = CACHE.Handlers.join('|');