//! HTTP routes. //! //! Guards: handlers that take [`AuthUser`](crate::auth::AuthUser) require a //! session, [`AdminUser`](crate::auth::AdminUser) also the admin role. //! Resources of other users answer 404, so their existence is not revealed. //! Everything under `/v1/public` is open (shared links, invitation landing). mod account; mod admin; mod analyses; mod auth; mod charts; mod shares; mod synastries; use std::sync::Arc; use axum::extract::{Path, State}; use axum::http::{HeaderValue, Method, header}; use axum::routing::{delete, get, patch, post}; use axum::{Json, Router}; use axum_extra::extract::WithRejection; use serde_json::{Value, json}; use tower_http::cors::{AllowOrigin, CorsLayer}; use tower_http::trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer}; use tracing::Level; use uuid::Uuid; use crate::AppState; use crate::error::ApiError; /// JSON request body with JSON error responses. pub type Body = WithRejection, ApiError>; /// `{id}` path parameter. pub type IdPath = WithRejection, ApiError>; pub type St = State>; /// For PATCH fields: missing = `None` (keep), `null` = `Some(None)` (clear). pub fn double_option<'de, D, T>(d: D) -> Result>, D::Error> where D: serde::Deserializer<'de>, T: serde::Deserialize<'de>, { serde::Deserialize::deserialize(d).map(Some) } pub fn router(state: Arc) -> Router { Router::new() .route("/healthz", get(health)) .route("/v1/config", get(public_config)) // login .route("/v1/auth/register", post(auth::register)) .route("/v1/auth/login", post(auth::login)) .route("/v1/auth/google", post(auth::google)) .route("/v1/auth/logout", post(auth::logout)) .route("/v1/auth/logout-all", post(auth::logout_all)) .route("/v1/auth/verify-email", post(auth::verify_email)) .route("/v1/auth/resend-verification", post(auth::resend_verification)) .route("/v1/auth/password-reset", post(auth::request_password_reset)) .route("/v1/auth/password-reset/confirm", post(auth::confirm_password_reset)) // own account (GDPR export and deletion included) .route("/v1/me", get(account::me).patch(account::update_me).delete(account::delete_me)) .route("/v1/me/password", post(account::change_password)) .route("/v1/me/export", get(account::export)) .route("/v1/credits", get(account::credits)) // charts .route("/v1/charts", get(charts::list).post(charts::create)) .route("/v1/charts/{id}", get(charts::get_one).patch(charts::update).delete(charts::remove)) .route("/v1/charts/{id}/analyses", get(analyses::list_for_chart).post(analyses::request_for_chart)) .route("/v1/charts/{id}/summary", post(analyses::summary_for_chart)) // synastries and invitations .route("/v1/synastries", get(synastries::list).post(synastries::create)) .route("/v1/synastries/{id}", get(synastries::get_one).delete(synastries::remove)) .route("/v1/synastries/{id}/analyses", get(analyses::list_for_synastry).post(analyses::request_for_synastry)) .route("/v1/invites", get(synastries::list_invites).post(synastries::create_invite)) .route("/v1/invites/{id}", delete(synastries::revoke_invite)) .route("/v1/invites/accept", post(synastries::accept_invite)) // analyses .route("/v1/analyses/{id}", get(analyses::get_one)) // sharing .route("/v1/shares", get(shares::list).post(shares::create)) .route("/v1/shares/{id}", delete(shares::revoke)) // public, no login .route("/v1/public/shares/{slug}", get(shares::public_view)) .route("/v1/public/invites/{token}", get(synastries::public_invite)) // administration .route("/v1/admin/users", get(admin::list_users)) .route("/v1/admin/users/{id}", patch(admin::update_user)) .route("/v1/admin/users/{id}/credits", post(admin::adjust_credits)) .layer(cors(&state.config.cors_allowed_origins)) .layer( TraceLayer::new_for_http() .make_span_with(DefaultMakeSpan::new().level(Level::INFO)) .on_response(DefaultOnResponse::new().level(Level::INFO)), ) .with_state(state) } /// Cookies are sent cross-origin only to the listed origins. fn cors(origins: &[String]) -> CorsLayer { CorsLayer::new() .allow_origin(AllowOrigin::list(origins.iter().filter_map(|o| HeaderValue::from_str(o).ok()))) .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE]) .allow_headers([header::CONTENT_TYPE, header::AUTHORIZATION]) .allow_credentials(true) } async fn health(State(state): St) -> Result, ApiError> { sqlx::query("SELECT 1").execute(&state.db).await?; Ok(Json(json!({ "status": "ok" }))) } /// What the frontend needs to know before login. async fn public_config(State(state): St) -> Json { let c = &state.config; Json(json!({ "google_login": { "enabled": state.google.enabled(), "client_id": c.google_client_ids.first() }, "locales": c.supported_locales, "llm_models": c.llm_models, "default_model": c.llm_models[0].id, "monthly_free_credits": c.monthly_free_credits, "password_login": c.password_login, "password_min_length": crate::auth::MIN_PASSWORD_LEN, })) }