wip: add client base and basic router

This commit is contained in:
2025-05-02 10:08:29 +02:00
parent 96bf9303ea
commit 4af8c825bb
8 changed files with 126 additions and 9 deletions

View File

@@ -1,7 +1,8 @@
use std::sync::{atomic::AtomicU32, Arc};
use http_body_util::{combinators::BoxBody, BodyExt, Full};
use hyper::{
body::{Bytes, Incoming}, server::conn::http1, service::service_fn, Error, Request, Response
body::{Bytes, Incoming},
server::conn::http1, service::service_fn, Error, Method, Request, Response
};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
@@ -10,11 +11,36 @@ mod logger;
mod args;
mod config;
macro_rules! response {
($status: ident $body: expr) => {{
let mut res = Response::new(Full::new(Bytes::from($body)).map_err(|n| match n {}).boxed());
*res.status_mut() = hyper::StatusCode::$status;
res
}};
($status: ident $body: expr, $($hkey: ident : $hval: literal),*$(,)?) => {{
let mut res = Response::new(Full::new(Bytes::from($body)).map_err(|n| match n {}).boxed());
*res.status_mut() = hyper::StatusCode::$status;
$(res.headers_mut()
.append(hyper::header::$hkey, hyper::header::HeaderValue::from_static($hval));)*
res
}};
}
async fn router(
_: Request<Incoming>,
req: Request<Incoming>,
_: Arc<SharedState>,
) -> Result<Response<BoxBody<Bytes, Error>>, Error> {
Ok(Response::new(Full::new("test".into()).map_err(|n| match n {}).boxed()))
match (req.method(), req.uri().path()) {
(&Method::GET, "/") => Ok(response!(
OK include_str!("../../dst/index.html"),
CONTENT_TYPE: "text/html",
)),
_ => Ok(response!(
NOT_FOUND include_str!("../../dst/not_found.html"),
CONTENT_TYPE: "text/html",
)),
}
}
fn load_config(path: &str) -> Result<config::Config, &'static str> {