-
Notifications
You must be signed in to change notification settings - Fork 1
/
_index.js
102 lines (94 loc) · 2.91 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
'use strict';
let routes = function(app) {
const compose = require('koa-compose');
const path = require('path');
const fs = require('fs');
const swaggerJSDoc = require('swagger-jsdoc');
const config = require(app.rootDir + '/lib/config');
const glob = require('glob');
const r = require('koa-router')({
prefix: config.app.namespace
});
r.get('', function*() {
this.body =
{ active: true
, timestamp: new Date().getTime()
};
return this.body;
});
function generateRoutes(files) {
files.map(function(file) {
if (file !== './routes/index.js') {
file = file.replace(/\/index.js|.\/routes\//g, '');
let paths = require(path.join(__dirname, file))(app);
for (let method in paths) {
if (paths.hasOwnProperty(method)) {
for (let p in paths[method]) {
if (paths[method].hasOwnProperty(p)) {
let args = paths[method][p];
let routeBreakdown = file.split('/');
let breakdownCount = routeBreakdown.length - 1;
let count = 0;
routeBreakdown = routeBreakdown.map(function(subpath) {
if (count < breakdownCount) {
subpath += ('/:' + subpath.replace(/s$/, 'Id'));
}
count++
return subpath;
});
let sp = routeBreakdown.join('/');
let uri = `${sp}${p}`
if (Array.isArray(args)) {
r[method.toLowerCase()](uri, compose(args));
} else {
r[method.toLowerCase()](uri, args);
}
}
}
}
}
}
});
}
let directories = glob
( './routes/**/**/index.js'
, function(er, files) {
if (!er) {
generateRoutes(files);
} else {
logger.error('Could not pull in files to generate routes.')
}
}
);
<%_ if (useSwagger) { _%>
if (process.env.NODE_ENV !== "PRODUCTION") {
let swaggerOptions =
{ swaggerDefinition:
{ swagger: '2.0'
, info:
{ title: 'API Explorer' // Title (required)
, version: '1.0.0' // Version (required)
, contact:
{ name: ''
, url: ''
}
}
, host: `${config.app.domain || config.app.host}:${config.app.port}`
, basePath: config.app.namespace
}
, apis:
[ app.rootDir + '/routes/**/*.js'
, app.rootDir + '/lib/**/*.js'
, app.rootDir + '/models/**/*.js'
]
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
let swaggerSpec = swaggerJSDoc(swaggerOptions);
r.get('/docs.json', function*() {
this.body = swaggerSpec;
});
}
<%_ } _%>
return r;
};
module.exports = routes;