UNPKG

3.37 kBJavaScriptView Raw
1var Definer = require('./definer')
2var dabaseDefiner = require('./resourcesDefiner/databaseDefiner')
3var Emitter = require('events').EventEmitter;
4var path = require('path')
5module.exports = Application;
6var async = require('async')
7var fs = require('fs');
8
9var validate = require('./resourcesDefiner/validate')
10
11
12
13
14Application.prototype.__proto__ = Emitter.prototype;
15function Application(){
16 var self;
17 this.path = path.join(path.dirname(require.main.filename))
18 this.modules = {}; /*模块对象集合*/
19 this.name = {};/*Application 名称*/
20 this.server = {};/* 服务器的对象*/
21 this.define = new Definer(this); /*加载定义器*/
22 this.middlewares = new Array(); /*中间件集合*/
23 this.dbs = {}; /*数据库连接集合*/
24 this.models = {}; /*公共模型*/
25
26 this.modelValidate = validate()/*模型验证器*/
27
28
29}
30
31Application.prototype.optionsHandler = function(options){
32 this.options = options;
33 this.name = options.name;
34 this.defaultModule = options.defaultModule;
35
36}
37
38Application.prototype.use = function(callback){
39 this.middlewares.push(callback)
40}
41
42
43
44
45
46
47
48Application.prototype.moduleRequire = function(){
49 for(var moduleName in this.modules){
50 this.modules[moduleName].requireControllers();
51 console.info(' successfully loaded ' + moduleName + ' module');
52 }
53}
54
55/*Application 资源加载器*/
56Application.prototype.load = function(){
57 this.moduleRequire();
58
59 this.addListener('databaseConnected',function(){
60 this.modelLoader();
61
62 })
63 this.addListener('databaseConnectError',function(){
64
65 })
66 this.databaseConnect(); /*连接数据库*/
67 this.routeModuleAllActions()
68
69}
70
71Application.prototype.routeModuleAllActions = function(){
72 var modules = this.modules;
73 for(var modulelName in modules){
74 var module = modules[modulelName]
75 for(var controllerName in module.controllers){
76 var controller = module.controllers[controllerName]
77 controller.routeAction();
78 }
79 }
80}
81
82
83Application.prototype.databaseConnect = function(){
84 var self = this;
85 var dbOptions = this.options.databases
86 var dbConnectInfo = new Array();
87 for(var i in dbOptions){
88 dbConnectInfo.push({name:i,url:dbOptions[i]})
89 }
90 /*顺序加载数据库*/
91 async.map(dbConnectInfo,dabaseDefiner,function(err,dbs){
92 /*如果数据库连接错误,就直接加载模块*/
93 if(err){
94 console.error(err);
95 self.emit('databaseConnectError');
96 return
97 }
98
99 /*数据库链接成功就加载 模型和模块资源*/
100 for(var i in dbs){
101 var db = dbs[i]
102 self.dbs[db.name] = db.connect;
103 }
104 self.emit('databaseConnected');
105 });
106
107
108}
109
110
111Application.prototype.modelLoader = function(){
112 var modelsPaths = this.options.models
113 for(var i in modelsPaths){
114 var modelsPath = path.join(this.path,modelsPaths[i]);
115 var filesName = fs.readdirSync(modelsPath);
116 for(var i in filesName){
117 require(path.join(modelsPath,filesName[i]))
118 }
119 }
120 /*下面两行代码是处理orm2 model的关系*/
121 var Relational = require(path.join(this.path,modelsPaths[0],'models_relational'))
122 Relational();
123
124 this.copyModelsFromDbs();
125}
126
127Application.prototype.copyModelsFromDbs = function(){
128 var dbs = this.dbs;
129 for(var dbName in dbs){
130 var db = dbs[dbName];
131 for(var modelName in db.models){
132 if(this.models[modelName]){
133 console.error(modelName + '模型名称重复,请检查');
134 throw new Error();
135 }
136 this.models[modelName] = db.models[modelName]
137 }
138 }
139}
140
141
142
143
144
145
146
147
148
149