var express = require("express");
var router = express.Router();

const actualFlagPost = {
  title: "The flag",
  content: "IGCTF{how-do-caches-work?}",
};

const posts = {
  "the-flag": {
    title: "The flag",
    content:
      "Easy challenge, right? Hold up, I forgot what the flag was again.. I'll update this page in a minute with the real flag.",
  },
  "updates-to-my-website": {
    title: "Updates to my website",
    content:
      "Hosting this website has become expensive. So now I'm using a cache to decrease server load. It's my first time using a cache, so I hope I set it up right.",
  },
};

/* GET posts pages. */
router.get("/:slug", function (req, res, next) {
  if (!posts[req.params.slug]) throw new Error("Post not found");
  res.render("post", posts[req.params.slug]);

  // yes, this is a bad way of making sure that requests after the first one contain the actual flag.
  if (req.params.slug === "the-flag") posts["the-flag"] = actualFlagPost;
});

module.exports = router;
