56 lines
1.8 KiB
Rust
56 lines
1.8 KiB
Rust
//! Sorsfonalak forum: pseudonymous members, topics, threads, posts,
|
|||
|
|
//! reactions, read marks, followed threads and moderation (docs/kozosseg.md).
|
||
|
|
//!
|
||
|
|
//! A service of its own, so it can be scaled apart from the backend. It shares
|
||
|
|
//! the database and the login: a session of the backend (`sf_session` cookie
|
||
|
|
//! or `Authorization: Bearer`) is valid here too.
|
||
|
|
//!
|
||
|
|
//! Configuration: `DATABASE_URL`; `BIND_ADDR` (default `0.0.0.0:8083`),
|
||
|
|
//! `LOG_FORMAT`, `RUST_LOG`, `LOG_DIR`. Database migrations run at startup.
|
||
|
|
//!
|
||
|
|
//! `forum healthcheck` performs a local health check (for Docker).
|
||
|
|
|
||
|
|
mod auth;
|
||
|
|
mod error;
|
||
|
|
mod rank;
|
||
|
|
mod routes;
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests;
|
||
|
|
mod text;
|
||
|
|
|
||
|
|
use std::sync::Arc;
|
||
|
|
|
||
|
|
use anyhow::Context;
|
||
|
|
use sqlx::PgPool;
|
||
|
|
|
||
|
|
const DEFAULT_BIND_ADDR: &str = "0.0.0.0:8083";
|
||
|
|
|
||
|
|
pub struct AppState {
|
||
|
|
pub db: PgPool,
|
||
|
|
/// Seconds a member waits between two posts (0 in the tests).
|
||
|
|
pub flood_secs: i32,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AppState {
|
||
|
|
pub fn new(db: PgPool) -> Self {
|
||
|
|
Self { db, flood_secs: 5 }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[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("forum");
|
||
|
|
let url = std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?;
|
||
|
|
let pool = db::connect(&url).await.context("connecting to the database")?;
|
||
|
|
db::migrate(&pool).await.context("running database migrations")?;
|
||
|
|
let app = routes::router(Arc::new(AppState::new(pool)));
|
||
|
|
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(())
|
||
|
|
}
|