checkpoint
This commit is contained in:
+22
-15
@@ -1,6 +1,9 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use axum::body::Body;
|
||||
use axum::extract::State;
|
||||
use axum::http::Uri;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
@@ -9,7 +12,13 @@ use crate::renderer::{default_path, render, RenderOutput};
|
||||
pub async fn start_server(root: PathBuf) {
|
||||
let state = Arc::new(root);
|
||||
let app = Router::new()
|
||||
.route("/", get(handler))
|
||||
.route("/", get(|State(root): State<Arc<PathBuf>>| async {
|
||||
let path = default_path(&root);
|
||||
serve_path(String::from(path.to_str().unwrap()), root)
|
||||
}))
|
||||
.route("/{*path}", get(|State(root): State<Arc<PathBuf>>, uri: Uri| async move {
|
||||
serve_path(String::from(uri.path()), root)
|
||||
}))
|
||||
.with_state(state);
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
@@ -17,34 +26,32 @@ pub async fn start_server(root: PathBuf) {
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn handler(
|
||||
State(root): State<Arc<PathBuf>>,
|
||||
) -> impl IntoResponse {
|
||||
let path = default_path(&root);
|
||||
serve_path(String::from(path.to_str().unwrap()), root)
|
||||
}
|
||||
|
||||
fn serve_path(path: String, root: impl AsRef<PathBuf>) -> impl IntoResponse {
|
||||
let path = path.strip_prefix("/").unwrap_or(path.as_str());
|
||||
let render = render(path.into(), root.as_ref());
|
||||
|
||||
match render {
|
||||
Ok(RenderOutput::Rendered(file, content)) |
|
||||
Ok(RenderOutput::Hidden(file, content)) => {
|
||||
Ok(RenderOutput::Rendered(_, content)) |
|
||||
Ok(RenderOutput::Hidden(_, content)) => {
|
||||
Response::builder()
|
||||
.status(200)
|
||||
.header("Content-Type", "text/html")
|
||||
.body(content).unwrap()
|
||||
.body(Body::from(content)).unwrap()
|
||||
}
|
||||
Ok(RenderOutput::RawFile(file)) => {
|
||||
Response::builder()
|
||||
.status(200)
|
||||
.body(Body::from(fs::read(file).unwrap_or(vec![]))).unwrap()
|
||||
}
|
||||
Ok(RenderOutput::RawFile(file)) => { todo!() }
|
||||
Ok(RenderOutput::NoOutput) |
|
||||
Ok(RenderOutput::Dir(_)) => {
|
||||
Response::builder()
|
||||
.status(404)
|
||||
.body(String::from("Not Found")).unwrap()
|
||||
.body(Body::from(include_str!("../templates/404.html"))).unwrap()
|
||||
}
|
||||
Err(err) => {
|
||||
Response::builder()
|
||||
.status(500)
|
||||
.body(format!("{}", err)).unwrap()
|
||||
.body(Body::from(format!("{}", err))).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user