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:
Generated
+80
@@ -1388,6 +1388,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "leb128"
|
name = "leb128"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
@@ -1474,6 +1480,15 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matchers"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
|
||||||
|
dependencies = [
|
||||||
|
"regex-automata",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchit"
|
name = "matchit"
|
||||||
version = "0.8.4"
|
version = "0.8.4"
|
||||||
@@ -1538,6 +1553,15 @@ dependencies = [
|
|||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nu-ansi-term"
|
||||||
|
version = "0.50.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.39.1"
|
version = "0.39.1"
|
||||||
@@ -2229,6 +2253,15 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sharded-slab"
|
||||||
|
version = "0.1.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@@ -2430,6 +2463,15 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thread_local"
|
||||||
|
version = "1.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinystr"
|
name = "tinystr"
|
||||||
version = "0.8.3"
|
version = "0.8.3"
|
||||||
@@ -2621,6 +2663,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
|
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"valuable",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tracing-log"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||||
|
dependencies = [
|
||||||
|
"log",
|
||||||
|
"once_cell",
|
||||||
|
"tracing-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tracing-subscriber"
|
||||||
|
version = "0.3.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
|
||||||
|
dependencies = [
|
||||||
|
"matchers",
|
||||||
|
"nu-ansi-term",
|
||||||
|
"once_cell",
|
||||||
|
"regex-automata",
|
||||||
|
"sharded-slab",
|
||||||
|
"smallvec",
|
||||||
|
"thread_local",
|
||||||
|
"tracing",
|
||||||
|
"tracing-core",
|
||||||
|
"tracing-log",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2688,6 +2760,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "valuable"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
@@ -3307,6 +3385,7 @@ dependencies = [
|
|||||||
name = "whoareyou-server"
|
name = "whoareyou-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"axum",
|
"axum",
|
||||||
"futures",
|
"futures",
|
||||||
@@ -3319,6 +3398,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
"wasmtime",
|
"wasmtime",
|
||||||
"wasmtime-wasi",
|
"wasmtime-wasi",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ edition.workspace = true
|
|||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
@@ -15,6 +16,7 @@ serde_json = "1"
|
|||||||
thiserror = "2"
|
thiserror = "2"
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
wasmtime = { version = "45", features = ["component-model"] }
|
wasmtime = { version = "45", features = ["component-model"] }
|
||||||
wasmtime-wasi = "45"
|
wasmtime-wasi = "45"
|
||||||
|
|
||||||
|
|||||||
@@ -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,3 +1,4 @@
|
|||||||
|
pub mod config;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod fetch;
|
pub mod fetch;
|
||||||
pub mod http;
|
pub mod http;
|
||||||
|
|||||||
@@ -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(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user