@@ -25,7 +25,6 @@ serverDm.run(function () {
25
25
jsGen . module . then = require ( 'thenjs' ) ;
26
26
jsGen . module . marked = require ( 'marked' ) ;
27
27
jsGen . module . rrestjs = require ( 'rrestjs' ) ;
28
- jsGen . module . mongoskin = require ( 'mongoskin' ) ;
29
28
jsGen . module . nodemailer = require ( 'nodemailer' ) ;
30
29
jsGen . serverlog = jsGen . module . rrestjs . restlog ;
31
30
jsGen . conf = jsGen . module . rrestjs . config ;
@@ -59,24 +58,55 @@ serverDm.run(function () {
59
58
resJson = jsGen . lib . tools . resJson ,
60
59
TimeLimitCache = jsGen . lib . redis . TimeLimitCache ;
61
60
62
- redis . connect ( ) . then ( function ( defer ) {
63
- redis . initConfig ( jsGen . lib . json . GlobalConfig , defer ) ; // 初始化config缓存
61
+ function exit ( ) {
62
+ redis . close ( ) ;
63
+ jsGen . dao . db . close ( ) ;
64
+ jsGen . serverlog . error ( error ) ;
65
+ return process . exit ( 1 ) ;
66
+ }
67
+
68
+ // 带'install'参数启动则初始化MongoDB,完成后退出
69
+ if ( process . argv . indexOf ( 'install' ) > 0 ) {
70
+ require ( './lib/install.js' ) ( ) . then ( function ( ) {
71
+ console . log ( 'jsGen installed!' ) ;
72
+ return exit ( ) ;
73
+ } ) . fail ( jsGen . thenErrLog ) ;
74
+ return ;
75
+ }
76
+
77
+ then . parallel ( [ function ( defer ) {
78
+ jsGen . dao . index . getGlobalConfig ( defer ) ;
79
+ } , function ( defer ) {
80
+ redis . connect ( ) . all ( defer ) ;
81
+ } ] ) . then ( function ( defer , result ) {
82
+ // 初始化config缓存
83
+ redis . initConfig ( result [ 0 ] , defer ) ;
84
+ } , function ( defer , error ) {
85
+ // redis 或者 mongodb 链接错误则退出
86
+ jsGen . serverlog . error ( error ) ;
87
+ return exit ( ) ;
64
88
} ) . then ( function ( defer , config ) {
65
89
jsGen . config = config ;
66
- if ( process . argv . indexOf ( 'install' ) > 0 ) { // 带'install'参数启动则初始化MongoDB
67
- require ( './api/install.js' ) ( ) . all ( defer ) ;
68
- } else { // Redis config缓存未赋值,则从MongoDB取值
69
- jsGen . dao . index . getGlobalConfig ( defer ) ;
70
- }
71
- } ) . then ( function ( defer , config ) {
72
- each ( jsGen . config , function ( value , key , list ) {
73
- if ( key in config ) {
74
- list [ key ] = config [ key ] ; // 写入config缓存
90
+ redis . userCache . index . total ( function ( error , users ) {
91
+ if ( ! users || process . argv . indexOf ( 'recache' ) > 0 ) {
92
+ // user缓存为空,则判断redis缓存为空,需要初始化
93
+ // 启动时指定了recache
94
+ var recache = require ( './lib/recache.js' ) ;
95
+ recache ( ) . then ( function ( defer2 , result ) {
96
+ console . log ( 'Redis cache rebuild success:' , result ) ;
97
+ defer ( ) ;
98
+ } , function ( defer2 , error ) {
99
+ // redis 缓存重建失败则退出
100
+ jsGen . serverlog . error ( error ) ;
101
+ return exit ( ) ;
102
+ } ) ;
103
+ } else {
104
+ defer ( ) ;
75
105
}
76
- } ) ;
77
- defer ( null , jsGen . config ) ;
78
- } ) . then ( function ( defer , config ) {
79
- var api = [ 'index' , 'user' , 'article' , 'tag' , 'collection' , 'message' , 'rebuild' ] ;
106
+ } ) ; // 读取user缓存
107
+ } ) . then ( function ( defer ) {
108
+ var api = [ 'index' , 'user' , 'article' , 'tag' , 'collection' , 'message' ] ,
109
+ config = jsGen . config ;
80
110
81
111
jsGen . cache = { } ;
82
112
jsGen . cache . tag = new CacheLRU ( config . tagCache ) ;
@@ -102,78 +132,63 @@ serverDm.run(function () {
102
132
data . nodejs = process . versions . node ;
103
133
data . rrestjs = _restConfig . _version ;
104
134
jsGen . config . info = data ;
105
- redis . userCache . index . total ( defer ) ; // 读取user缓存
106
- } ) . then ( function ( defer , users ) {
107
- var rebuild = jsGen . api . rebuild ;
108
- if ( ! users ) { // user缓存为空,则判断redis缓存为空,需要初始化
109
- // 初始化redis缓存
110
- then ( function ( defer2 ) {
111
- rebuild . user ( ) . all ( defer2 ) ;
112
- } ) . then ( function ( defer2 ) {
113
- rebuild . tag ( ) . all ( defer2 ) ;
114
- } ) . then ( function ( defer2 ) {
115
- rebuild . article ( ) . all ( defer ) ;
116
- } ) . fail ( defer ) ;
117
- } else {
118
- defer ( ) ;
119
- }
120
- } ) . then ( function ( defer ) {
121
- http . createServer ( function ( req , res ) {
122
- var dm = domain . create ( ) ;
123
135
124
- function errHandler ( err , res , dm ) {
125
- delete err . domain ;
136
+ function errHandler ( err , res , dm ) {
137
+ delete err . domain ;
126
138
127
- try {
128
- res . on ( 'finish' , function ( ) {
129
- //jsGen.dao.db.close();
130
- process . nextTick ( function ( ) {
131
- dm . dispose ( ) ;
132
- } ) ;
139
+ try {
140
+ res . on ( 'finish' , function ( ) {
141
+ //jsGen.dao.db.close();
142
+ process . nextTick ( function ( ) {
143
+ dm . dispose ( ) ;
133
144
} ) ;
134
- if ( err . hasOwnProperty ( 'name' ) ) {
135
- res . sendjson ( resJson ( err ) ) ;
136
- } else {
137
- jsGen . serverlog . error ( err ) ;
138
- res . sendjson ( resJson ( jsGen . Err ( jsGen . lib . msg . MAIN . requestDataErr ) ) ) ;
139
- }
140
- } catch ( error ) {
141
- delete error . domain ;
142
- jsGen . serverlog . error ( error ) ;
143
- dm . dispose ( ) ;
145
+ } ) ;
146
+ if ( err . hasOwnProperty ( 'name' ) ) {
147
+ res . sendjson ( resJson ( err ) ) ;
148
+ } else {
149
+ jsGen . serverlog . error ( err ) ;
150
+ res . sendjson ( resJson ( jsGen . Err ( jsGen . lib . msg . MAIN . requestDataErr ) ) ) ;
144
151
}
152
+ } catch ( error ) {
153
+ delete error . domain ;
154
+ jsGen . serverlog . error ( error ) ;
155
+ dm . dispose ( ) ;
145
156
}
157
+ }
146
158
147
- function router ( req , res ) {
148
- if ( req . path [ 0 ] === 'api' && jsGen . api [ req . path [ 1 ] ] ) {
149
- jsGen . api [ req . path [ 1 ] ] [ req . method . toUpperCase ( ) ] ( req , res ) ; // 处理api请求
150
- } else if ( req . path [ 0 ] . toLowerCase ( ) === 'sitemap.xml' ) {
151
- jsGen . api . article . sitemap ( req , res ) ; // 响应搜索引擎sitemap,动态生成
152
- } else if ( req . path [ 0 ] . slice ( - 3 ) . toLowerCase ( ) === 'txt' ) {
153
- // 直接响应static目录的txt文件,如robots.txt
159
+ function router ( req , res ) {
160
+ if ( req . path [ 0 ] === 'api' && jsGen . api [ req . path [ 1 ] ] ) {
161
+ jsGen . api [ req . path [ 1 ] ] [ req . method . toUpperCase ( ) ] ( req , res ) ; // 处理api请求
162
+ } else if ( req . path [ 0 ] . toLowerCase ( ) === 'sitemap.xml' ) {
163
+ jsGen . api . article . sitemap ( req , res ) ; // 响应搜索引擎sitemap,动态生成
164
+ } else if ( req . path [ 0 ] . slice ( - 3 ) . toLowerCase ( ) === 'txt' ) {
165
+ // 直接响应static目录的txt文件,如robots.txt
166
+ then ( function ( defer ) {
167
+ fs . readFile ( processPath + jsGen . conf . staticFolder + req . path [ 0 ] , 'utf8' , defer ) ;
168
+ } ) . then ( function ( defer , txt ) {
169
+ res . setHeader ( 'Content-Type' , 'text/plain' ) ;
170
+ res . send ( txt ) ;
171
+ } ) . fail ( res . throwError ) ;
172
+ } else if ( jsGen . robotReg . test ( req . useragent ) ) {
173
+ jsGen . api . article . robot ( req , res ) ; // 处理搜索引擎请求
174
+ } else {
175
+ jsGen . config . visitors = 1 ; // 访问次数+1
176
+ res . setHeader ( 'Content-Type' , 'text/html' ) ;
177
+ if ( jsGen . cache . indexTpl ) {
178
+ res . send ( jsGen . cache . indexTpl ) ; // 响应首页index.html
179
+ } else {
154
180
then ( function ( defer ) {
155
- fs . readFile ( processPath + jsGen . conf . staticFolder + req . path [ 0 ] , 'utf8' , defer ) ;
156
- } ) . then ( function ( defer , txt ) {
157
- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
158
- res . send ( txt ) ;
181
+ fs . readFile ( processPath + jsGen . conf . staticFolder + '/index.html' , 'utf8' , defer ) ;
182
+ } ) . then ( function ( defer , tpl ) {
183
+ jsGen . cache . indexTpl = tpl ;
184
+ res . send ( jsGen . cache . indexTpl ) ;
159
185
} ) . fail ( res . throwError ) ;
160
- } else if ( jsGen . robotReg . test ( req . useragent ) ) {
161
- jsGen . api . article . robot ( req , res ) ; // 处理搜索引擎请求
162
- } else {
163
- jsGen . config . visitors = 1 ; // 访问次数+1
164
- res . setHeader ( 'Content-Type' , 'text/html' ) ;
165
- if ( jsGen . cache . indexTpl ) {
166
- res . send ( jsGen . cache . indexTpl ) ; // 响应首页index.html
167
- } else {
168
- then ( function ( defer ) {
169
- fs . readFile ( processPath + jsGen . conf . staticFolder + '/index.html' , 'utf8' , defer ) ;
170
- } ) . then ( function ( defer , tpl ) {
171
- jsGen . cache . indexTpl = tpl ;
172
- res . send ( jsGen . cache . indexTpl ) ;
173
- } ) . fail ( res . throwError ) ;
174
- }
175
186
}
176
187
}
188
+ }
189
+
190
+ http . createServer ( function ( req , res ) {
191
+ var dm = domain . create ( ) ;
177
192
178
193
res . throwError = function ( defer , err ) { // 处理then.js捕捉的错误
179
194
if ( ! util . isError ( err ) ) {
0 commit comments