diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..a6af73c --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,5 @@ +use axum::{Router, routing::get}; + +pub fn api_router() -> Router { + Router::new().route("/api/live", get(async || "Mnemosyne lives")) +} diff --git a/src/main.rs b/src/main.rs index 013fb12..86ec1c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,40 @@ use std::error::Error; +use tokio::net::TcpListener; + +mod api; mod database; mod persons; mod quotes; mod tags; mod users; -fn main() -> Result<(), Box> { - dotenvy::dotenv()?; +// Mnemosyne, the mother of the nine muses +const DEFAULT_PORT: u16 = 0x9999; // 39321 + +#[tokio::main] +async fn main() -> Result<(), Box> { + if let Err(e) = dotenvy::dotenv() { + if !e.not_found() { + return Err(e.into()); + } + } + database::migrations()?; users::setup::initialise_reserved_users_if_needed()?; + let port = match std::env::var("PORT") { + Ok(p) => p.parse::()?, + Err(e) => match e { + std::env::VarError::NotPresent => DEFAULT_PORT, + _ => return Err(e)?, + }, + }; + let r = api::api_router(); + let l = TcpListener::bind(format!("0.0.0.0:{port}")).await?; + println!("Listener bound to {}", l.local_addr()?); + let port = l.local_addr()?.port(); + + axum::serve(l, r).await?; Ok(()) }