Compare commits
2 Commits
394b0b5b77
...
275590709a
Author | SHA1 | Date | |
---|---|---|---|
275590709a | |||
5dcbf57d15 |
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -304,6 +304,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"serde",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -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"
|
||||
|
@ -90,7 +90,7 @@ operations.
|
||||
|
||||
### Bootstrapping
|
||||
|
||||
* Load `/api/v1/resource/all/<username>` and then follow the sub resources to
|
||||
* Load `/api/v1/ref/all/<username>` and then follow the sub resources to
|
||||
load the entire dataset locally making sure to keep the content-addresses
|
||||
around for comparison.
|
||||
|
||||
|
@ -5,15 +5,17 @@ use serde::{Serialize, Deserialize};
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Reference {
|
||||
object_id: String,
|
||||
content_address: String,
|
||||
path: String,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
dependents: Vec<Rc<Reference>>,
|
||||
}
|
||||
|
||||
impl Reference {
|
||||
pub fn new(object_id: String, path: String) -> Self {
|
||||
pub fn new(object_id: String, content_address: String, path: String) -> Self {
|
||||
Self {
|
||||
object_id,
|
||||
content_address,
|
||||
path,
|
||||
dependents: Vec::new(),
|
||||
}
|
||||
|
92
src/lib.rs
92
src/lib.rs
@ -1,10 +1,90 @@
|
||||
use axum::{
|
||||
routing::get,
|
||||
Router
|
||||
};
|
||||
use std::rc::Rc;
|
||||
|
||||
use axum::{extract::Path, routing::get, Json, Router};
|
||||
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<Rc<Reference>> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn endpoints() -> Router {
|
||||
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))
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// TODO(jwall): Javascript test script
|
||||
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();
|
||||
}
|
||||
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
offline_web::serve().await;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user