core.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @Author HonorLee (deve@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. 'use strict';
  7. global.Core = {};
  8. global.CACHE = {};
  9. //Global Path
  10. global.Core.Path = {
  11. System : ROOTPATH + '/system',
  12. CoreLib : ROOTPATH + '/system/lib/core',
  13. ExtraLib : ROOTPATH + '/system/lib/extra',
  14. Helper : ROOTPATH + '/system/lib/helper',
  15. Work : ROOTPATH + '/app',
  16. Module : ROOTPATH + '/app/modules',
  17. Handler : ROOTPATH + '/app/handlers',
  18. View : ROOTPATH + '/app/view',
  19. Temp : ROOTPATH + '/temp',
  20. Session : ROOTPATH + '/temp/session',
  21. Log : ROOTPATH + '/log',
  22. Asset : ROOTPATH + '/' + Config.Asset.asset_path,
  23. Upload : ROOTPATH + '/' + Config.Asset.upload_path
  24. };
  25. //Global Extension Require
  26. global.Moment = require('moment-range').extendMoment(require('moment'));
  27. global.URL = require('url');
  28. global.FILE = require('fs-extra');
  29. global.EJS = require('ejs');
  30. global.ASYNC = require('async');
  31. global.Base64 = require('js-base64');
  32. global.MD5 = require('md5');
  33. global.Formidable = require('formidable');
  34. global.MIME = require('mime-types');
  35. global.Path = require('path');
  36. global.Tracer = require('tracer').dailyfile({root:Core.Path.Log,format : "{{timestamp}} <{{title}}> {{file}}:{{line}} {{message}}", dateformat : "HH:MM:ss.L"});
  37. String.random = require('string-random');
  38. Core.Request = require('request');
  39. FILE.walkSync = require('klaw-sync');
  40. //System Library load
  41. let CoreLibFiles = FILE.readdirSync(Core.Path.CoreLib);
  42. CoreLibFiles.forEach(function(filename){
  43. let nameWithOutMimeType = (filename.split('.')[0]).toUpperCase();
  44. let coreClass;
  45. try {
  46. coreClass = require(Core.Path.CoreLib + '/' + filename);
  47. }catch(e){
  48. console.log('[Core] Core library file ['+filename+'] load error!');
  49. console.log(e);
  50. Tracer.error('[Core] Core library file ['+filename+'] load error!');
  51. }
  52. if(coreClass.hasOwnProperty('_name') && coreClass['_name']){
  53. global[coreClass['_name']] = coreClass;
  54. }else{
  55. global[nameWithOutMimeType] = coreClass;
  56. }
  57. if(typeof coreClass == 'object' && coreClass.hasOwnProperty('__construct')) coreClass['__construct']();
  58. });
  59. CoreLibFiles = null;
  60. //System Library load end
  61. //Core Setting,just change it if necessary!
  62. global.Core.Setting = {};
  63. //If Mysql on,load Mysql Extension
  64. if(Config && Config.Database.Mysql.on){
  65. let MysqlPool = require(Core.Path.ExtraLib + '/mysql-pool.js').instance(Config.Database.Mysql);
  66. let testMysqlCon = MysqlPool.getConnection(function(err,connection){
  67. connection.release();
  68. if(!err){
  69. connection.query('SELECT VERSION() as version',function(err,result,fields){
  70. if(err){
  71. LOGGER.error('Mysql Connect error,please recheck your config');
  72. LOGGER.error(err);
  73. }else{
  74. LOGGER.info('Mysql Connect success');
  75. LOGGER.info('Mysql Version: ' + result[0]['version'] + ' | User: ' + Config.Database.Mysql.user + ' | Database: ' + Config.Database.Mysql.database);
  76. global.MysqlPool = MysqlPool;
  77. global.MysqlDB = require(Core.Path.Helper + '/mysqldb.js');
  78. }
  79. });
  80. }
  81. });
  82. }
  83. //If Mongodb on,load Mongodb Extension
  84. if(Config && Config.Database.Mongodb.on && Config.Database.Mongodb.database){
  85. let verify = Config.Database.Mongodb.user?Config.Database.Mongodb.user+':'+Config.Database.Mongodb.password+'@':'';
  86. let mongoConnect = 'mongodb://' + verify + Config.Database.Mongodb.host+':'+Config.Database.Mongodb.port+'/'+Config.Database.Mongodb.database;
  87. require('mongodb').MongoClient.connect(mongoConnect,function(err,db){
  88. if(err) {
  89. LOGGER.error('MongoDB connect error!',true);
  90. LOGGER.error('Server start failed. Log has been saved!');
  91. // Logger.out(err);
  92. return;
  93. }
  94. LOGGER.info('Mongodb Connect success');
  95. global.MongoDB = {db:db};
  96. global.MG = function(collection){
  97. if(!collection) return null;
  98. return MongoDB.db.collection(Config.Database.Mongodb.prefix+collection);
  99. };
  100. });
  101. }
  102. //If Memcache on,load Memcache Extension
  103. if(Config && Config.Database.Memcache.on){
  104. let Memclass = require('memcached');
  105. let Memcache = new Memclass(Config.Database.Memcache.host+':'+Config.Database.Memcache.port);
  106. Memcache.version(function(err,data){
  107. if(err){
  108. LOGGER.error('Memcache Connect error,please recheck your config');
  109. LOGGER.error(err);
  110. }else{
  111. LOGGER.info('Memcache Connect success');
  112. LOGGER.info('Memcache Version: ' + data[0]['version']);
  113. global.Memcache = Memcache;
  114. }
  115. });
  116. }
  117. //Check File Paths
  118. for(let path in global.Core.Path){
  119. try{
  120. FILE.statSync(global.Core.Path[path]);
  121. }catch(e){
  122. FILE.mkdirsSync(global.Core.Path[path]);
  123. }
  124. }
  125. if(Config && Config.Wechat.on){
  126. global.Wechat = require(Core.Path.ExtraLib + '/wechat/wechat.js');
  127. try{
  128. FILE.statSync(Core.Path.Handler + Config.Wechat.handler_path +'/index.js');
  129. }catch(e){
  130. FILE.copy(Core.Path.ExtraLib + '/wechat/handler/wechat.js',Core.Path.Handler + Config.Wechat.handler_path +'/index.js')
  131. }
  132. }
  133. let HandlerFiles = FILE.walkSync(Core.Path.Handler, {nodir: true});
  134. CACHE.Handlers = [];
  135. for(let i in HandlerFiles){
  136. let path = HandlerFiles[i]['path'];
  137. CACHE.Handlers.push(path.replace(Core.Path.Handler,''));
  138. }
  139. CACHE.HandlersString = CACHE.Handlers.join('|');