74 lines
2.2 KiB
Rust
74 lines
2.2 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 config;
|
||
|
|
mod credits;
|
||
|
|
mod error;
|
||
|
|
mod google;
|
||
|
|
mod routes;
|
||
|
|
#[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::google::GoogleVerifier;
|
||
|
|
|
||
|
|
const DEFAULT_BIND_ADDR: &str = "0.0.0.0:8081";
|
||
|
|
|
||
|
|
pub struct AppState {
|
||
|
|
pub db: PgPool,
|
||
|
|
pub config: Config,
|
||
|
|
pub horoscope: HoroscopeClient,
|
||
|
|
pub google: GoogleVerifier,
|
||
|
|
pub login_limiter: LoginLimiter,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AppState {
|
||
|
|
pub fn new(db: PgPool, config: Config) -> Self {
|
||
|
|
Self {
|
||
|
|
horoscope: HoroscopeClient::new(&config.horoscope_api_url),
|
||
|
|
google: GoogleVerifier::new(config.google_client_ids.clone(), config.google_jwks_url.clone()),
|
||
|
|
login_limiter: LoginLimiter::new(10, Duration::from_secs(15 * 60)),
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
service_kit::init_logging();
|
||
|
|
let config = Config::from_env()?;
|
||
|
|
let pool = db::connect(&config.database_url).await.context("connecting to the database")?;
|
||
|
|
db::MIGRATOR.run(&pool).await.context("running database migrations")?;
|
||
|
|
tracing::info!(
|
||
|
|
google_login = !config.google_client_ids.is_empty(),
|
||
|
|
horoscope_api = %config.horoscope_api_url,
|
||
|
|
"database ready"
|
||
|
|
);
|
||
|
|
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(())
|
||
|
|
}
|