core.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. global.Core = {};
  3. //Global Path
  4. global.Core.Path = {
  5. System : ROOTPATH + '/system',
  6. CoreLib : ROOTPATH + '/system/lib/core',
  7. ExtraLib : ROOTPATH + '/system/lib/extra',
  8. Helper : ROOTPATH + '/system/lib/helper',
  9. Work : ROOTPATH + '/app',
  10. Module : ROOTPATH + '/app/modules',
  11. Handler : ROOTPATH + '/app/handlers',
  12. View : ROOTPATH + '/app/view',
  13. Temp : ROOTPATH + '/temp',
  14. Session : ROOTPATH + '/temp/session',
  15. Log : ROOTPATH + '/log',
  16. Asset : ROOTPATH + '/' + Config.asset_path
  17. };
  18. //Global Extension Require
  19. global.Commander = require('commander');
  20. global.URL = require('url');
  21. global.FILE = require('fs-extra');
  22. global.EJS = require('ejs');
  23. global.ASYNC = require('async');
  24. global.Base64 = require('js-base64');
  25. global.MD5 = require('md5');
  26. global.DateFormat = require('date-format');
  27. global.Formidable = require('formidable');
  28. global.MIME = require('mime-types');
  29. global.Path = require('path');
  30. global.Request = require('request');
  31. global.Tracer = require('tracer').dailyfile({root:Core.Path.Log,format : "{{timestamp}} <{{title}}> {{file}}:{{line}} {{message}}", dateformat : "HH:MM:ss.L"});
  32. //System Library load
  33. let CoreLibFiles = FILE.readdirSync(Core.Path.CoreLib);
  34. CoreLibFiles.forEach(function(filename){
  35. let nameWithOutMimeType = (filename.split('.')[0]).toUpperCase();
  36. try {
  37. global[nameWithOutMimeType] = require(Core.Path.CoreLib + '/' + filename);
  38. }catch(e){
  39. console.log('[Core] Core library file ['+filename+'] load error!');
  40. Tracer.error('[Core] Core library file ['+filename+'] load error!');
  41. }
  42. });
  43. //System Library load end
  44. //Core Setting,just change it if necessary!
  45. global.Core.Setting = {
  46. //Mysql connect pool setting
  47. mysql_pool:{
  48. name: 'NMVCPOOL',
  49. maxconn: 5
  50. }
  51. };
  52. //If Mysql on,load Mysql Extension
  53. if(Config && Config.mysql_on && Config.mysql_cfg){
  54. let mysqlConfig = {
  55. pool:Core.Setting.mysql_pool,
  56. db:Config.mysql_cfg
  57. };
  58. let MysqlPool = require(Core.Path.ExtraLib + '/mysql-pool.js').instance(mysqlConfig);
  59. // global.MysqlPool = require(Core.Path.ExtraLib + '/mysql-pool.js').instance(mysqlConfig);
  60. // global.MysqlDB = require(Core.Path.Helper + '/mysqldb.js');
  61. MysqlPool.getConnection(Core.Setting.mysql_pool.name).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.mysql_cfg.user + ' | Database: ' + Config.mysql_cfg.database);
  68. global.MysqlPool = MysqlPool;
  69. global.MysqlDB = require(Core.Path.Helper + '/mysqldb.js');
  70. }
  71. });
  72. }
  73. //If Mongodb on,load Mongodb Extension
  74. if(Config && Config.mongodb_on && Config.mongodb_cfg && Config.mongodb_cfg.database){
  75. let verify = Config.mongodb_cfg.user?Config.mongodb_cfg.user+':'+Config.mongodb_cfg.password+'@':'';
  76. let mongoConnect = 'mongodb://' + verify + Config.mongodb_cfg.host+':'+Config.mongodb_cfg.port+'/'+Config.mongodb_cfg.database;
  77. require('mongodb').MongoClient.connect(mongoConnect,function(err,db){
  78. if(err) {
  79. Logger.error('MongoDB connect error!',true);
  80. Logger.error('Server start failed. Log has been saved!');
  81. // Logger.out(err);
  82. return;
  83. }
  84. LOGGER.info('Mongodb Connect success');
  85. global.MongoDB = {db:db};
  86. });
  87. }
  88. //Check File Paths
  89. for(let path in global.Core.Path){
  90. try{
  91. FILE.statSync(global.Core.Path[path]);
  92. }catch(e){
  93. FILE.mkdirsSync(global.Core.Path[path]);
  94. }
  95. }