import { Router } from "@fastly/expressly";

const router = new Router();

router.get("(.*)", async (req, res) => {
    let url;
    try {
      url = new URL(req.query.get("url"));
    } catch (err) {
      console.log("unable to extract a valid url from query string");
      res.sendStatus(400);
    }

    if(IsValidDomain(url.hostname)){
        console.log("Valid domain: "+url.hostname);
        res.text("hello");
    }else{
        console.log("Invalid domain: "+url.hostname);
        res.sendStatus(403);
    }
});

router.listen();

// List of valid domains.
function ValidDomain() {
  return ["example.com", "fastly.com"];
}

// IsValidDomain() function checks if a string is in
// the list of valid domains.
function IsValidDomain(d) {
  for (let i = 0; i < ValidDomain().length; i++) {
    if (d === ValidDomain()[i]) {
      return true;
    }
  }
  return false;
}