-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (77 loc) · 2.65 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
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
const fs = require('fs');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
/**
* CORS & Preflight request
*/
const { CORS_ALLOW_ORIGIN } = process.env
app.use((req, res, next) => {
if (req.path !== '/' && !req.path.includes('.')) {
res.set({
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin':
CORS_ALLOW_ORIGIN || req.headers.origin || '*',
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
'Content-Type': 'application/json; charset=utf-8',
})
}
req.method === 'OPTIONS' ? res.status(204).end() : next()
})
/**
* binding route
*/
const providerPath = path.join(__dirname, 'out', 'data-source', 'provider')
const providerFiles = fs.readdirSync(providerPath)
const providerList = providerFiles.filter(f => f.endsWith('.js')).map(f => require(path.join(providerPath, f)).default)
const handlerPath = path.join(__dirname, 'handler');
const files = fs.readdirSync(handlerPath)
const handlers = files.reverse().filter(f => f.endsWith('.js'))
.map(file => {
const identifier = file.split('.').shift()
let providers = []
const route = `/${file.replace(/\[([a-z]+(,[a-z]+)*)\]/, (m, p1) => {
providers = providerList.filter(p => {
for (const i of p1.split(',')) {
if (p.support(i)) {
return true
}
}
return false
})
return ''
})
.replace(/\.js$/i, '')
.replace(/_/g, '/')
.replace(/@/g, ':')}`
const modulePath = path.join(handlerPath, file)
const handle = require(modulePath)
return { identifier, route, providers, handle }
})
console.log(handlers)
for (const handler of handlers) {
app.use(handler.route, async (req, res) => {
try {
res.send({
code: 200,
msg: 'succ',
data: await handler.handle(handler.providers, { ...req.params, ...req.query })
})
}
catch (e) {
console.error(e)
res.send({ code: 500, msg: e.message })
}
})
}
module.exports = app;