-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (71 loc) · 2 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
require('dotenv').config()
const http = require('http')
const axios = require('axios')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const Auth = require('@baiducloud/sdk').Auth
const auth = new Auth(process.env.BCE_AK, process.env.BCE_SK)
// BCE host
const bceAxios = axios.create({
httpAgent: new http.Agent({
keepAlive: true,
maxSockets: 200
}),
maxSockets: 200
})
// Express
function createApp() {
let app = express();
// application level middleware
if (process.env.NODE_ENV === 'development') {
app.use(cors({
origin: true,
credentials: true
}))
}
app.disable('x-powered-by');
app.use(bodyParser.json({
limit: '200kb'
}))
app.use(bodyParser.urlencoded({
parameterLimit: 100,
extended: true
}))
return app
}
// sign request which will be forwarded to
function sign(req, resp, next) {
const { url, method = 'GET', params = {}, headers = {} } = req.body
headers['x-bce-date'] = new Date().toISOString()
const signature = auth.generateAuthorization(method, url, params, headers, undefined, undefined, ['Host'])
headers['Authorization'] = signature
req.body.headers = headers
next()
}
const bce = createApp()
const router = new express.Router()
router.use(sign)
router.post('/', async (req, resp) => {
// forward requests
const { method = 'GET', url, params = {}, headers } = req.body
let r
console.log('%s %s HTTP/1.1', method, url)
console.log('Host: %s', headers['Host'])
console.log('Authorization: %s', headers['Authorization'])
try {
r = await bceAxios.request({
url: `http://${headers['Host'] || headers['host']}${url}`,
method,
params,
headers
})
} catch (e) {
console.log('error: %s', e)
resp.status(500).end()
return
}
resp.json(r.data)
})
bce.use(router)
bce.listen(8080)