2021-04-02 12:24:01 -05:00
|
|
|
const express = require("express");
|
|
|
|
const { createProxyMiddleware } = require("http-proxy-middleware");
|
|
|
|
const port = process.env.PORT || 3000;
|
2021-04-12 13:10:31 -07:00
|
|
|
const fetch = require("node-fetch");
|
2021-04-02 12:24:01 -05:00
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
const backendEndpoint = "https://cdb-ms-mpac-pbe.cosmos.azure.com";
|
|
|
|
const previewSiteEndpoint = "https://dataexplorer-preview.azurewebsites.net";
|
|
|
|
const previewStorageWebsiteEndpoint = "https://dataexplorerpreview.z5.web.core.windows.net/";
|
|
|
|
const githubApiUrl = "https://api.github.com/repos/Azure/cosmos-explorer";
|
|
|
|
const githubPullRequestUrl = "https://github.com/Azure/cosmos-explorer/pull";
|
|
|
|
const azurePortalMpacEndpoint = "https://ms.portal.azure.com/";
|
|
|
|
|
|
|
|
const api = createProxyMiddleware({
|
|
|
|
target: backendEndpoint,
|
2021-04-02 12:24:01 -05:00
|
|
|
changeOrigin: true,
|
|
|
|
logLevel: "debug",
|
|
|
|
bypass: (req, res) => {
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.send();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
const proxy = createProxyMiddleware({
|
|
|
|
target: backendEndpoint,
|
2021-04-02 12:24:01 -05:00
|
|
|
changeOrigin: true,
|
|
|
|
secure: false,
|
|
|
|
logLevel: "debug",
|
|
|
|
pathRewrite: { "^/proxy": "" },
|
|
|
|
router: (req) => {
|
|
|
|
let newTarget = req.headers["x-ms-proxy-target"];
|
|
|
|
return newTarget;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
const commit = createProxyMiddleware({
|
|
|
|
target: previewStorageWebsiteEndpoint,
|
2021-04-02 12:24:01 -05:00
|
|
|
changeOrigin: true,
|
|
|
|
secure: false,
|
|
|
|
logLevel: "debug",
|
2025-01-30 16:14:03 -08:00
|
|
|
pathRewrite: { "^/commit": "/" },
|
2021-04-02 12:24:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
app.use("/api", api);
|
|
|
|
app.use("/proxy", proxy);
|
|
|
|
app.use("/commit", commit);
|
2021-04-12 13:10:31 -07:00
|
|
|
app.get("/pull/:pr(\\d+)", (req, res) => {
|
|
|
|
const pr = req.params.pr;
|
2025-01-30 16:14:03 -08:00
|
|
|
if (!/^\d+$/.test(pr)) {
|
|
|
|
return res.status(400).send("Invalid pull request number");
|
|
|
|
}
|
2021-04-12 13:10:31 -07:00
|
|
|
const [, query] = req.originalUrl.split("?");
|
|
|
|
const search = new URLSearchParams(query);
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
fetch(`${githubApiUrl}/pulls/${pr}`)
|
2021-04-12 13:10:31 -07:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then(({ head: { ref, sha } }) => {
|
2025-01-30 16:14:03 -08:00
|
|
|
const prUrl = new URL(`${githubPullRequestUrl}/${pr}`);
|
2021-04-12 13:10:31 -07:00
|
|
|
prUrl.hash = ref;
|
|
|
|
search.set("feature.pr", prUrl.href);
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
const explorer = new URL(`${previewSiteEndpoint}/commit/${sha}/explorer.html`);
|
2021-04-12 13:10:31 -07:00
|
|
|
explorer.search = search.toString();
|
|
|
|
|
2025-01-30 16:14:03 -08:00
|
|
|
const portal = new URL(azurePortalMpacEndpoint);
|
2021-04-12 13:10:31 -07:00
|
|
|
portal.searchParams.set("dataExplorerSource", explorer.href);
|
|
|
|
|
|
|
|
return res.redirect(portal.href);
|
|
|
|
})
|
|
|
|
.catch(() => res.sendStatus(500));
|
|
|
|
});
|
2021-05-27 22:13:18 -05:00
|
|
|
app.get("/", (req, res) => {
|
2025-01-30 16:14:03 -08:00
|
|
|
fetch(`${githubApiUrl}/branches/master`)
|
2021-05-27 22:13:18 -05:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then(({ commit: { sha } }) => {
|
2025-01-30 16:14:03 -08:00
|
|
|
const explorer = new URL(`${previewSiteEndpoint}/commit/${sha}/hostedExplorer.html`);
|
2021-05-27 22:13:18 -05:00
|
|
|
return res.redirect(explorer.href);
|
|
|
|
})
|
|
|
|
.catch(() => res.sendStatus(500));
|
|
|
|
});
|
2021-04-12 13:10:31 -07:00
|
|
|
|
2021-04-02 12:24:01 -05:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Example app listening on port: ${port}`);
|
|
|
|
});
|