-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.js
43 lines (36 loc) · 1.09 KB
/
gateway.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
var express = require("express")
const path = require("path")
const helmet = require("helmet")
var {
createProxyMiddleware
} = require("http-proxy-middleware")
const { helmetOptions } = require("lib")
var app = express()
app.use("/.well-known/", express.static(path.join(__dirname, "../.well-known/"), {
dotfiles: "allow"
}))
app.use(helmet(helmetOptions))
if(process.env.gateway_HTTPS_Redirect == "true"){
app.use((req, res, next) => {
if(req.secure || req.path.split("/")[1] == ".well-known"){
next()
}
else{
res.redirect(`https://${req.headers.host}${req.url}`)
}
})
}
const services = require("./services.js")
for(const apiService of services){
const {serviceOn, log, postPathRegex, getPathRegex, baseURL} = apiService
if(serviceOn){
console.log(log)
app.use(new RegExp(postPathRegex.source + "|" + getPathRegex.source), createProxyMiddleware((pathname, req) => {
return (pathname.match(postPathRegex) && req.method === "POST") || (pathname.match(getPathRegex) && req.method === "GET")
}, {
target: baseURL,
logLevel: "silent",
}))
}
}
module.exports = app