use crate::KnowledgeBase;
mod api;
mod assets;
#[cfg(test)]
mod tests;
pub fn is_loopback(bind: &str) -> bool {
match bind.parse::<std::net::IpAddr>() {
Ok(ip) => ip.is_loopback(),
Err(_) => matches!(bind, "localhost" | "localhost.localdomain"),
}
}
pub fn serve(kb: KnowledgeBase, bind: &str, port: u16, require_token: bool) -> anyhow::Result<()> {
let token = if require_token {
Some(crate::utils::gen_uuid())
} else {
None
};
let addr = format!("{bind}:{port}");
let server = tiny_http::Server::http(addr.as_str())
.map_err(|e| anyhow::anyhow!("failed to bind {addr}: {e}"))?;
let url = match &token {
Some(t) => format!("http://{bind}:{port}/#token={t}"),
None => format!("http://{bind}:{port}/"),
};
eprintln!("innate web listening on http://{bind}:{port}");
eprintln!("open: {url}");
if token.is_none() {
eprintln!("WARNING: --no-token set; governance endpoints are unauthenticated.");
}
let ctx = api::Ctx {
kb,
token,
bind: bind.to_string(),
port,
};
for request in server.incoming_requests() {
api::handle(&ctx, request);
}
Ok(())
}