core.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Request = require('request');
  37. global.Tracer = require('tracer').dailyfile({root:Core.Path.Log,format : "{{timestamp}} <{{title}}> {{file}}:{{line}} {{message}}", dateformat : "HH:MM:ss.L"});
  38. //System Library load
  39. let CoreLibFiles = FILE.readdirSync(Core.Path.CoreLib);
  40. CoreLibFiles.forEach(function(filename){
  41. let nameWithOutMimeType = (filename.split('.')[0]).toUpperCase();
  42. try {
  43. global[nameWithOutMimeType] = require(Core.Path.CoreLib + '/' + filename);
  44. if(typeof global[nameWithOutMimeType] == 'object' && global[nameWithOutMimeType]['__construct']) global[nameWithOutMimeType]['__construct']();
  45. }catch(e){
  46. console.log('[Core] Core library file ['+filename+'] load error!');
  47. Tracer.error('[Core] Core library file ['+filename+'] load error!');
  48. }
  49. });
  50. CoreLibFiles = null;
  51. //System Library load end
  52. //Core Setting,just change it if necessary!
  53. global.Core.Setting = {};
  54. //If Mysql on,load Mysql Extension
  55. if(Config && Config.Database.Mysql.on){
  56. let MysqlPool = require(Core.Path.ExtraLib + '/mysql-pool.js').instance(Config.Database.Mysql);
  57. let testMysqlCon = MysqlPool.getConnection(function(err,connection){
  58. connection.release();
  59. if(!err){
  60. connection.query('SELECT VERSION() as version',function(err,result,fields){
  61. if(err){
  62. LOGGER.error('Mysql Connect error,please recheck your config');
  63. LOGGER.error(err);
  64. }else{
  65. LOGGER.info('Mysql Connect success');
  66. LOGGER.info('Mysql Version: ' + result[0]['version'] + ' | User: ' + Config.Database.Mysql.user + ' | Database: ' + Config.Database.Mysql.database);
  67. global.MysqlPool = MysqlPool;
  68. global.MysqlDB = require(Core.Path.Helper + '/mysqldb.js');
  69. }
  70. });
  71. }
  72. });
  73. }
  74. //If Mongodb on,load Mongodb Extension
  75. if(Config && Config.Database.Mongodb.on && Config.Database.Mongodb.database){
  76. let verify = Config.Database.Mongodb.user?Config.Database.Mongodb.user+':'+Config.Database.Mongodb.password+'@':'';
  77. let mongoConnect = 'mongodb://' + verify + Config.Database.Mongodb.host+':'+Config.Database.Mongodb.port+'/'+Config.Database.Mongodb.database;
  78. require('mongodb').MongoClient.connect(mongoConnect,function(err,db){
  79. if(err) {
  80. LOGGER.error('MongoDB connect error!',true);
  81. LOGGER.error('Server start failed. Log has been saved!');
  82. // Logger.out(err);
  83. return;
  84. }
  85. LOGGER.info('Mongodb Connect success');
  86. global.MongoDB = {db:db};
  87. global.MG = function(collection){
  88. if(!collection) return null;
  89. return MongoDB.db.collection(Config.Database.Mongodb.prefix+collection);
  90. };
  91. });
  92. }
  93. //If Memcache on,load Memcache Extension
  94. if(Config && Config.Database.Memcache.on){
  95. let Memclass = require('memcached');
  96. let Memcache = new Memclass(Config.Database.Memcache.host+':'+Config.Database.Memcache.port);
  97. Memcache.version(function(err,data){
  98. if(err){
  99. LOGGER.error('Memcache Connect error,please recheck your config');
  100. LOGGER.error(err);
  101. }else{
  102. LOGGER.info('Memcache Connect success');
  103. LOGGER.info('Memcache Version: ' + data[0]['version']);
  104. global.Memcache = Memcache;
  105. }
  106. });
  107. }
  108. //Check File Paths
  109. for(let path in global.Core.Path){
  110. try{
  111. FILE.statSync(global.Core.Path[path]);
  112. }catch(e){
  113. FILE.mkdirsSync(global.Core.Path[path]);
  114. }
  115. }