diff --git a/src/db/interface.rs b/src/db/interface.rs index c109873..baccc60 100644 --- a/src/db/interface.rs +++ b/src/db/interface.rs @@ -53,7 +53,7 @@ pub trait DatabaseInterface: Clone + Send + Sync + 'static { fn create_domain( &mut self, domain: &str, - ) -> impl Future, BoxedError>>; + ) -> impl Future, BoxedError>> + Send; fn get_domain( &self, domain: &str, diff --git a/src/http/domain.rs b/src/http/domain.rs index 479b5f4..8bccc5b 100644 --- a/src/http/domain.rs +++ b/src/http/domain.rs @@ -1,8 +1,9 @@ -use axum::extract::{Path, State}; -use axum::response::{Html, IntoResponse, Response}; +use axum::extract::{Form, Path, State}; +use axum::response::{Html, IntoResponse, Redirect, Response}; use axum_extra::extract::cookie::CookieJar; use http::StatusCode; use minijinja::context; +use serde::Deserialize; use crate::db::{DatabaseInterface, Operation}; use crate::http::HttpState; @@ -52,3 +53,29 @@ pub async fn get_domain( .unwrap(); (StatusCode::OK, Html(page)).into_response() } + +#[derive(Clone, Debug, Deserialize)] +pub struct DomainCreationForm { + domainname: String, +} + +pub async fn create_domain( + State(mut state): State>, + cookies: CookieJar, + Form(form): Form, +) -> Response { + let Some(session) = state.sessions.get_session(&cookies) else { + return login_page(State(state), None).await.into_response(); + }; + + let op = Operation::CreateDomain; + if !session.user.can_perform(&op) { + return "Not authorized to create a new domain".into_response(); + } + + match state.db.create_domain(&form.domainname).await { + Ok(Ok(())) => Redirect::to(&format!("/domain/{}", form.domainname)).into_response(), + Ok(Err(e)) => format!("Failed to create domain {}: {}", form.domainname, e).into_response(), + Err(e) => format!("Database error: {e}").into_response(), + } +} diff --git a/src/http/mod.rs b/src/http/mod.rs index 4692ec9..9cd0a94 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -101,7 +101,7 @@ pub async fn http_listen(listener: Listener, db: Database< .route("/login", post(login::post_login)) .route("/logout", get(logout::logout)) .route("/domain/{domain}", get(domain::get_domain)) - //.route("/domain", post(domain::create_domain)) + .route("/domain", post(domain::create_domain)) .with_state(HttpState::new(db)); axum::serve(listener, app).await.unwrap();