website-staging-server/index.ts

60 lines
1.3 KiB
TypeScript

import { execSync } from "child_process";
import dotenv from "dotenv";
dotenv.config();
import Express from "express";
const app = Express();
const PORT = process.env.PORT;
const base64 = Buffer.from(`${process.env.USER}:${process.env.PASS}`).toString(
"base64",
);
app.use(Express.static(__dirname + "/out"));
app.post("/", (req, res) => {
const header = req.header("authorization");
if (header == null) {
res.status(403).send("Authorization header is needed");
return;
}
if (header.split(" ").length !== 2) {
res.status(400).send("Authorization header is malformed");
return;
}
const [type, secret] = header.split(" ");
if (type !== "Basic") {
res.status(400).send("Only basic authorization is supported");
return;
}
if (secret !== base64) {
res.status(401).send("Incorrect username or password");
return;
}
getLatestArtifacts();
res.send("Hello World!");
});
app.listen(PORT, () => {
console.log(`👀 http://localhost:${PORT}`);
});
function getLatestArtifacts() {
const branch = "linna-staging";
const job = "deploy-staging";
const url = `https://git.uwaterloo.ca/csc/website/-/jobs/artifacts/${branch}/download?job=${job}`;
const zipPath = `${branch}.zip`;
execSync(`wget -O ${zipPath} '${url}'`);
}