use fastly::http::{header::LOCATION, StatusCode};
use fastly::{ConfigStore, Error, Request, Response};

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    let redirects = ConfigStore::open("store_name");
    if let Some(dest) = redirects.get(req.get_path()) {
        let mut new_url = req.get_url().clone();
        new_url.set_path(dest.as_str());

        // Build a redirect response and return it downstream.
        return Ok(
            Response::from_status(StatusCode::PERMANENT_REDIRECT).with_header(LOCATION, new_url)
        );
    }
    Ok(req.send("origin_0")?)
}