Files
sorsfonalak/services/backend/src/routes/mod.rs
T
fecoandClaude Opus 5.5 450174c273 Free chart summaries; password login off by default
- New analysis kind 'summary' (migration 0005): saving a chart queues a
  free 3-4 sentence summary written by SUMMARY_MODEL (default Sonnet
  5.5, about 1 cent; Haiku was tested too but chose weaker highlights).
  POST /v1/charts/{id}/summary returns it and re-queues a failed one.
- The chart page shows it in a "Röviden" card and polls until ready.
- PASSWORD_LOGIN (default false): register, login and password reset
  answer 403; only Google (later Apple, Facebook) logins remain.
- Prompt v2: Hungarian names for elements and modalities (an English
  "Fire" slipped into a summary); worker compare-models --summary.
- The worker also reads a lower-case openrouter_api_key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-16 17:12:17 +00:00

126 lines
5.3 KiB
Rust

//! 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<T> = WithRejection<Json<T>, ApiError>;
/// `{id}` path parameter.
pub type IdPath = WithRejection<Path<Uuid>, ApiError>;
pub type St = State<Arc<AppState>>;
/// For PATCH fields: missing = `None` (keep), `null` = `Some(None)` (clear).
pub fn double_option<'de, D, T>(d: D) -> Result<Option<Option<T>>, D::Error>
where
D: serde::Deserializer<'de>,
T: serde::Deserialize<'de>,
{
serde::Deserialize::deserialize(d).map(Some)
}
pub fn router(state: Arc<AppState>) -> 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<Json<Value>, 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<Value> {
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,
}))
}