core.js 4.7 KB

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