-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
137 lines (103 loc) · 3.53 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const path = require('path')
const extend = require('extend')
const mountRoutes = require('mount-koa-routes')
const Koa = require('koa')
const app = new Koa()
const env = process.env.NODE_ENV || "development"
global.debug = require('debug')('moa2')
module.exports = function (config) {
global.$config = config
require('./init')
// require('./db') move db.js to project
require('./config/global')
// $models
global.$models = require('mount-models')($config.home + '/');
debug('global.$models')
debug(global.$models)
if ($config.db_debug === true) {
return;
}
debug('Configuration = ' + JSON.stringify(config, null, 4))
debug('NODE_ENV = ' + env)
safe_require(path.join($config.home, './init'))
safe_require(path.join($config.home, './db'))
safe_require(path.join($config.home, './config/global'))
// $middlewares
global.$middlewares = require('mount-middlewares')(__dirname)
console.log('build-in middlewares = ' )
debug($middlewares)
let build_ins = config.build_ins;
for (let i in build_ins) {
var default_middleware = build_ins[i]
debug('mount build-in global middleware with ' + default_middleware )
//+ ':' + $middlewares[default_middleware]
app.use($middlewares[default_middleware])
}
extend(global.$middlewares, require('mount-middlewares')(config.home))
console.dir('global.$middlewares' + config.middlewares.global)
global.$global_middlewares = []
if (config.middlewares['global']) {
global.$global_middlewares = config.middlewares['global']
}
delete config.middlewares['global']
// load_koa_middlewares with configuration
let configed_middlewares = require('get_koa_middlewares_object_with_config')(config.middlewares)
for (let key in configed_middlewares) {
global.$middlewares[key] = configed_middlewares[key]
}
$global_middlewares.forEach(function (middleware) {
debug('mount global middleware with ' + middleware)
app.use($middlewares[middleware])
})
// $controllers
global.$controllers = require('mount-controllers')($config.home + '/app/');
debug('global.$controllers')
debug(global.$controllers)
// for production
if (env === 'production') {
app.use($middlewares.log4js())
config.routes.map(function (route) {
// mount routes from app/routes folder
if (route.path) {
mountRoutes(app, path.join(route.path, route.folder), false)
} else {
mountRoutes(app, path.join(__dirname, route.folder), false)
}
})
} else if (env === 'test') {
// for test
debug('test')
// mount routes from app/routes folder
config.routes.map(function (route) {
// mount routes from app/routes folder
if (route.path) {
mountRoutes(app, path.join(route.path, route.folder), true)
} else {
mountRoutes(app, path.join(__dirname, route.folder), true)
}
})
} else {
// default for development
app.use($middlewares.logger)
// request logger
app.use($middlewares.request_logger)
debug(config.routes)
// mount routes from app/routes folder
config.routes.map(function (route) {
debug(path.join(route.path, route.folder))
// mount routes from app/routes folder
if (route.path) {
mountRoutes(app, path.join(route.path, route.folder), true)
} else {
mountRoutes(app, path.join(__dirname, route.folder), true)
}
})
}
// response
app.on('error', function (err, ctx) {
debug(err)
})
app.start = app.listen
return app
}