llldap/src/http/mod.rs

107 lines
3.4 KiB
Rust
Raw Normal View History

2026-09-03 21:50:32 +02:00
use axum::Router;
use axum::extract::State;
use axum::response::Html;
use axum::routing::{get, post};
2026-09-03 21:50:32 +02:00
use axum::serve::Listener as AxumListener;
use axum_extra::extract::cookie::CookieJar;
use http::StatusCode;
use minijinja::{Environment, context, path_loader};
use static_serve::embed_assets;
2026-09-03 21:50:32 +02:00
use crate::db::{Database, DatabaseInterface};
use crate::listener::{Listener, ListenerKind};
use crate::stream::{AbstractSocketAddr, AbstractStreamKind};
mod login;
mod logout;
mod session;
use session::HttpSessionManager;
2026-09-03 21:50:32 +02:00
impl AxumListener for Listener {
type Io = AbstractStreamKind;
type Addr = AbstractSocketAddr;
async fn accept(&mut self) -> (Self::Io, Self::Addr) {
loop {
let res = match &self.kind {
ListenerKind::Tcp(l) => l
.accept()
.await
.map(|(stream, remote_addr)| (stream.into(), remote_addr.into())),
ListenerKind::Uds(l) => l
.accept()
.await
.map(|(stream, remote_addr)| (stream.into(), remote_addr.into())),
};
match res {
Ok((stream, remote_addr)) => return (stream, remote_addr),
Err(e) => {
// Here the error could be fatal, or could simply be that a client aborted the connected,
// in which case we don't want to crash the server, simply skip this client connection.
// https://doc.rust-lang.org/stable/std/net/struct.TcpListener.html#errors
match e.kind() {
std::io::ErrorKind::ConnectionAborted => {}
_ => panic!("Unrecoverable HTTP client connection error: {e}"),
}
}
}
}
}
fn local_addr(&self) -> std::io::Result<Self::Addr> {
match &self.kind {
ListenerKind::Tcp(l) => l.local_addr().map(Into::into),
ListenerKind::Uds(l) => l.local_addr().map(Into::into),
}
}
}
#[derive(Clone)]
pub struct HttpState<D: DatabaseInterface> {
pub db: Database<D>,
pub sessions: HttpSessionManager,
pub templates: Environment<'static>,
}
impl<D: DatabaseInterface> HttpState<D> {
pub fn new(db: Database<D>) -> Self {
let mut templates = Environment::new();
templates.set_loader(path_loader("templates"));
Self {
db,
sessions: HttpSessionManager::new(),
templates,
}
}
}
2026-09-03 21:50:32 +02:00
pub async fn http_listen<D: DatabaseInterface>(listener: Listener, db: Database<D>) {
embed_assets!("assets");
let app = Router::new()
.nest("/assets", static_router())
.route("/", get(home))
.route("/login", get(home))
.route("/login", post(login::post_login))
.route("/logout", get(logout::logout))
.with_state(HttpState::new(db));
2026-09-03 21:50:32 +02:00
axum::serve(listener, app).await.unwrap();
}
pub async fn home<D: DatabaseInterface>(
State(state): State<HttpState<D>>,
cookies: CookieJar,
) -> (StatusCode, Html<String>) {
if let Some(session) = state.sessions.get_session(&cookies) {
let page = state
.templates
.get_template("home.html")
.unwrap()
.render(context! {username => session.username})
.unwrap();
(StatusCode::OK, Html(page))
} else {
login::login_page(State(state), None).await
}
2026-09-03 21:50:32 +02:00
}