#![cfg(feature = "ui")]
use bytes::Bytes;
use hyper::{Body, Request, Response, StatusCode};
const BASE_URL: &str = "/ui";
const CATCH_ALL_URL: &str = "index.html";
pub fn handle_get(req: &Request<Body>) -> Option<Response<Body>> {
if req.uri().path() == "/" {
return Some(
Response::builder()
.status(StatusCode::MOVED_PERMANENTLY)
.header("Content-Type", "text/plain")
.header("Location", "/ui/")
.body(Bytes::from("Moved permanently to /ui/").into())
.unwrap()
)
}
let req_path = std::path::Path::new(req.uri().path());
if let Ok(p) = req_path.strip_prefix(BASE_URL) {
match routinator_ui::endpoints::ui_resource(p) {
Some(endpoint) => {
Some(serve(endpoint.content, endpoint.content_type))
}
None => {
if let Some(default) =
routinator_ui::endpoints::ui_resource(
std::path::Path::new(CATCH_ALL_URL)
)
{
Some(serve(default.content, default.content_type))
} else {
Some(super::not_found())
}
}
}
} else {
Some(super::not_found())
}
}
fn serve(data: &'static [u8], ctype: &'static [u8]) -> Response<Body> {
Response::builder()
.header("Content-Type", ctype)
.body(data.into())
.unwrap()
}