27
27
var express = require ( 'express' ) ;
28
28
var path = require ( 'path' ) ;
29
29
var http = require ( 'http' ) ;
30
+ var methodOverride = require ( 'method-override' ) ;
31
+ var errorhandler = require ( 'errorhandler' ) ;
30
32
31
33
var database = require ( "./util/database" ) ;
32
34
var util = require ( './util/dexter-util' ) ;
@@ -40,7 +42,9 @@ var codeMetrics = require("./routes/codeMetrics");
40
42
var functionMetrics = require ( "./routes/functionMetrics" ) ;
41
43
var monitor = require ( "./routes/monitor" ) ;
42
44
var adminSE = require ( "./routes/adminSE" ) ;
43
-
45
+ var methodOverride = require ( 'method-override' ) ;
46
+ var swaggerUi = require ( 'swagger-ui-express' ) ;
47
+ var swaggerDocument = require ( './api-docs.json' ) ;
44
48
var app = express ( ) ;
45
49
46
50
global . runOptions = {
@@ -85,6 +89,7 @@ function initialize(){
85
89
setExecutionMode ( ) ;
86
90
setAppConfigure ( ) ;
87
91
initModules ( ) ;
92
+ initSwagger ( ) ;
88
93
initRestAPI ( ) ;
89
94
startServer ( ) ;
90
95
}
@@ -93,13 +98,13 @@ function setRunOptionsByCliOptions(){
93
98
var cliOptions = util . getCliOptions ( ) ;
94
99
95
100
global . runOptions . port = cliOptions . getValue ( 'p' , 4982 ) ;
96
- global . runOptions . databaseHost = cliOptions . getValue ( 'database.host' , 'localhost ' ) ;
101
+ global . runOptions . databaseHost = cliOptions . getValue ( 'database.host' , 'dexter-test ' ) ;
97
102
global . runOptions . databasePort = cliOptions . getValue ( 'database.port' , 3306 ) ;
98
- global . runOptions . databaseUser = cliOptions . getValue ( 'database.user' , '' ) ;
99
- global . runOptions . databasePassword = cliOptions . getValue ( 'database.password' , '' ) ;
103
+ global . runOptions . databaseUser = cliOptions . getValue ( 'database.user' , 'root ' ) ;
104
+ global . runOptions . databasePassword = cliOptions . getValue ( 'database.password' , 'gre4d ' ) ;
100
105
global . runOptions . databaseAdminUser = cliOptions . getValue ( 'database.admin.user' , '' ) ;
101
106
global . runOptions . databaseAdminPassword = cliOptions . getValue ( 'database.admin.password' , '' ) ;
102
- global . runOptions . databaseName = cliOptions . getValue ( 'database.name' , '' ) ;
107
+ global . runOptions . databaseName = cliOptions . getValue ( 'database.name' , 'my_dexter_db ' ) ;
103
108
global . runOptions . serverName = cliOptions . getValue ( 'server.name' , 'dexter-server-default' ) ;
104
109
global . runOptions . serverIP = util . getLocalIPAddress ( ) ;
105
110
}
@@ -111,24 +116,22 @@ function setExecutionMode(){
111
116
}
112
117
113
118
function setAppConfigure ( ) {
114
- app . configure ( function ( ) {
115
- app . set ( "jsonp callback" , true ) ;
116
- app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
117
- app . set ( 'view engine' , 'jade' ) ;
118
- app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
119
- app . use ( express . json ( { limit :'300mb' } ) ) ;
120
- app . use ( express . urlencoded ( ) ) ;
121
- app . use ( express . methodOverride ( ) ) ;
122
- } ) ;
123
-
124
- app . configure ( 'development' , function ( ) {
125
- app . use ( express . errorHandler ( { dumpExceptions : true , showStack : true } ) ) ;
126
- } ) ;
127
-
128
-
129
- app . configure ( 'production' , function ( ) {
130
- app . use ( express . errorHandler ( { "dumpExceptions" : false , "showStack" : false } ) ) ;
131
- } ) ;
119
+
120
+ app . set ( "jsonp callback" , true ) ;
121
+ app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
122
+ app . set ( 'view engine' , 'jade' ) ;
123
+ app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
124
+ app . use ( express . json ( { limit :'300mb' } ) ) ;
125
+ app . use ( express . urlencoded ( ) ) ;
126
+ app . use ( methodOverride ( ) ) ;
127
+
128
+ if ( process . env . NODE_ENV === 'development' ) {
129
+ app . use ( errorhandler ( { dumpExceptions : true , showStack : true } ) ) ;
130
+ } ;
131
+
132
+ if ( process . env . NODE_ENV === 'production' ) {
133
+ app . use ( errorhandler ( { "dumpExceptions" : false , "showStack" : false } ) ) ;
134
+ } ;
132
135
133
136
134
137
app . all ( '*' , function ( req , res , next ) {
@@ -140,6 +143,7 @@ function setAppConfigure(){
140
143
} ) ;
141
144
}
142
145
146
+
143
147
function initModules ( ) {
144
148
log . init ( ) ;
145
149
database . init ( ) ;
@@ -167,6 +171,12 @@ function addAccessLog(req){
167
171
config . addAccessLog ( parameter ) ;
168
172
}
169
173
174
+ function initSwagger ( ) {
175
+ log . info ( "Setting up Swagger" ) ;
176
+ app . use ( '/api/api-docs' , swaggerUi . serve , swaggerUi . setup ( swaggerDocument ) ) ;
177
+
178
+ }
179
+
170
180
function isNoNeedToAddAccessLog ( url ) {
171
181
return noNeedAccessLogUriList . indexOf ( url ) >= 0 ;
172
182
}
0 commit comments