-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (33 loc) · 1.21 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
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import { createProxyMiddleware } from 'http-proxy-middleware';
dotenv.config();
const PORT = process.env.PORT || 3000;
const app = express();
const corsOptions = {
origin: process.env.CORS_URLS.split(","),
optionsSuccessStatus: 200
}
app.use(cors(corsOptions));
//////////////////////////////////////////////////////////////
function getProxy(providerName) {
return createProxyMiddleware({
target: process.env[providerName.toUpperCase() + "_API_URL"],
changeOrigin: true,
pathRewrite: (path, req) => {
if (process.env.LOG) { console.log("Proxying " + process.env[providerName.toUpperCase() + "_API_URL"] + path); }
return path.replace("/" + providerName, "");
},
headers: { Authorization: "Bearer " + process.env[providerName.toUpperCase() + "_API_TOKEN"] }
});
}
// ---------------------------------------------
const provider = ["pixelfed", "mastodon"];
provider.forEach((key) => {
app.use("/" + key, getProxy(key));
})
//////////////////////////////////////////////////////////////
app.listen(PORT, () => {
console.log(`API Proxy listening on port ${PORT} for ${provider.join(", ")}`);
});