2026-11-15 10:25:35 +00:00
|
|
|
//! 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;
|
2026-11-17 06:45:33 +00:00
|
|
|
mod birth_changes;
|
2026-11-15 10:25:35 +00:00
|
|
|
mod config;
|
|
|
|
|
mod credits;
|
|
|
|
|
mod error;
|
2026-11-20 07:52:34 +00:00
|
|
|
mod invoices;
|
2026-11-18 13:15:57 +00:00
|
|
|
mod models;
|
2026-11-20 19:40:03 +00:00
|
|
|
mod oidc;
|
2026-11-17 16:19:20 +00:00
|
|
|
mod packages;
|
2026-11-17 11:03:55 +00:00
|
|
|
mod persons;
|
2026-11-15 10:25:35 +00:00
|
|
|
mod routes;
|
2026-11-20 06:41:21 +00:00
|
|
|
mod stripe;
|
2026-11-15 10:25:35 +00:00
|
|
|
#[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;
|
2026-11-20 19:40:03 +00:00
|
|
|
use crate::oidc::IdTokenVerifier;
|
2026-11-15 10:25:35 +00:00
|
|
|
|
|
|
|
|
const DEFAULT_BIND_ADDR: &str = "0.0.0.0:8081";
|
|
|
|
|
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
pub db: PgPool,
|
|
|
|
|
pub config: Config,
|
|
|
|
|
pub horoscope: HoroscopeClient,
|
2026-11-20 19:40:03 +00:00
|
|
|
pub google: IdTokenVerifier,
|
|
|
|
|
pub apple: IdTokenVerifier,
|
2026-11-15 10:25:35 +00:00
|
|
|
pub login_limiter: LoginLimiter,
|
2026-11-21 20:31:45 +00:00
|
|
|
/// 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,
|
2026-11-21 21:13:41 +00:00
|
|
|
/// 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,
|
2026-11-18 13:15:57 +00:00
|
|
|
pub models: models::Catalog,
|
2026-11-20 06:41:21 +00:00
|
|
|
pub stripe: stripe::Client,
|
2026-11-15 10:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
|
|
|
|
pub fn new(db: PgPool, config: Config) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
horoscope: HoroscopeClient::new(&config.horoscope_api_url),
|
2026-11-20 19:40:03 +00:00
|
|
|
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()),
|
2026-11-15 10:25:35 +00:00
|
|
|
login_limiter: LoginLimiter::new(10, Duration::from_secs(15 * 60)),
|
2026-11-21 20:31:45 +00:00
|
|
|
calculator_limiter: LoginLimiter::new(30, Duration::from_secs(60 * 60)),
|
|
|
|
|
places_limiter: LoginLimiter::new(300, Duration::from_secs(60 * 60)),
|
2026-11-21 21:13:41 +00:00
|
|
|
share_visits: LoginLimiter::new(1, Duration::from_secs(30 * 60)),
|
2026-11-18 13:15:57 +00:00
|
|
|
models: models::Catalog::new(&config.openrouter_models_url),
|
2026-11-20 06:41:21 +00:00
|
|
|
stripe: stripe::Client::default(),
|
2026-11-15 10:25:35 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2026-11-20 18:57:09 +00:00
|
|
|
let _log = service_kit::init_logging("backend");
|
2026-11-15 10:25:35 +00:00
|
|
|
let config = Config::from_env()?;
|
|
|
|
|
let pool = db::connect(&config.database_url).await.context("connecting to the database")?;
|
2026-11-15 21:00:50 +00:00
|
|
|
db::migrate(&pool).await.context("running database migrations")?;
|
2026-11-15 10:25:35 +00:00
|
|
|
tracing::info!(
|
|
|
|
|
google_login = !config.google_client_ids.is_empty(),
|
2026-11-20 19:40:03 +00:00
|
|
|
apple_login = !config.apple_client_ids.is_empty(),
|
2026-11-15 10:25:35 +00:00
|
|
|
horoscope_api = %config.horoscope_api_url,
|
|
|
|
|
"database ready"
|
|
|
|
|
);
|
2026-11-18 12:18:39 +00:00
|
|
|
if std::env::args().nth(1).as_deref() == Some("recalculate") {
|
|
|
|
|
return routes::recalculate(&AppState::new(pool, config)).await;
|
|
|
|
|
}
|
2026-11-15 10:25:35 +00:00
|
|
|
let app = routes::router(Arc::new(AppState::new(pool, config)));
|
|
|
|
|
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(())
|
|
|
|
|
}
|