Files
sorsfonalak/services/backend/src/main.rs
T
fecoandClaude Opus 5.5 37cb0abf4b
CI / test (push) Successful in 2m21s
CI / build (push) Skipped
CI / deploy (push) Skipped
Famous people: admin page, own prompt, one photo, public pages
- Admin "Híres emberek": birth data, one photo (S3: DigitalOcean Spaces in
  production, RustFS in development), a face point that every crop keeps
  in view, publishing, and a detailed portrait written with its own
  `celebrity` prompt (third person, by name, from the chart alone).
- The photo is stored once (upright JPEG, at most 2000 px, no metadata);
  next/image makes the sizes from it, so Cloudflare can cache those.
- Public list and pages (/hires-emberek), in the sitemap, with a share
  image showing the face and the chart.
- Share images: the wheel's glyphs and numbers are drawn with the card's
  fonts (inside the SVG image they came out as boxes, on every card).

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

112 lines
4.0 KiB
Rust

//! Sorsfonalak backend: users, login, charts, synastries, analyses, credits
//! and public sharing. Charts are calculated by the horoscope-api service.
//!
//! Configuration: see [`config::Config`]; plus `BIND_ADDR` (default
//! `0.0.0.0:8081`), `LOG_FORMAT` and `RUST_LOG`.
//! Database migrations run at startup.
//!
//! `backend healthcheck` performs a local health check (for Docker).
mod auth;
mod birth_changes;
mod config;
mod credits;
mod error;
mod invoices;
mod models;
mod oidc;
mod packages;
mod persons;
mod photo;
mod routes;
mod storage;
mod stripe;
#[cfg(test)]
mod tests;
mod views;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Context;
use horoscope_client::HoroscopeClient;
use sqlx::PgPool;
use crate::auth::LoginLimiter;
use crate::config::Config;
use crate::oidc::IdTokenVerifier;
const DEFAULT_BIND_ADDR: &str = "0.0.0.0:8081";
pub struct AppState {
pub db: PgPool,
pub config: Config,
pub horoscope: HoroscopeClient,
pub google: IdTokenVerifier,
pub apple: IdTokenVerifier,
pub login_limiter: LoginLimiter,
/// The public sign calculator, per visitor address.
pub calculator_limiter: LoginLimiter,
/// The public place search (typing makes several calls), per visitor address.
pub places_limiter: LoginLimiter,
/// Share visits already counted (link and visitor address, for half an hour):
/// a reload is not a new visit, without storing anything in the browser.
pub share_visits: LoginLimiter,
pub models: models::Catalog,
pub stripe: stripe::Client,
/// Uploaded photos; `None` when no object storage is configured.
pub storage: Option<storage::Storage>,
}
impl AppState {
pub fn new(db: PgPool, config: Config) -> Self {
Self {
horoscope: HoroscopeClient::new(&config.horoscope_api_url),
google: IdTokenVerifier::google(config.google_client_ids.clone(), config.google_jwks_url.clone()),
apple: IdTokenVerifier::apple(config.apple_client_ids.clone(), config.apple_jwks_url.clone()),
login_limiter: LoginLimiter::new(10, Duration::from_secs(15 * 60)),
calculator_limiter: LoginLimiter::new(30, Duration::from_secs(60 * 60)),
places_limiter: LoginLimiter::new(300, Duration::from_secs(60 * 60)),
share_visits: LoginLimiter::new(1, Duration::from_secs(30 * 60)),
models: models::Catalog::new(&config.openrouter_models_url),
stripe: stripe::Client::default(),
storage: config.storage.clone().map(storage::Storage::new),
db,
config,
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let addr = service_kit::bind_addr(DEFAULT_BIND_ADDR)?;
if std::env::args().nth(1).as_deref() == Some("healthcheck") {
return service_kit::healthcheck(addr);
}
let _log = service_kit::init_logging("backend");
let config = Config::from_env()?;
let pool = db::connect(&config.database_url).await.context("connecting to the database")?;
db::migrate(&pool).await.context("running database migrations")?;
tracing::info!(
google_login = !config.google_client_ids.is_empty(),
apple_login = !config.apple_client_ids.is_empty(),
horoscope_api = %config.horoscope_api_url,
"database ready"
);
if std::env::args().nth(1).as_deref() == Some("recalculate") {
return routes::recalculate(&AppState::new(pool, config)).await;
}
let state = AppState::new(pool, config);
if state.config.storage_create_bucket
&& let Some(storage) = &state.storage
&& let Err(e) = storage.ensure_bucket().await
{
tracing::warn!(error = %e, "creating the object storage bucket failed");
}
let app = routes::router(Arc::new(state));
let listener = tokio::net::TcpListener::bind(addr).await.with_context(|| format!("binding {addr}"))?;
tracing::info!(%addr, "listening");
axum::serve(listener, app).with_graceful_shutdown(service_kit::shutdown_signal()).await?;
Ok(())
}