-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.18 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
var fs = require('fs');
var path = require('path');
var mount = require('koa-mount');
var compose = require('koa-compose');
var rel = function(p) {
return path.relative(__dirname, path.join(process.cwd(), p));
};
var dir;
var middleware = [];
var getEndpoints = function(apiDir) {
var update = false;
if(!apiDir) {
apiDir = dir;
update = true;
} else {
dir = apiDir;
}
middleware.length = 0;
middleware = middleware.concat(fs.readdirSync(apiDir)
.map(function(p) {
if(path.extname(p) == '.js') {
return path.basename(p, '.js');
} else {
return false;
}
}).filter(function(name) {
return name;
}).map(function(name) {
var loc = './' + rel(path.join(apiDir, name));
if(require.cache[require.resolve(loc)]) {
delete require.cache[require.resolve(loc)];
}
var middleware = require(loc);
if(middleware.middleware) {
middleware = mount('/' + name, require(loc));
}
return middleware;
}));
if(!update) {
return function*() {
yield compose(middleware);
};
} else {
process.send('rebuild-api-complete');
}
};
process.on('message', function(m) {
if(m == 'rebuild-api') {
getEndpoints();
}
});
module.exports = getEndpoints;