From 5dcbf57d150182892294724f4f8c85ea1aebf653 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 29 Mar 2025 20:32:25 -0400 Subject: [PATCH] wip: initial web server wiring --- Cargo.lock | 1 + Cargo.toml | 8 ++++++++ src/lib.rs | 20 +++++++++++++++++--- src/main.rs | 5 +++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index ae3ef42..ebe097c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,6 +304,7 @@ version = "0.1.0" dependencies = [ "axum", "serde", + "tokio", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b2faf43..2cc5069 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,14 @@ name = "offline-web" version = "0.1.0" edition = "2021" +[lib] +path = "src/lib.rs" + +[[bin]] +name = "example" +path = "src/main.rs" + [dependencies] axum = "0.8.3" serde = { version = "1.0.219", features = ["derive", "rc"] } +tokio = "1.44.1" diff --git a/src/lib.rs b/src/lib.rs index 9175220..4e758f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,24 @@ +use std::rc::Rc; + use axum::{ routing::get, - Router + Router, + Json, }; +use datamodel::Reference; mod datamodel; -pub fn endpoints() -> Router { - Router::new().route("/api/v1/ref/all/username", get(|| async { "hello world" })) +async fn all_references() -> Json> { + Json(datamodel::Reference::new("silly id".to_string(), "all/username".to_string()).to_rc()) +} + +pub fn endpoints() -> Router { + Router::new().route("/api/v1/ref/all/username", get(all_references)) +} + +pub async fn serve() { + // run our app with hyper, listening globally on port 3000 + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap(); + axum::serve(listener, endpoints()).await.unwrap(); } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7a64137 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ + +#[tokio::main(flavor = "current_thread")] +async fn main() { + offline_web::serve().await; +}