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

const BACKEND_ORIGIN_0: &str = "origin_0";
const BACKEND_ORIGIN_1: &str = "origin_1";

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    match (req.get_path(), req.get_query_str()) {
        // Requests for exactly / => origin 0
        // (query strings not allowed)
        ("/", None) => Ok(req.send(BACKEND_ORIGIN_0)?),
        // Requests for /status and all subpaths => origin 1
        // (query strings allowed)
        (s, _) if s == "/status" || s.starts_with("/status/") => Ok(req.send(BACKEND_ORIGIN_1)?),
        // Unrecognised path => 404 error
        _ => Ok(
            Response::from_body("The page you requested could not be found")
                .with_status(StatusCode::NOT_FOUND),
        ),
    }
}