visa.buildfor.life keeps the immigration records of the foreigners B4L looks after. This first phase is the base the deadlines and extension packs build on (docs/PLAN.md, phases 2 to 7). - One Rust binary (axum 0.8, minijinja, sqlx on PostgreSQL) with its templates, static files and migrations compiled in. - Login through Kanidm (OpenID Connect with PKCE and a nonce, ES256), client visa-web, group visa; sessions in PostgreSQL keyed by the SHA-256 of the cookie. VISA_INSECURE_NO_LOGIN=1 for loopback development only. - Same-site check and form token on every POST; a CSP without inline script or style; security headers. - People: list with filter, page, create, edit, archive, restore. - Passports from the machine-readable zone with check digits (ICAO 9303 TD3); expiry warnings at 12 and 6 months. - Addresses in Thailand in the order Thai forms use, with the house master and the TM.30 receipt. - Append-only audit log of every view and change, with the names of the changed fields but never their values (PDPA); the Activity page. - Invented demo people (visa seed-demo, refused when login is on). - 40 tests: unit tests, pages with demo data, the forms, the audit log and the whole login against a fake OpenID provider. - CI: gitleaks over the history, fmt, clippy and the tests against PostgreSQL 18; the pre-commit secret scan.
116 lines
3.3 KiB
Rust
116 lines
3.3 KiB
Rust
//! `visa [serve|migrate|seed-demo|help]`: the server and its admin commands.
|
|
|
|
use std::process::ExitCode;
|
|
|
|
use anyhow::{Context, Result, bail, ensure};
|
|
use tracing::info;
|
|
|
|
use visa::config::Config;
|
|
use visa::{dates, db, seed, web};
|
|
|
|
const USAGE: &str = "\
|
|
visa: immigration records, deadlines and extension packs (visa.buildfor.life)
|
|
|
|
Usage: visa [COMMAND]
|
|
|
|
Commands:
|
|
serve run the web server (default); applies pending migrations first
|
|
migrate apply pending database migrations and exit
|
|
seed-demo add invented demo people (local development only)
|
|
help show this text
|
|
|
|
Settings come from the environment; see src/config.rs or README.md.";
|
|
|
|
#[tokio::main]
|
|
async fn main() -> ExitCode {
|
|
tracing_subscriber::fmt()
|
|
.with_max_level(tracing::Level::INFO)
|
|
.with_target(false)
|
|
.init();
|
|
let cmd = std::env::args().nth(1).unwrap_or_else(|| "serve".into());
|
|
match run(&cmd).await {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(e) => {
|
|
eprintln!("visa: {e:#}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn run(cmd: &str) -> Result<()> {
|
|
match cmd {
|
|
"serve" => serve().await,
|
|
"migrate" => {
|
|
let cfg = Config::from_env()?;
|
|
let pool = db::connect(&cfg).await?;
|
|
db::migrate(&pool).await?;
|
|
println!("migrations applied");
|
|
Ok(())
|
|
}
|
|
"seed-demo" => {
|
|
let cfg = Config::from_env()?;
|
|
ensure!(
|
|
!cfg.login_enabled(),
|
|
"seed-demo writes invented people and runs only in local development \
|
|
(VISA_INSECURE_NO_LOGIN=1, no VISA_OIDC_ISSUER)"
|
|
);
|
|
let pool = db::connect(&cfg).await?;
|
|
db::migrate(&pool).await?;
|
|
if seed::demo(&pool, dates::today()).await? {
|
|
println!("demo people added");
|
|
} else {
|
|
println!("demo people are there already");
|
|
}
|
|
Ok(())
|
|
}
|
|
"help" | "--help" | "-h" => {
|
|
println!("{USAGE}");
|
|
Ok(())
|
|
}
|
|
other => bail!("unknown command {other:?}; `visa help` lists the commands"),
|
|
}
|
|
}
|
|
|
|
async fn serve() -> Result<()> {
|
|
let cfg = Config::from_env()?;
|
|
let pool = db::connect(&cfg).await?;
|
|
db::migrate(&pool).await?;
|
|
let listen = cfg.listen;
|
|
let login = cfg.login_enabled();
|
|
let st = web::AppState::new(cfg, pool)?;
|
|
web::spawn_background(st.clone());
|
|
let app = web::router(st);
|
|
let listener = tokio::net::TcpListener::bind(listen)
|
|
.await
|
|
.with_context(|| format!("cannot listen on {listen}"))?;
|
|
if !login {
|
|
info!("no login: VISA_INSECURE_NO_LOGIN is set (loopback only)");
|
|
}
|
|
info!("listening on http://{listen}");
|
|
axum::serve(listener, app)
|
|
.with_graceful_shutdown(shutdown())
|
|
.await
|
|
.context("serving")?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn shutdown() {
|
|
let ctrl_c = async {
|
|
let _ = tokio::signal::ctrl_c().await;
|
|
};
|
|
#[cfg(unix)]
|
|
let term = async {
|
|
if let Ok(mut s) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
|
{
|
|
s.recv().await;
|
|
}
|
|
};
|
|
#[cfg(not(unix))]
|
|
let term = std::future::pending::<()>();
|
|
tokio::select! {
|
|
_ = ctrl_c => {},
|
|
_ = term => {},
|
|
}
|
|
info!("shutting down");
|
|
}
|