feat: wire config, component loading, and axum serve in main

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 15:32:10 +02:00
parent 7747ffbc20
commit 86c4440576
5 changed files with 261 additions and 1 deletions
+111
View File
@@ -0,0 +1,111 @@
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;
use crate::error::ConfigError;
#[derive(Debug)]
pub struct AppConfig {
pub listen: SocketAddr,
pub components_dir: PathBuf,
pub cache_ttl: Duration,
pub fetch_timeout: Duration,
}
impl AppConfig {
pub fn from_env() -> Result<Self, ConfigError> {
Self::from_lookup(|key| std::env::var(key).ok())
}
pub fn from_lookup(get: impl Fn(&str) -> Option<String>) -> Result<Self, ConfigError> {
let listen = match get("WHOAREYOU_LISTEN") {
Some(value) => value.parse().map_err(|err| ConfigError::Invalid {
key: "WHOAREYOU_LISTEN".to_string(),
message: format!("{err}"),
})?,
None => SocketAddr::from(([127, 0, 0, 1], 8080)),
};
let components_dir = get("WHOAREYOU_COMPONENTS_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("components"));
let cache_ttl_hours: u64 = parse_or("WHOAREYOU_CACHE_TTL_HOURS", &get, 24)?;
let fetch_timeout_secs: u64 = parse_or("WHOAREYOU_FETCH_TIMEOUT_SECS", &get, 10)?;
Ok(Self {
listen,
components_dir,
cache_ttl: Duration::from_secs(cache_ttl_hours * 3600),
fetch_timeout: Duration::from_secs(fetch_timeout_secs),
})
}
}
fn parse_or(
key: &str,
get: &impl Fn(&str) -> Option<String>,
default: u64,
) -> Result<u64, ConfigError> {
match get(key) {
Some(value) => value.parse().map_err(|err| ConfigError::Invalid {
key: key.to_string(),
message: format!("{err}"),
}),
None => Ok(default),
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::*;
fn env<'a>(pairs: &'a [(&'a str, &'a str)]) -> impl Fn(&str) -> Option<String> + 'a {
let map: HashMap<String, String> = pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
move |key: &str| map.get(key).cloned()
}
#[test]
fn defaults_apply_when_unset() {
let config = AppConfig::from_lookup(env(&[])).unwrap();
assert_eq!(config.listen.to_string(), "127.0.0.1:8080");
assert_eq!(
config.components_dir,
std::path::PathBuf::from("components")
);
assert_eq!(config.cache_ttl, std::time::Duration::from_secs(24 * 3600));
assert_eq!(config.fetch_timeout, std::time::Duration::from_secs(10));
}
#[test]
fn env_overrides_apply() {
let config = AppConfig::from_lookup(env(&[
("WHOAREYOU_LISTEN", "0.0.0.0:9000"),
("WHOAREYOU_COMPONENTS_DIR", "/opt/providers"),
("WHOAREYOU_CACHE_TTL_HOURS", "1"),
("WHOAREYOU_FETCH_TIMEOUT_SECS", "30"),
]))
.unwrap();
assert_eq!(config.listen.to_string(), "0.0.0.0:9000");
assert_eq!(
config.components_dir,
std::path::PathBuf::from("/opt/providers")
);
assert_eq!(config.cache_ttl, std::time::Duration::from_secs(3600));
assert_eq!(config.fetch_timeout, std::time::Duration::from_secs(30));
}
#[test]
fn invalid_values_error() {
assert!(AppConfig::from_lookup(env(&[("WHOAREYOU_LISTEN", "not-an-addr")])).is_err());
assert!(AppConfig::from_lookup(env(&[("WHOAREYOU_CACHE_TTL_HOURS", "soon")])).is_err());
}
}
+1
View File
@@ -1,3 +1,4 @@
pub mod config;
pub mod error;
pub mod fetch;
pub mod http;
+67 -1
View File
@@ -1 +1,67 @@
fn main() {}
use std::sync::Arc;
use anyhow::Context;
use tracing::info;
use tracing_subscriber::EnvFilter;
use whoareyou_server::config::AppConfig;
use whoareyou_server::fetch::ReqwestFetcher;
use whoareyou_server::service::{LookupService, ProviderHandle};
use whoareyou_server::{http, wasm};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
)
.init();
let config = AppConfig::from_env()?;
let engine = wasm::engine()?;
let linker = wasm::linker(&engine)?;
wasm::spawn_epoch_thread(&engine);
let mut providers: Vec<Arc<dyn ProviderHandle>> = Vec::new();
let dir = std::fs::read_dir(&config.components_dir)
.with_context(|| format!("reading components dir {:?}", config.components_dir))?;
for entry in dir {
let path = entry?.path();
if path.extension().is_some_and(|ext| ext == "wasm") {
let provider = wasm::WasmProvider::load(&engine, &linker, &path)
.with_context(|| format!("loading component {path:?}"))?;
info!(
name = provider.name(),
version = provider.version(),
?path,
"loaded provider"
);
providers.push(Arc::new(provider));
}
}
anyhow::ensure!(
!providers.is_empty(),
"no .wasm components found in {:?}",
config.components_dir
);
let fetcher = Arc::new(ReqwestFetcher::new(config.fetch_timeout)?);
let service = Arc::new(LookupService::new(providers, fetcher, config.cache_ttl));
let app = http::router(service);
let listener = tokio::net::TcpListener::bind(config.listen).await?;
info!("listening on http://{}", config.listen);
axum::serve(listener, app).await?;
Ok(())
}