website-staging-server/index.ts

57 lines
1.2 KiB
TypeScript

import { execSync } from "child_process";
import dotenv from 'dotenv'
dotenv.config()
import Express from "express";
const app = Express();
const PORT = 58081;
const base64 = Buffer.from(`${process.env.USER}:${process.env.PASS}`).toString("base64");
app.use(Express.static("public"));
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(`Example app listening at http://hfcs:${PORT}`);
});
function getLatestArtifacts() {
const url =
"https://git.uwaterloo.ca/csc/website/-/jobs/artifacts/adi-staging/download?job=push_to_staging";
const zipPath = "tmp/artifact.zip";
execSync(`wget -O ${zipPath} '${url}'`);
}