mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-25 02:08:30 +02:00
wip: heavy refactoring html routers
This commit is contained in:
@@ -10,7 +10,8 @@ serde = { workspace = true }
|
||||
axum = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
async-openai = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
axum-htmx = "0.6.0"
|
||||
axum_session = "0.14.4"
|
||||
@@ -28,8 +29,6 @@ plotly = "0.12.1"
|
||||
surrealdb = "2.0.4"
|
||||
tower-http = { version = "0.6.2", features = ["fs"] }
|
||||
chrono-tz = "0.10.1"
|
||||
async-openai = "0.24.1"
|
||||
|
||||
|
||||
|
||||
common = { path = "../common" }
|
||||
composite-retrieval = { path = "../composite-retrieval" }
|
||||
|
||||
139
crates/html-router/src/error.rs
Normal file
139
crates/html-router/src/error.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
// use axum::{
|
||||
// http::StatusCode,
|
||||
// response::{Html, IntoResponse, Response},
|
||||
// };
|
||||
// use common::error::AppError;
|
||||
// use minijinja::context;
|
||||
// use minijinja_autoreload::AutoReloader;
|
||||
// use std::sync::Arc;
|
||||
|
||||
// pub type TemplateResult<T> = Result<T, HtmlError>;
|
||||
|
||||
// // Helper trait for converting to HtmlError with templates
|
||||
// pub trait IntoHtmlError {
|
||||
// fn with_template(self, templates: Arc<AutoReloader>) -> HtmlError;
|
||||
// }
|
||||
// // // Implement for AppError
|
||||
// impl IntoHtmlError for AppError {
|
||||
// fn with_template(self, templates: Arc<AutoReloader>) -> HtmlError {
|
||||
// HtmlError::new(self, templates)
|
||||
// }
|
||||
// }
|
||||
// // // Implement for minijinja::Error directly
|
||||
// impl IntoHtmlError for minijinja::Error {
|
||||
// fn with_template(self, templates: Arc<AutoReloader>) -> HtmlError {
|
||||
// HtmlError::from_template_error(self, templates)
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub enum HtmlError {
|
||||
// ServerError(Arc<AutoReloader>),
|
||||
// NotFound(Arc<AutoReloader>),
|
||||
// Unauthorized(Arc<AutoReloader>),
|
||||
// BadRequest(String, Arc<AutoReloader>),
|
||||
// Template(String, Arc<AutoReloader>),
|
||||
// }
|
||||
|
||||
// impl HtmlError {
|
||||
// pub fn new(error: AppError, templates: Arc<AutoReloader>) -> Self {
|
||||
// match error {
|
||||
// AppError::NotFound(_msg) => HtmlError::NotFound(templates),
|
||||
// AppError::Auth(_msg) => HtmlError::Unauthorized(templates),
|
||||
// AppError::Validation(msg) => HtmlError::BadRequest(msg, templates),
|
||||
// _ => {
|
||||
// tracing::error!("Internal error: {:?}", error);
|
||||
// HtmlError::ServerError(templates)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// pub fn from_template_error(error: minijinja::Error, templates: Arc<AutoReloader>) -> Self {
|
||||
// tracing::error!("Template error: {:?}", error);
|
||||
// HtmlError::Template(error.to_string(), templates)
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl IntoResponse for HtmlError {
|
||||
// fn into_response(self) -> Response {
|
||||
// let (status, context, templates) = match self {
|
||||
// HtmlError::ServerError(templates) | HtmlError::Template(_, templates) => (
|
||||
// StatusCode::INTERNAL_SERVER_ERROR,
|
||||
// context! {
|
||||
// status_code => 500,
|
||||
// title => "Internal Server Error",
|
||||
// error => "Internal Server Error",
|
||||
// description => "Something went wrong on our end."
|
||||
// },
|
||||
// templates,
|
||||
// ),
|
||||
// HtmlError::NotFound(templates) => (
|
||||
// StatusCode::NOT_FOUND,
|
||||
// context! {
|
||||
// status_code => 404,
|
||||
// title => "Page Not Found",
|
||||
// error => "Not Found",
|
||||
// description => "The page you're looking for doesn't exist or was removed."
|
||||
// },
|
||||
// templates,
|
||||
// ),
|
||||
// HtmlError::Unauthorized(templates) => (
|
||||
// StatusCode::UNAUTHORIZED,
|
||||
// context! {
|
||||
// status_code => 401,
|
||||
// title => "Unauthorized",
|
||||
// error => "Access Denied",
|
||||
// description => "You need to be logged in to access this page."
|
||||
// },
|
||||
// templates,
|
||||
// ),
|
||||
// HtmlError::BadRequest(msg, templates) => (
|
||||
// StatusCode::BAD_REQUEST,
|
||||
// context! {
|
||||
// status_code => 400,
|
||||
// title => "Bad Request",
|
||||
// error => "Bad Request",
|
||||
// description => msg
|
||||
// },
|
||||
// templates,
|
||||
// ),
|
||||
// };
|
||||
|
||||
// let html = match templates.acquire_env() {
|
||||
// Ok(env) => match env.get_template("errors/error.html") {
|
||||
// Ok(tmpl) => match tmpl.render(context) {
|
||||
// Ok(output) => output,
|
||||
// Err(e) => {
|
||||
// tracing::error!("Template render error: {:?}", e);
|
||||
// Self::fallback_html()
|
||||
// }
|
||||
// },
|
||||
// Err(e) => {
|
||||
// tracing::error!("Template get error: {:?}", e);
|
||||
// Self::fallback_html()
|
||||
// }
|
||||
// },
|
||||
// Err(e) => {
|
||||
// tracing::error!("Environment acquire error: {:?}", e);
|
||||
// Self::fallback_html()
|
||||
// }
|
||||
// };
|
||||
|
||||
// (status, Html(html)).into_response()
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl HtmlError {
|
||||
// fn fallback_html() -> String {
|
||||
// r#"
|
||||
// <html>
|
||||
// <body>
|
||||
// <div class="container mx-auto p-4">
|
||||
// <h1 class="text-4xl text-error">Error</h1>
|
||||
// <p class="mt-4">Sorry, something went wrong displaying this page.</p>
|
||||
// </div>
|
||||
// </body>
|
||||
// </html>
|
||||
// "#
|
||||
// .to_string()
|
||||
// }
|
||||
// }
|
||||
@@ -1,19 +1,23 @@
|
||||
pub mod error;
|
||||
pub mod html_state;
|
||||
mod middleware_analytics;
|
||||
mod middleware_auth;
|
||||
mod routes;
|
||||
mod template_response;
|
||||
|
||||
use axum::{
|
||||
extract::FromRef,
|
||||
middleware::from_fn_with_state,
|
||||
middleware::{from_fn_with_state, map_response_with_state},
|
||||
routing::{delete, get, patch, post},
|
||||
Router,
|
||||
};
|
||||
use axum_session::SessionLayer;
|
||||
use axum_session_auth::{AuthConfig, AuthSessionLayer};
|
||||
use axum_session::{Session, SessionLayer};
|
||||
use axum_session_auth::{AuthConfig, AuthSession, AuthSessionLayer};
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use common::storage::types::user::User;
|
||||
use html_state::HtmlState;
|
||||
use middleware_analytics::analytics_middleware;
|
||||
use middleware_auth::require_auth;
|
||||
use routes::{
|
||||
account::{delete_account, set_api_key, show_account_page, update_timezone},
|
||||
admin_panel::{show_admin_panel, toggle_registration_status},
|
||||
@@ -39,26 +43,40 @@ use routes::{
|
||||
signup::{process_signup_and_show_verification, show_signup_form},
|
||||
};
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use template_response::with_template_response;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
/// Router for HTML endpoints
|
||||
pub type AuthSessionType = AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>;
|
||||
pub type SessionType = Session<SessionSurrealPool<Any>>;
|
||||
|
||||
/// Html routes
|
||||
pub fn html_routes<S>(app_state: &HtmlState) -> Router<S>
|
||||
where
|
||||
S: Clone + Send + Sync + 'static,
|
||||
HtmlState: FromRef<S>,
|
||||
{
|
||||
Router::new()
|
||||
// Public routes - no auth required
|
||||
let public_routes = Router::new()
|
||||
.route("/", get(index_handler))
|
||||
.route("/gdpr/accept", post(accept_gdpr))
|
||||
.route("/gdpr/deny", post(deny_gdpr))
|
||||
.route("/search", get(search_result_handler))
|
||||
.route("/signout", get(sign_out_user))
|
||||
.route("/signin", get(show_signin_form).post(authenticate_user))
|
||||
.route(
|
||||
"/signup",
|
||||
get(show_signup_form).post(process_signup_and_show_verification),
|
||||
)
|
||||
.route("/documentation", get(show_documentation_index))
|
||||
.route("/documentation/privacy-policy", get(show_privacy_policy))
|
||||
.route("/documentation/get-started", get(show_get_started))
|
||||
.route("/documentation/mobile-friendly", get(show_mobile_friendly))
|
||||
.nest_service("/assets", ServeDir::new("assets/"));
|
||||
|
||||
// Protected routes - auth required
|
||||
let protected_routes = Router::new()
|
||||
.route("/chat", get(show_chat_base).post(new_chat_user_message))
|
||||
.route("/initialized-chat", post(show_initialized_chat))
|
||||
.route("/chat/:id", get(show_existing_chat).post(new_user_message))
|
||||
.route("/chat/response-stream", get(get_response_stream))
|
||||
.route("/knowledge/:id", get(show_reference_tooltip))
|
||||
.route("/signout", get(sign_out_user))
|
||||
.route("/signin", get(show_signin_form).post(authenticate_user))
|
||||
.route(
|
||||
"/ingress-form",
|
||||
get(show_ingress_form).post(process_ingress_form),
|
||||
@@ -72,6 +90,9 @@ where
|
||||
"/content/:id",
|
||||
get(show_text_content_edit_form).patch(patch_text_content),
|
||||
)
|
||||
.route("/search", get(search_result_handler))
|
||||
.route("/chat/response-stream", get(get_response_stream))
|
||||
.route("/knowledge/:id", get(show_reference_tooltip))
|
||||
.route("/knowledge", get(show_knowledge_page))
|
||||
.route(
|
||||
"/knowledge-entity/:id",
|
||||
@@ -90,16 +111,17 @@ where
|
||||
.route("/set-api-key", post(set_api_key))
|
||||
.route("/update-timezone", patch(update_timezone))
|
||||
.route("/delete-account", delete(delete_account))
|
||||
.route(
|
||||
"/signup",
|
||||
get(show_signup_form).post(process_signup_and_show_verification),
|
||||
)
|
||||
.route("/documentation", get(show_documentation_index))
|
||||
.route("/documentation/privacy-policy", get(show_privacy_policy))
|
||||
.route("/documentation/get-started", get(show_get_started))
|
||||
.route("/documentation/mobile-friendly", get(show_mobile_friendly))
|
||||
.nest_service("/assets", ServeDir::new("assets/"))
|
||||
.route_layer(from_fn_with_state(app_state.clone(), require_auth));
|
||||
|
||||
// Combine routes and add common middleware
|
||||
Router::new()
|
||||
.merge(public_routes)
|
||||
.merge(protected_routes)
|
||||
.layer(from_fn_with_state(app_state.clone(), analytics_middleware))
|
||||
.layer(map_response_with_state(
|
||||
app_state.clone(),
|
||||
with_template_response,
|
||||
))
|
||||
.layer(
|
||||
AuthSessionLayer::<User, String, SessionSurrealPool<Any>, Surreal<Any>>::new(Some(
|
||||
app_state.db.client.clone(),
|
||||
|
||||
48
crates/html-router/src/middleware_auth.rs
Normal file
48
crates/html-router/src/middleware_auth.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequestParts, Request},
|
||||
http::request::Parts,
|
||||
middleware::Next,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use common::storage::types::user::User;
|
||||
|
||||
use crate::{template_response::TemplateResponse, AuthSessionType};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RequireUser(pub User);
|
||||
|
||||
// Implement FromRequestParts for RequireUser
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for RequireUser
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
parts
|
||||
.extensions
|
||||
.get::<User>()
|
||||
.cloned()
|
||||
.map(RequireUser)
|
||||
.ok_or_else(|| TemplateResponse::redirect("/signin").into_response())
|
||||
}
|
||||
}
|
||||
|
||||
// Auth middleware that adds the user to extensions
|
||||
pub async fn require_auth(auth: AuthSessionType, mut request: Request, next: Next) -> Response {
|
||||
// Check if user is authenticated
|
||||
match auth.current_user {
|
||||
Some(user) => {
|
||||
// Add user to request extensions
|
||||
request.extensions_mut().insert(user);
|
||||
// Continue to the handler
|
||||
next.run(request).await
|
||||
}
|
||||
None => {
|
||||
// Redirect to login
|
||||
TemplateResponse::redirect("/signin").into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,42 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, Uri},
|
||||
response::{IntoResponse, Redirect},
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::HxRedirect;
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use axum::{extract::State, response::IntoResponse, Form};
|
||||
use chrono_tz::TZ_VARIANTS;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
storage::types::user::User,
|
||||
use crate::{
|
||||
middleware_auth::RequireUser,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType,
|
||||
};
|
||||
use common::storage::types::user::User;
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
use crate::html_state::HtmlState;
|
||||
|
||||
use super::{render_block, render_template};
|
||||
|
||||
page_data!(AccountData, "auth/account_settings.html", {
|
||||
#[derive(Serialize)]
|
||||
pub struct AccountPageData {
|
||||
user: User,
|
||||
timezones: Vec<String>
|
||||
});
|
||||
timezones: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn show_account_page(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
let timezones = TZ_VARIANTS.iter().map(|tz| tz.to_string()).collect();
|
||||
|
||||
let output = render_template(
|
||||
AccountData::template_name(),
|
||||
AccountData { user, timezones },
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
Ok(TemplateResponse::new_template(
|
||||
"auth/account_settings.html",
|
||||
AccountPageData { user, timezones },
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn set_api_key(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
// Generate and set the API key
|
||||
let api_key = User::set_api_key(&user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
let api_key = User::set_api_key(&user.id, &state.db).await?;
|
||||
|
||||
// Clear the cache so new requests have access to the user with api key
|
||||
auth.cache_clear_user(user.id.to_string());
|
||||
|
||||
// Update the user's API key
|
||||
@@ -69,40 +46,28 @@ pub async fn set_api_key(
|
||||
};
|
||||
|
||||
// Render the API key section block
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"auth/account_settings.html",
|
||||
"api_key_section",
|
||||
AccountData {
|
||||
AccountPageData {
|
||||
user: updated_user,
|
||||
timezones: vec![],
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn delete_account(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
state
|
||||
.db
|
||||
.delete_item::<User>(&user.id)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
state.db.delete_item::<User>(&user.id).await?;
|
||||
|
||||
auth.logout_user();
|
||||
|
||||
auth.session.destroy();
|
||||
|
||||
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
|
||||
Ok(TemplateResponse::redirect("/"))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -112,18 +77,13 @@ pub struct UpdateTimezoneForm {
|
||||
|
||||
pub async fn update_timezone(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
Form(form): Form<UpdateTimezoneForm>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
User::update_timezone(&user.id, &form.timezone, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
User::update_timezone(&user.id, &form.timezone, &state.db).await?;
|
||||
|
||||
// Clear the cache
|
||||
auth.cache_clear_user(user.id.to_string());
|
||||
|
||||
// Update the user's API key
|
||||
@@ -135,15 +95,12 @@ pub async fn update_timezone(
|
||||
let timezones = TZ_VARIANTS.iter().map(|tz| tz.to_string()).collect();
|
||||
|
||||
// Render the API key section block
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"auth/account_settings.html",
|
||||
"timezone_section",
|
||||
AccountData {
|
||||
AccountPageData {
|
||||
user: updated_user,
|
||||
timezones,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,62 +1,39 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::{IntoResponse, Redirect},
|
||||
Form,
|
||||
use axum::{extract::State, response::IntoResponse, Form};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
middleware_auth::RequireUser,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use common::storage::types::{analytics::Analytics, system_settings::SystemSettings, user::User};
|
||||
|
||||
use common::{
|
||||
error::HtmlError,
|
||||
storage::types::{analytics::Analytics, system_settings::SystemSettings, user::User},
|
||||
};
|
||||
use crate::html_state::HtmlState;
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
|
||||
use super::{render_block, render_template};
|
||||
|
||||
page_data!(AdminPanelData, "auth/admin_panel.html", {
|
||||
#[derive(Serialize)]
|
||||
pub struct AdminPanelData {
|
||||
user: User,
|
||||
settings: SystemSettings,
|
||||
analytics: Analytics,
|
||||
users: i64,
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn show_admin_panel(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated and admin
|
||||
let user = match auth.current_user {
|
||||
Some(user) if user.admin => user,
|
||||
_ => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
let settings = SystemSettings::get_current(&state.db).await?;
|
||||
let analytics = Analytics::get_current(&state.db).await?;
|
||||
let users_count = Analytics::get_users_amount(&state.db).await?;
|
||||
|
||||
let settings = SystemSettings::get_current(&state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
let analytics = Analytics::get_current(&state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
let users_count = Analytics::get_users_amount(&state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
let output = render_template(
|
||||
AdminPanelData::template_name(),
|
||||
Ok(TemplateResponse::new_template(
|
||||
"auth/admin_panel.html",
|
||||
AdminPanelData {
|
||||
user,
|
||||
settings,
|
||||
analytics,
|
||||
users: users_count,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
fn checkbox_to_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
|
||||
@@ -83,36 +60,28 @@ pub struct RegistrationToggleData {
|
||||
|
||||
pub async fn toggle_registration_status(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
Form(input): Form<RegistrationToggleInput>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated and admin
|
||||
let _user = match auth.current_user {
|
||||
Some(user) if user.admin => user,
|
||||
_ => return Ok(Redirect::to("/").into_response()),
|
||||
// Early return if the user is not admin
|
||||
if !user.admin {
|
||||
return Ok(TemplateResponse::redirect("/"));
|
||||
};
|
||||
|
||||
let current_settings = SystemSettings::get_current(&state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
let current_settings = SystemSettings::get_current(&state.db).await?;
|
||||
|
||||
let new_settings = SystemSettings {
|
||||
registrations_enabled: input.registration_open,
|
||||
..current_settings.clone()
|
||||
};
|
||||
|
||||
SystemSettings::update(&state.db, new_settings.clone())
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
SystemSettings::update(&state.db, new_settings.clone()).await?;
|
||||
|
||||
let output = render_block(
|
||||
AdminPanelData::template_name(),
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"auth/admin_panel.html",
|
||||
"registration_status_input",
|
||||
RegistrationToggleData {
|
||||
settings: new_settings,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
@@ -10,6 +10,12 @@ use axum::{
|
||||
};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use composite_retrieval::{
|
||||
answer_retrieval::{
|
||||
create_chat_request, create_user_message, format_entities_json, LLMResponseFormat,
|
||||
},
|
||||
retrieve_entities,
|
||||
};
|
||||
use futures::{
|
||||
stream::{self, once},
|
||||
Stream, StreamExt, TryStreamExt,
|
||||
@@ -21,19 +27,11 @@ use surrealdb::{engine::any::Any, Surreal};
|
||||
use tokio::sync::{mpsc::channel, Mutex};
|
||||
use tracing::{error, info};
|
||||
|
||||
use common::{
|
||||
retrieval::{
|
||||
combined_knowledge_entity_retrieval,
|
||||
query_helper::{
|
||||
create_chat_request, create_user_message, format_entities_json, LLMResponseFormat,
|
||||
},
|
||||
},
|
||||
storage::{
|
||||
db::SurrealDbClient,
|
||||
types::{
|
||||
message::{Message, MessageRole},
|
||||
user::User,
|
||||
},
|
||||
use common::storage::{
|
||||
db::SurrealDbClient,
|
||||
types::{
|
||||
message::{Message, MessageRole},
|
||||
user::User,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -100,7 +98,7 @@ pub async fn get_response_stream(
|
||||
};
|
||||
|
||||
// 2. Retrieve knowledge entities
|
||||
let entities = match combined_knowledge_entity_retrieval(
|
||||
let entities = match retrieve_entities(
|
||||
&state.db,
|
||||
&state.openai_client,
|
||||
&user_message.content,
|
||||
|
||||
@@ -12,8 +12,9 @@ use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use crate::routes::HtmlError;
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
error::AppError,
|
||||
storage::types::{
|
||||
conversation::Conversation,
|
||||
message::{Message, MessageRole},
|
||||
|
||||
@@ -8,8 +8,9 @@ use serde::Serialize;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use crate::routes::HtmlError;
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
error::AppError,
|
||||
storage::types::{knowledge_entity::KnowledgeEntity, user::User},
|
||||
};
|
||||
|
||||
|
||||
@@ -6,12 +6,9 @@ use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
|
||||
use common::{
|
||||
error::HtmlError,
|
||||
storage::types::{text_content::TextContent, user::User},
|
||||
};
|
||||
use common::storage::types::{text_content::TextContent, user::User};
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
use crate::{error::HtmlError, html_state::HtmlState, page_data};
|
||||
|
||||
use super::render_template;
|
||||
|
||||
|
||||
@@ -1,78 +1,54 @@
|
||||
use axum::{extract::State, response::IntoResponse};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use axum::response::IntoResponse;
|
||||
use common::storage::types::user::User;
|
||||
use serde::Serialize;
|
||||
|
||||
use common::{error::HtmlError, storage::types::user::User};
|
||||
use crate::template_response::{HtmlError, TemplateResponse};
|
||||
use crate::AuthSessionType;
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
|
||||
use super::render_template;
|
||||
|
||||
page_data!(DocumentationData, "do_not_use_this", {
|
||||
#[derive(Serialize)]
|
||||
pub struct DocumentationPageData {
|
||||
user: Option<User>,
|
||||
current_path: String
|
||||
});
|
||||
|
||||
pub async fn show_privacy_policy(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let output = render_template(
|
||||
"documentation/privacy.html",
|
||||
DocumentationData {
|
||||
user: auth.current_user,
|
||||
current_path: "/privacy_policy".to_string(),
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
current_path: String,
|
||||
}
|
||||
|
||||
pub async fn show_get_started(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let output = render_template(
|
||||
pub async fn show_privacy_policy(auth: AuthSessionType) -> Result<impl IntoResponse, HtmlError> {
|
||||
Ok(TemplateResponse::new_template(
|
||||
"documentation/privacy.html",
|
||||
DocumentationPageData {
|
||||
user: auth.current_user,
|
||||
current_path: "/privacy-policy".to_string(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn show_get_started(auth: AuthSessionType) -> Result<impl IntoResponse, HtmlError> {
|
||||
Ok(TemplateResponse::new_template(
|
||||
"documentation/get_started.html",
|
||||
DocumentationData {
|
||||
DocumentationPageData {
|
||||
user: auth.current_user,
|
||||
current_path: "/get-started".to_string(),
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
pub async fn show_mobile_friendly(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let output = render_template(
|
||||
|
||||
pub async fn show_mobile_friendly(auth: AuthSessionType) -> Result<impl IntoResponse, HtmlError> {
|
||||
Ok(TemplateResponse::new_template(
|
||||
"documentation/mobile_friendly.html",
|
||||
DocumentationData {
|
||||
DocumentationPageData {
|
||||
user: auth.current_user,
|
||||
current_path: "/mobile-friendly".to_string(),
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn show_documentation_index(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
auth: AuthSessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let output = render_template(
|
||||
Ok(TemplateResponse::new_template(
|
||||
"documentation/index.html",
|
||||
DocumentationData {
|
||||
DocumentationPageData {
|
||||
user: auth.current_user,
|
||||
current_path: "/index".to_string(),
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
use axum::response::{Html, IntoResponse};
|
||||
use axum_session::Session;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::engine::any::Any;
|
||||
|
||||
use common::error::HtmlError;
|
||||
use crate::SessionType;
|
||||
|
||||
pub async fn accept_gdpr(
|
||||
session: Session<SessionSurrealPool<Any>>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
pub async fn accept_gdpr(session: SessionType) -> impl IntoResponse {
|
||||
session.set("gdpr_accepted", true);
|
||||
|
||||
Ok(Html("").into_response())
|
||||
Html("").into_response()
|
||||
}
|
||||
|
||||
pub async fn deny_gdpr(
|
||||
session: Session<SessionSurrealPool<Any>>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
pub async fn deny_gdpr(session: SessionType) -> impl IntoResponse {
|
||||
session.set("gdpr_accepted", true);
|
||||
|
||||
Ok(Html("").into_response())
|
||||
Html("").into_response()
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Path, State},
|
||||
response::{IntoResponse, Redirect},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use axum_session::Session;
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use serde::Serialize;
|
||||
use tokio::join;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
middleware_auth::RequireUser,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType, SessionType,
|
||||
};
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
error::AppError,
|
||||
storage::types::{
|
||||
file_info::FileInfo, ingestion_task::IngestionTask, knowledge_entity::KnowledgeEntity,
|
||||
knowledge_relationship::KnowledgeRelationship, text_chunk::TextChunk,
|
||||
@@ -18,67 +20,51 @@ use common::{
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{html_state::HtmlState, page_data, routes::render_template};
|
||||
use crate::html_state::HtmlState;
|
||||
|
||||
use super::render_block;
|
||||
|
||||
page_data!(IndexData, "index/index.html", {
|
||||
#[derive(Serialize)]
|
||||
pub struct IndexPageData {
|
||||
gdpr_accepted: bool,
|
||||
user: Option<User>,
|
||||
latest_text_contents: Vec<TextContent>,
|
||||
active_jobs: Vec<IngestionTask>
|
||||
});
|
||||
active_jobs: Vec<IngestionTask>,
|
||||
}
|
||||
|
||||
pub async fn index_handler(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
session: Session<SessionSurrealPool<Any>>,
|
||||
auth: AuthSessionType,
|
||||
session: SessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
info!("Displaying index page");
|
||||
|
||||
let gdpr_accepted = auth.current_user.is_some() | session.get("gdpr_accepted").unwrap_or(false);
|
||||
|
||||
let active_jobs = match auth.current_user.is_some() {
|
||||
true => {
|
||||
User::get_unfinished_ingestion_tasks(&auth.current_user.clone().unwrap().id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?
|
||||
.await?
|
||||
}
|
||||
false => vec![],
|
||||
};
|
||||
|
||||
let latest_text_contents = match auth.current_user.clone().is_some() {
|
||||
true => User::get_latest_text_contents(
|
||||
auth.current_user.clone().unwrap().id.as_str(),
|
||||
&state.db,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?,
|
||||
true => {
|
||||
User::get_latest_text_contents(
|
||||
auth.current_user.clone().unwrap().id.as_str(),
|
||||
&state.db,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
false => vec![],
|
||||
};
|
||||
|
||||
// let latest_knowledge_entities = match auth.current_user.is_some() {
|
||||
// true => User::get_latest_knowledge_entities(
|
||||
// auth.current_user.clone().unwrap().id.as_str(),
|
||||
// &state.db,
|
||||
// )
|
||||
// .await
|
||||
// .map_err(|e| HtmlError::new(e, state.templates.clone()))?,
|
||||
// false => vec![],
|
||||
// };
|
||||
|
||||
let output = render_template(
|
||||
IndexData::template_name(),
|
||||
IndexData {
|
||||
Ok(TemplateResponse::new_template(
|
||||
"index/index.html",
|
||||
IndexPageData {
|
||||
gdpr_accepted,
|
||||
user: auth.current_user,
|
||||
latest_text_contents,
|
||||
active_jobs,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -87,21 +73,17 @@ pub struct LatestTextContentData {
|
||||
user: User,
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn delete_text_content(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
Path(id): Path<String>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
// Get and validate TextContent
|
||||
let text_content = get_and_validate_text_content(&state, &id, user).await?;
|
||||
let text_content = get_and_validate_text_content(&state, &id, &user).await?;
|
||||
|
||||
// Perform concurrent deletions
|
||||
let deletion_tasks = join!(
|
||||
join!(
|
||||
async {
|
||||
if let Some(file_info) = text_content.file_info {
|
||||
FileInfo::delete_by_id(&file_info.id, &state.db).await
|
||||
@@ -115,33 +97,17 @@ pub async fn delete_text_content(
|
||||
KnowledgeRelationship::delete_relationships_by_source_id(&text_content.id, &state.db)
|
||||
);
|
||||
|
||||
// Handle potential errors from concurrent operations
|
||||
match deletion_tasks {
|
||||
(Ok(_), Ok(_), Ok(_), Ok(_), Ok(_)) => (),
|
||||
_ => {
|
||||
return Err(HtmlError::new(
|
||||
AppError::Processing("Failed to delete one or more items".to_string()),
|
||||
state.templates.clone(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// Render updated content
|
||||
let latest_text_contents = User::get_latest_text_contents(&user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
let latest_text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
|
||||
|
||||
let output = render_block(
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"index/signed_in/recent_content.html",
|
||||
"latest_content_section",
|
||||
LatestTextContentData {
|
||||
user: user.clone(),
|
||||
user: user.to_owned(),
|
||||
latest_text_contents,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
// Helper function to get and validate text content
|
||||
@@ -149,23 +115,16 @@ async fn get_and_validate_text_content(
|
||||
state: &HtmlState,
|
||||
id: &str,
|
||||
user: &User,
|
||||
) -> Result<TextContent, HtmlError> {
|
||||
) -> Result<TextContent, AppError> {
|
||||
let text_content = state
|
||||
.db
|
||||
.get_item::<TextContent>(id)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?
|
||||
.ok_or_else(|| {
|
||||
HtmlError::new(
|
||||
AppError::NotFound("No item found".to_string()),
|
||||
state.templates.clone(),
|
||||
)
|
||||
})?;
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFound("Item was not found".to_string()))?;
|
||||
|
||||
if text_content.user_id != user.id {
|
||||
return Err(HtmlError::new(
|
||||
AppError::Auth("You are not the owner of that content".to_string()),
|
||||
state.templates.clone(),
|
||||
return Err(AppError::Auth(
|
||||
"You are not the owner of that content".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -180,57 +139,35 @@ pub struct ActiveJobsData {
|
||||
|
||||
pub async fn delete_job(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
Path(id): Path<String>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/signin").into_response()),
|
||||
};
|
||||
User::validate_and_delete_job(&id, &user.id, &state.db).await?;
|
||||
|
||||
User::validate_and_delete_job(&id, &user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
let active_jobs = User::get_unfinished_ingestion_tasks(&user.id, &state.db).await?;
|
||||
|
||||
let active_jobs = User::get_unfinished_ingestion_tasks(&user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
let output = render_block(
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"index/signed_in/active_jobs.html",
|
||||
"active_jobs_section",
|
||||
ActiveJobsData {
|
||||
user: user.clone(),
|
||||
active_jobs,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn show_active_jobs(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/signin").into_response()),
|
||||
};
|
||||
let active_jobs = User::get_unfinished_ingestion_tasks(&user.id, &state.db).await?;
|
||||
|
||||
let active_jobs = User::get_unfinished_ingestion_tasks(&user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
let output = render_block(
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"index/signed_in/active_jobs.html",
|
||||
"active_jobs_section",
|
||||
ActiveJobsData {
|
||||
user: user.clone(),
|
||||
active_jobs,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use tempfile::NamedTempFile;
|
||||
use tracing::info;
|
||||
|
||||
use common::{
|
||||
error::{AppError, HtmlError, IntoHtmlError},
|
||||
error::AppError,
|
||||
storage::types::{
|
||||
file_info::FileInfo, ingestion_payload::IngestionPayload, ingestion_task::IngestionTask,
|
||||
user::User,
|
||||
@@ -19,6 +19,7 @@ use common::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
error::{HtmlError, IntoHtmlError},
|
||||
html_state::HtmlState,
|
||||
page_data,
|
||||
routes::{index::ActiveJobsData, render_block},
|
||||
|
||||
@@ -14,7 +14,7 @@ use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
error::AppError,
|
||||
storage::types::{
|
||||
knowledge_entity::{KnowledgeEntity, KnowledgeEntityType},
|
||||
knowledge_relationship::KnowledgeRelationship,
|
||||
@@ -22,7 +22,7 @@ use common::{
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{html_state::HtmlState, page_data, routes::render_template};
|
||||
use crate::{error::HtmlError, html_state::HtmlState, page_data, routes::render_template};
|
||||
|
||||
page_data!(KnowledgeBaseData, "knowledge/base.html", {
|
||||
entities: Vec<KnowledgeEntity>,
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
||||
use axum::response::Html;
|
||||
use minijinja_autoreload::AutoReloader;
|
||||
|
||||
use common::error::{HtmlError, IntoHtmlError};
|
||||
use crate::error::{HtmlError, IntoHtmlError};
|
||||
|
||||
pub mod account;
|
||||
pub mod admin_panel;
|
||||
@@ -19,73 +19,73 @@ pub mod signin;
|
||||
pub mod signout;
|
||||
pub mod signup;
|
||||
|
||||
pub trait PageData {
|
||||
fn template_name() -> &'static str;
|
||||
}
|
||||
// pub trait PageData {
|
||||
// fn template_name() -> &'static str;
|
||||
// }
|
||||
|
||||
// Helper function for render_template
|
||||
pub fn render_template<T>(
|
||||
template_name: &str,
|
||||
context: T,
|
||||
templates: Arc<AutoReloader>,
|
||||
) -> Result<Html<String>, HtmlError>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
let env = templates
|
||||
.acquire_env()
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
let tmpl = env
|
||||
.get_template(template_name)
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
let context = minijinja::Value::from_serialize(&context);
|
||||
let output = tmpl
|
||||
.render(context)
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
Ok(Html(output))
|
||||
}
|
||||
// // Helper function for render_template
|
||||
// pub fn render_template<T>(
|
||||
// template_name: &str,
|
||||
// context: T,
|
||||
// templates: Arc<AutoReloader>,
|
||||
// ) -> Result<Html<String>, HtmlError>
|
||||
// where
|
||||
// T: serde::Serialize,
|
||||
// {
|
||||
// let env = templates
|
||||
// .acquire_env()
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
// let tmpl = env
|
||||
// .get_template(template_name)
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
// let context = minijinja::Value::from_serialize(&context);
|
||||
// let output = tmpl
|
||||
// .render(context)
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
// Ok(Html(output))
|
||||
// }
|
||||
|
||||
pub fn render_block<T>(
|
||||
template_name: &str,
|
||||
block: &str,
|
||||
context: T,
|
||||
templates: Arc<AutoReloader>,
|
||||
) -> Result<Html<String>, HtmlError>
|
||||
where
|
||||
T: serde::Serialize,
|
||||
{
|
||||
let env = templates
|
||||
.acquire_env()
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
let tmpl = env
|
||||
.get_template(template_name)
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
// pub fn render_block<T>(
|
||||
// template_name: &str,
|
||||
// block: &str,
|
||||
// context: T,
|
||||
// templates: Arc<AutoReloader>,
|
||||
// ) -> Result<Html<String>, HtmlError>
|
||||
// where
|
||||
// T: serde::Serialize,
|
||||
// {
|
||||
// let env = templates
|
||||
// .acquire_env()
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
// let tmpl = env
|
||||
// .get_template(template_name)
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
|
||||
let context = minijinja::Value::from_serialize(&context);
|
||||
let output = tmpl
|
||||
.eval_to_state(context)
|
||||
.map_err(|e| e.with_template(templates.clone()))?
|
||||
.render_block(block)
|
||||
.map_err(|e| e.with_template(templates.clone()))?;
|
||||
// let context = minijinja::Value::from_serialize(&context);
|
||||
// let output = tmpl
|
||||
// .eval_to_state(context)
|
||||
// .map_err(|e| e.with_template(templates.clone()))?
|
||||
// .render_block(block)
|
||||
// .map_err(|e| e.with_template(templates.clone()))?;
|
||||
|
||||
Ok(output.into())
|
||||
}
|
||||
// Ok(output.into())
|
||||
// }
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! page_data {
|
||||
($name:ident, $template_name:expr, {$($(#[$attr:meta])* $field:ident: $ty:ty),*$(,)?}) => {
|
||||
use serde::{Serialize, Deserialize};
|
||||
use $crate::routes::PageData;
|
||||
// #[macro_export]
|
||||
// macro_rules! page_data {
|
||||
// ($name:ident, $template_name:expr, {$($(#[$attr:meta])* $field:ident: $ty:ty),*$(,)?}) => {
|
||||
// use serde::{Serialize, Deserialize};
|
||||
// use $crate::routes::PageData;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct $name {
|
||||
$($(#[$attr])* pub $field: $ty),*
|
||||
}
|
||||
// #[derive(Debug, Deserialize, Serialize)]
|
||||
// pub struct $name {
|
||||
// $($(#[$attr])* pub $field: $ty),*
|
||||
// }
|
||||
|
||||
impl PageData for $name {
|
||||
fn template_name() -> &'static str {
|
||||
$template_name
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
// impl PageData for $name {
|
||||
// fn template_name() -> &'static str {
|
||||
// $template_name
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
@@ -8,7 +8,8 @@ use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use common::{error::HtmlError, storage::types::user::User};
|
||||
use crate::routes::HtmlError;
|
||||
use common::storage::types::user::User;
|
||||
|
||||
use crate::{html_state::HtmlState, routes::render_template};
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, Uri},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
response::{Html, IntoResponse},
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::{HxBoosted, HxRedirect};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use axum_htmx::HxBoosted;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use common::{error::HtmlError, storage::types::user::User};
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
|
||||
use super::{render_block, render_template};
|
||||
use crate::{
|
||||
html_state::HtmlState,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType,
|
||||
};
|
||||
use common::storage::types::user::User;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct SignupParams {
|
||||
@@ -22,36 +20,26 @@ pub struct SignupParams {
|
||||
pub remember_me: Option<String>,
|
||||
}
|
||||
|
||||
page_data!(ShowSignInForm, "auth/signin_form.html", {});
|
||||
|
||||
pub async fn show_signin_form(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
auth: AuthSessionType,
|
||||
HxBoosted(boosted): HxBoosted,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
if auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
return Ok(TemplateResponse::redirect("/"));
|
||||
}
|
||||
let output = match boosted {
|
||||
true => render_block(
|
||||
ShowSignInForm::template_name(),
|
||||
match boosted {
|
||||
true => Ok(TemplateResponse::new_partial(
|
||||
"auth/signin_form.html",
|
||||
"body",
|
||||
ShowSignInForm {},
|
||||
state.templates.clone(),
|
||||
)?,
|
||||
false => render_template(
|
||||
ShowSignInForm::template_name(),
|
||||
ShowSignInForm {},
|
||||
state.templates.clone(),
|
||||
)?,
|
||||
};
|
||||
|
||||
Ok(output.into_response())
|
||||
{},
|
||||
)),
|
||||
false => Ok(TemplateResponse::new_template("auth/signin_form.html", {})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn authenticate_user(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
auth: AuthSessionType,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match User::authenticate(form.email, form.password, &state.db).await {
|
||||
@@ -67,5 +55,5 @@ pub async fn authenticate_user(
|
||||
auth.remember_user(true);
|
||||
}
|
||||
|
||||
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
|
||||
Ok(TemplateResponse::redirect("/").into_response())
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
use axum::response::{IntoResponse, Redirect};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
use common::{error::ApiError, storage::types::user::User};
|
||||
use crate::{
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType,
|
||||
};
|
||||
|
||||
pub async fn sign_out_user(
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
pub async fn sign_out_user(auth: AuthSessionType) -> Result<impl IntoResponse, HtmlError> {
|
||||
if !auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
return Ok(TemplateResponse::redirect("/"));
|
||||
}
|
||||
|
||||
auth.logout_user();
|
||||
|
||||
Ok(Redirect::to("/").into_response())
|
||||
Ok(TemplateResponse::redirect("/"))
|
||||
}
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, Uri},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
response::{Html, IntoResponse},
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::{HxBoosted, HxRedirect};
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use axum_htmx::HxBoosted;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
|
||||
use common::{error::HtmlError, storage::types::user::User};
|
||||
use common::storage::types::user::User;
|
||||
|
||||
use crate::html_state::HtmlState;
|
||||
|
||||
use super::{render_block, render_template};
|
||||
use crate::{
|
||||
html_state::HtmlState,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType,
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct SignupParams {
|
||||
@@ -24,24 +22,26 @@ pub struct SignupParams {
|
||||
}
|
||||
|
||||
pub async fn show_signup_form(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
auth: AuthSessionType,
|
||||
HxBoosted(boosted): HxBoosted,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
if auth.is_authenticated() {
|
||||
return Ok(Redirect::to("/").into_response());
|
||||
return Ok(TemplateResponse::redirect("/"));
|
||||
}
|
||||
let output = match boosted {
|
||||
true => render_block("auth/signup_form.html", "body", {}, state.templates.clone())?,
|
||||
false => render_template("auth/signup_form.html", {}, state.templates.clone())?,
|
||||
};
|
||||
|
||||
Ok(output.into_response())
|
||||
match boosted {
|
||||
true => Ok(TemplateResponse::new_partial(
|
||||
"auth/signup_form.html",
|
||||
"body",
|
||||
{},
|
||||
)),
|
||||
false => Ok(TemplateResponse::new_template("auth/signup_form.html", {})),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn process_signup_and_show_verification(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
auth: AuthSessionType,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match User::create_new(form.email, form.password, &state.db, form.timezone).await {
|
||||
@@ -54,5 +54,5 @@ pub async fn process_signup_and_show_verification(
|
||||
|
||||
auth.login_user(user.id);
|
||||
|
||||
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
|
||||
Ok(TemplateResponse::redirect("/").into_response())
|
||||
}
|
||||
|
||||
288
crates/html-router/src/template_response.rs
Normal file
288
crates/html-router/src/template_response.rs
Normal file
@@ -0,0 +1,288 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
Extension,
|
||||
};
|
||||
use common::error::AppError;
|
||||
use minijinja::{context, Value};
|
||||
use minijinja_autoreload::AutoReloader;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{html_state::HtmlState, AuthSessionType};
|
||||
|
||||
// Enum for template types
|
||||
#[derive(Clone)]
|
||||
pub enum TemplateKind {
|
||||
Full(String), // Full page template
|
||||
Partial(String, String), // Template name, block name
|
||||
Error(StatusCode), // Error template with status code
|
||||
Redirect(axum::response::Redirect), // Redirect
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TemplateResponse {
|
||||
template_kind: TemplateKind,
|
||||
context: Value,
|
||||
}
|
||||
|
||||
impl TemplateResponse {
|
||||
pub fn new_template<T: Serialize>(name: impl Into<String>, context: T) -> Self {
|
||||
Self {
|
||||
template_kind: TemplateKind::Full(name.into()),
|
||||
context: Value::from_serialize(&context),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_partial<T: Serialize>(
|
||||
template: impl Into<String>,
|
||||
block: impl Into<String>,
|
||||
context: T,
|
||||
) -> Self {
|
||||
Self {
|
||||
template_kind: TemplateKind::Partial(template.into(), block.into()),
|
||||
context: Value::from_serialize(&context),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(status: StatusCode, title: &str, error: &str, description: &str) -> Self {
|
||||
let ctx = context! {
|
||||
status_code => status.as_u16(),
|
||||
title => title,
|
||||
error => error,
|
||||
description => description
|
||||
};
|
||||
|
||||
Self {
|
||||
template_kind: TemplateKind::Error(status),
|
||||
context: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience methods for common errors
|
||||
pub fn not_found() -> Self {
|
||||
Self::error(
|
||||
StatusCode::NOT_FOUND,
|
||||
"Page Not Found",
|
||||
"Not Found",
|
||||
"The page you're looking for doesn't exist or was removed.",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn server_error() -> Self {
|
||||
Self::error(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Internal Server Error",
|
||||
"Internal Server Error",
|
||||
"Something went wrong on our end.",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn unauthorized() -> Self {
|
||||
Self::error(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Unauthorized",
|
||||
"Access Denied",
|
||||
"You need to be logged in to access this page.",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bad_request(message: &str) -> Self {
|
||||
Self::error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Bad Request",
|
||||
"Bad Request",
|
||||
message,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn redirect(path: impl AsRef<str>) -> Self {
|
||||
let redirect_response = axum::response::Redirect::to(path.as_ref());
|
||||
|
||||
Self {
|
||||
template_kind: TemplateKind::Redirect(redirect_response),
|
||||
context: Value::from_serialize(&()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for TemplateResponse {
|
||||
fn into_response(self) -> Response {
|
||||
Extension(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper to avoid recursion
|
||||
struct TemplateStateWrapper {
|
||||
state: HtmlState,
|
||||
auth: AuthSessionType,
|
||||
template_response: TemplateResponse,
|
||||
}
|
||||
|
||||
impl IntoResponse for TemplateStateWrapper {
|
||||
fn into_response(self) -> Response {
|
||||
let templates = self.state.templates;
|
||||
|
||||
match &self.template_response.template_kind {
|
||||
TemplateKind::Full(name) => {
|
||||
render_template(name, self.template_response.context, templates)
|
||||
}
|
||||
TemplateKind::Partial(name, block) => {
|
||||
render_block(name, block, self.template_response.context, templates)
|
||||
}
|
||||
TemplateKind::Error(status) => {
|
||||
let html = match try_render_template(
|
||||
"errors/error.html",
|
||||
self.template_response.context,
|
||||
templates,
|
||||
) {
|
||||
Ok(html_string) => Html(html_string),
|
||||
Err(_) => fallback_error(),
|
||||
};
|
||||
(*status, html).into_response()
|
||||
}
|
||||
TemplateKind::Redirect(redirect) => redirect.clone().into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions for rendering with error handling
|
||||
fn render_template(name: &str, context: Value, templates: Arc<AutoReloader>) -> Response {
|
||||
match try_render_template(name, context, templates.clone()) {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(_) => fallback_error().into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_block(name: &str, block: &str, context: Value, templates: Arc<AutoReloader>) -> Response {
|
||||
match try_render_block(name, block, context, templates.clone()) {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(_) => fallback_error().into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
fn try_render_template(
|
||||
template_name: &str,
|
||||
context: Value,
|
||||
templates: Arc<AutoReloader>,
|
||||
) -> Result<String, ()> {
|
||||
let env = templates.acquire_env().map_err(|e| {
|
||||
tracing::error!("Environment error: {:?}", e);
|
||||
()
|
||||
})?;
|
||||
|
||||
let tmpl = env.get_template(template_name).map_err(|e| {
|
||||
tracing::error!("Template error: {:?}", e);
|
||||
()
|
||||
})?;
|
||||
|
||||
tmpl.render(context).map_err(|e| {
|
||||
tracing::error!("Render error: {:?}", e);
|
||||
()
|
||||
})
|
||||
}
|
||||
|
||||
fn try_render_block(
|
||||
template_name: &str,
|
||||
block: &str,
|
||||
context: Value,
|
||||
templates: Arc<AutoReloader>,
|
||||
) -> Result<String, ()> {
|
||||
let env = templates.acquire_env().map_err(|e| {
|
||||
tracing::error!("Environment error: {:?}", e);
|
||||
()
|
||||
})?;
|
||||
|
||||
let tmpl = env.get_template(template_name).map_err(|e| {
|
||||
tracing::error!("Template error: {:?}", e);
|
||||
()
|
||||
})?;
|
||||
|
||||
let mut state = tmpl.eval_to_state(context).map_err(|e| {
|
||||
tracing::error!("Eval error: {:?}", e);
|
||||
()
|
||||
})?;
|
||||
|
||||
state.render_block(block).map_err(|e| {
|
||||
tracing::error!("Block render error: {:?}", e);
|
||||
()
|
||||
})
|
||||
}
|
||||
|
||||
fn fallback_error() -> Html<String> {
|
||||
Html(
|
||||
r#"
|
||||
<html>
|
||||
<body>
|
||||
<div class="container mx-auto p-4">
|
||||
<h1 class="text-4xl text-error">Error</h1>
|
||||
<p class="mt-4">Sorry, something went wrong displaying this page.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"#
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn with_template_response(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSessionType,
|
||||
response: Response,
|
||||
) -> Response {
|
||||
// Clone the TemplateResponse from extensions
|
||||
let template_response = response.extensions().get::<TemplateResponse>().cloned();
|
||||
|
||||
if let Some(template_response) = template_response {
|
||||
TemplateStateWrapper {
|
||||
state,
|
||||
auth,
|
||||
template_response,
|
||||
}
|
||||
.into_response()
|
||||
} else {
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
// Define HtmlError
|
||||
pub enum HtmlError {
|
||||
AppError(AppError),
|
||||
TemplateError(String),
|
||||
}
|
||||
|
||||
// Conversion from AppError to HtmlError
|
||||
impl From<AppError> for HtmlError {
|
||||
fn from(err: AppError) -> Self {
|
||||
HtmlError::AppError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Conversion for database error to HtmlError
|
||||
impl From<surrealdb::Error> for HtmlError {
|
||||
fn from(err: surrealdb::Error) -> Self {
|
||||
HtmlError::AppError(AppError::from(err))
|
||||
}
|
||||
}
|
||||
|
||||
// Now implement IntoResponse for HtmlError
|
||||
impl IntoResponse for HtmlError {
|
||||
fn into_response(self) -> Response {
|
||||
match self {
|
||||
HtmlError::AppError(err) => {
|
||||
let template_response = match err {
|
||||
AppError::NotFound(_) => TemplateResponse::not_found(),
|
||||
AppError::Auth(_) => TemplateResponse::unauthorized(),
|
||||
AppError::Validation(msg) => TemplateResponse::bad_request(&msg),
|
||||
_ => {
|
||||
tracing::error!("Internal error: {:?}", err);
|
||||
TemplateResponse::server_error()
|
||||
}
|
||||
};
|
||||
template_response.into_response()
|
||||
}
|
||||
HtmlError::TemplateError(_) => TemplateResponse::server_error().into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user