core.js 4.8 KB

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