wip: heavy refactoring html routers

This commit is contained in:
Per Stark
2025-03-08 15:47:44 +01:00
parent 812bce27d1
commit 89155130e6
50 changed files with 1130 additions and 987 deletions

View File

@@ -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())
))
}

View File

@@ -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())
))
}

View File

@@ -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,

View File

@@ -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},

View File

@@ -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},
};

View File

@@ -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;

View File

@@ -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())
))
}

View File

@@ -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()
}

View File

@@ -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())
))
}

View File

@@ -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},

View File

@@ -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>,

View File

@@ -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
// }
// }
// };
// }

View File

@@ -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)]

View File

@@ -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())
}

View File

@@ -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("/"))
}

View File

@@ -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())
}