-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
124 lines (100 loc) · 2.28 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
var express = require('express');
var path = require('path');
var mount_routes = require('mount-routes');
var mount_plugins = require('mount_plugin');
var app = express();
var lifecycle = require('./lifecycle');
app.set_absolute_path = function (key, path) {
this.set(key, app.cfg.root + "/" + path);
};
app.set_key_with_setting_key = function (key, setting_key) {
var __path = path.join(app.cfg.root, app.cfg[setting_key]);
if (app.debug) {
console.log(key + " = " + __path);
}
this.set(key, __path);
};
/**
* mount routes
*/
app.mount_routes = function (path) {
mount_routes(this, path, this.debug);
}
/**
* mount plugins
*/
app.mount_plugins = function (plugin_dir) {
mount_plugins(this, plugin_dir, this.debug);
}
module.exports = function (config) {
// root = base2-examples/
// base2-examples/node_modules/base2
app.set('root', _get_default_root_path());
var deepExtend = require('deep-extend');
deepExtend(app, {
express: express
});
var cfg = {
debug: false,
type: "express",
favicon: 'favicon.ico',
favicon_enable: false,
post_enable: true,
post_limit_size : '100kb',
cookie_enable: true,
log_enable: true,
log_level: "dev",
// "views": "views",
// "routes": "routes",
// "public": "public",
pre: function (app) {
if (app.debug) {
console.log('pre hook');
}
},
post: function (app) {
if (app.debug) {
console.log('post hook');
}
}
}
deepExtend(cfg, config);
app.debug = cfg.debug;
app.cfg = cfg;
// init lifecycle
var life = app.life = lifecycle(app);
// deepExtend(app, cfg);
// hook_pre
hook_pre(app);
// settings
life.settings();
//
// global middlewares
life.global_middlewares();
//
// routes
life.routes();
// hook_post
hook_post(app);
return app;
};
function __set(app, k, v, default_v){
app.set('port', v ? v : default_v);
}
function __call (config, key, app) {
if (config[key]) {
config[key](app);
}
}
function hook_post (app) {
__call(app.cfg, 'post', app);
}
function hook_pre (app) {
__call(app.cfg, 'pre', app);
}
function _get_default_root_path () {
var arr = __dirname.split('/');
arr.pop();
arr.pop();
return arr.join('/');
}