-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (151 loc) · 4.73 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* peppa
* */
// NODE_ENV
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
// un caught exception
process.on('uncaughtException', console.error);
// un handled rejection
process.on('unhandledRejection', (reason, p) => {
console.log('unhandled Rejection at:', p, 'reason:', reason);
});
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const Koa = require('koa');
const ip = require('ip');
const extend = require('extend');
const ENV = process.env.NODE_ENV;
const HANDLE_HOOK = Symbol('Peppa#Handle#Hook');
const LOADER = Symbol('Peppa#Loader');
// 在 koa 上面做扩展
class Peppa extends Koa {
constructor(options = {}) {
super();
// config
this.config = options;
// root dir
this.root = this.config.root || process.cwd();
// modules dir
this.modules = this.config.modules || process.cwd();
assert(typeof this.root === 'string', 'config.root required,and must be a string');
assert(fs.existsSync(this.root), `Directory ${this.root} is not exist`);
assert(fs.statSync(this.root).isDirectory(), `Directory ${this.root} is not a directory`);
// plugins
this.plugins = [];
// hooks
this.hooks = {
onStart: [],
onStarted: []
};
// locals
this.locals = {
env: ENV,
resourceCache: {
css: true,
js: true
}
};
//
this.proxy = true;
//
this.set('env', ENV);
// koa.use();
this.use((ctx, next) => {
ctx.set('X-Powered-By', 'Peppa');
ctx.peppa = this;
return next();
});
// run loader
this[LOADER]();
}
get(name) {
return this[name];
}
set(name, value) {
this[name] = value;
}
// start
start(port, fn, ...args) {
// run hooks
this[HANDLE_HOOK]('onStart');
if (!port) {
port = this.config.port;
}
assert(typeof port === 'number', `Application server port: ${port} must be a Number!`);
const rawArgv = arguments;
const msg = `[INFO] ip: "${ip.address()}", process.pid : "${process.pid}",env:"${ENV}", server has started on port:"${port}"`;
// listen
this.listen(port, () => {
this[HANDLE_HOOK]('onStarted');
if (rawArgv.length === 0) {
} else if (rawArgv.length === 1) {
if (typeof port === 'function') {
return port(ip.address());
}
} else {
if (typeof fn === 'function') {
return fn(ip.address());
}
console.log(`[INFO] server has started on port:${port}, ${fn}`, ...args);
}
});
}
// run hooks
[HANDLE_HOOK](name) {
this.hooks[name].forEach(plugin => {
assert(typeof plugin[name] === 'function', 'Plugin muse be an function!');
plugin[name](this);
});
}
[LOADER]() {
}
// use plugin
usePlugin(plugin, opt, ...middleware) {
if (this.plugins.indexOf(plugin) === -1) {
assert(typeof plugin === 'function', 'Plugin muse be an function!');
this.plugins.push(plugin);
// register hooks
plugin = new plugin(extend(true, opt, this.config), ...middleware);
for (let key in this.hooks) {
if (typeof plugin[key] === 'function') {
this.hooks[key].push(plugin);
}
}
}
return this;
}
// use bucket
useBucket(...middleware) {
const rootPath = this.config.rootPath || '';
let publicPath = this.config.publicPath || 'public';
publicPath = path.resolve(rootPath, publicPath);
// middleware
this.usePlugin(require('./plugins/middlewares'), {
exception: {
v500: 'exception/500',
v404: 'exception/404',
},
publicPath: publicPath
}, ...middleware);
// render
this.usePlugin(require('./plugins/render'), {
viewPath: path.resolve(rootPath, 'view'),
cache: ENV !== 'development'
});
// request
this.usePlugin(require('./plugins/request'));
// router
this.usePlugin(require('./plugins/router'), {
// controller path
controllerPath: path.resolve(rootPath, 'controller'),
// route path
routePath: path.resolve(rootPath, 'route'),
// filter path
filterPath: path.resolve(rootPath, 'filter')
});
}
}
module.exports = Peppa;