use based::request::assets::DataResponse;
use rocket::{get, launch, routes};

#[get("/")]
pub async fn index() -> DataResponse {
    DataResponse::new(
        include_str!("page/index.html").as_bytes().to_vec(),
        "text/html".to_string(),
        Some(60 * 60),
    )
}

#[get("/banner")]
pub async fn banner_img() -> DataResponse {
    DataResponse::new(
        include_bytes!("banner.png").to_vec(),
        "image/png".to_string(),
        Some(60 * 60),
    )
}

#[get("/rootfs/<arch>/rootfs.tar.xz")]
pub async fn rootfs_tar(arch: &str) -> DataResponse {
    match arch {
        "aarch64" | "arm64" => DataResponse::new_file(
            "./tar/aarch64.tar.xz",
            "application/tar".to_string(),
            Some(60 * 60),
        ),
        "x86_64" | "amd64" => DataResponse::new_file(
            "./tar/x86_64.tar.xz",
            "application/tar".to_string(),
            Some(60 * 60),
        ),
        _ => DataResponse::new(
            "Weird arch".as_bytes().to_vec(),
            "text/plain".to_string(),
            None,
        ),
    }
}

#[get("/iso/<arch>")]
pub async fn install_iso(arch: &str) -> DataResponse {
    match arch {
        "x86_64" | "amd64" => DataResponse::new_file(
            "./iso/x86_64.iso",
            "application/x-iso9660-image".to_string(),
            Some(60 * 60),
        ),
        _ => DataResponse::new(
            "Weird arch".as_bytes().to_vec(),
            "text/plain".to_string(),
            None,
        ),
    }
}

#[launch]
async fn rocket() -> _ {
    env_logger::init();

    rocket::build().mount("/", routes![index, rootfs_tar, banner_img, install_iso])
}
