offline-web/src/lib.rs

100 lines
3.0 KiB
Rust
Raw Normal View History

2025-03-29 20:32:25 -04:00
use std::rc::Rc;
2025-03-30 21:18:08 -04:00
use axum::{extract::Path, http::{self, Response}, response::{AppendHeaders, Html, IntoResponse}, routing::get, Json, Router};
2025-03-29 20:32:25 -04:00
use datamodel::Reference;
2025-03-28 17:46:09 -04:00
2025-03-29 16:06:11 -04:00
mod datamodel;
2025-03-28 17:46:09 -04:00
2025-03-29 20:32:25 -04:00
async fn all_references() -> Json<Rc<Reference>> {
2025-03-30 15:02:46 -04:00
let path_root = String::from("ref/0");
let mut root_ref = Reference::new(
"username:0".to_string(),
String::from("0"),
path_root.clone(),
);
for i in 1..=10 {
let mut item_ref = Reference::new(
format!("item:{}", i),
format!("0:{}", i),
format!("{}/item/{}", path_root, i),
);
for j in 1..=10 {
item_ref = item_ref.add_dep(Rc::new(Reference::new(
format!("item:{}:subitem:{}", i, j),
format!("0:{}:{}", i, j),
format!("{}/item/{}/subitem/{}", path_root, i, j),
)));
}
root_ref = root_ref.add_dep(Rc::new(item_ref));
}
Json(root_ref.to_rc())
}
async fn dummy_item_ref(Path(i): Path<usize>) -> Json<Rc<Reference>> {
let path_root = String::from("ref/0");
let mut item_ref = Reference::new(
format!("item:{}", i),
format!("0:{}", i),
format!("{}/item/{}", path_root, i),
);
for j in 1..=10 {
item_ref = item_ref.add_dep(Rc::new(Reference::new(
format!("item:{}:subitem:{}", i, j),
format!("0:{}:{}", i, j),
format!("{}/item/{}/subitem/{}", path_root, i, j),
)));
}
Json(item_ref.to_rc())
}
async fn dummy_subitem_ref(Path(i): Path<usize>, Path(j): Path<usize>) -> Json<Rc<Reference>> {
let path_root = String::from("ref/0");
Json(
Reference::new(
format!("item:{}:subitem:{}", i, j),
format!("0:{}:{}", i, j),
format!("{}/item/{}/subitem/{}", path_root, i, j),
)
.to_rc(),
)
}
async fn dummy_object(Path(addr): Path<String>) -> String {
format!("I am object {}", addr)
2025-03-29 20:32:25 -04:00
}
2025-03-30 21:18:08 -04:00
async fn get_client_js() -> impl IntoResponse {
(
[(http::header::CONTENT_TYPE, "application/javascript")],
include_str!("../static/client.js"),
)
}
2025-03-29 16:06:11 -04:00
pub fn endpoints() -> Router {
2025-03-30 15:02:46 -04:00
Router::new().nest(
"/api/v1",
Router::new().nest(
"/ref",
Router::new()
.route("/all/username", get(all_references))
.route("/item/{i}", get(dummy_item_ref))
.route("/item/{i}/subitem/{j}", get(dummy_subitem_ref))
).nest(
"/object",
Router::new()
.route("/{addr}", get(dummy_object))
),
)
2025-03-30 21:18:08 -04:00
.route("/lib/client.js", get(get_client_js))
.route("/ui/", get(|| async { Html(include_str!("../static/index.html")).into_response() }))
2025-03-29 20:32:25 -04:00
}
2025-03-30 15:02:46 -04:00
// TODO(jwall): Javascript test script
2025-03-29 20:32:25 -04:00
pub async fn serve() {
// run our app with hyper, listening globally on port 3000
2025-03-30 15:02:46 -04:00
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
2025-03-29 20:32:25 -04:00
axum::serve(listener, endpoints()).await.unwrap();
2025-03-28 17:46:09 -04:00
}