1 | var Definer = require('./definer')
|
2 | var dabaseDefiner = require('./resourcesDefiner/databaseDefiner')
|
3 | var Emitter = require('events').EventEmitter;
|
4 | var path = require('path')
|
5 | module.exports = Application;
|
6 | var async = require('async')
|
7 | var fs = require('fs');
|
8 |
|
9 | var validate = require('./resourcesDefiner/validate')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Application.prototype.__proto__ = Emitter.prototype;
|
15 | function Application(){
|
16 | var self;
|
17 | this.path = path.join(path.dirname(require.main.filename))
|
18 | this.modules = {};
|
19 | this.name = {};
|
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 |
|
31 | Application.prototype.optionsHandler = function(options){
|
32 | this.options = options;
|
33 | this.name = options.name;
|
34 | this.defaultModule = options.defaultModule;
|
35 |
|
36 | }
|
37 |
|
38 | Application.prototype.use = function(callback){
|
39 | this.middlewares.push(callback)
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | Application.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 |
|
56 | Application.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 |
|
71 | Application.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 |
|
83 | Application.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 |
|
111 | Application.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 |
|
121 | var Relational = require(path.join(this.path,modelsPaths[0],'models_relational'))
|
122 | Relational();
|
123 |
|
124 | this.copyModelsFromDbs();
|
125 | }
|
126 |
|
127 | Application.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 |
|