Commit ebe8266bbb08ab4a8bfda843ce4c04e4ae62eeea

Authored by Georg Hopp
1 parent 850da5fe

Add first async request to API

@@ -26,26 +26,26 @@ async fn main() -> std::io::Result<()> { @@ -26,26 +26,26 @@ async fn main() -> std::io::Result<()> {
26 26
27 let server = HttpServer::new(move || { 27 let server = HttpServer::new(move || {
28 App::new() . data(database_pool.clone()) 28 App::new() . data(database_pool.clone())
29 - . route("/", web::get().to(routes::root))  
30 . service(actix_files::Files::new("/static", "./static")) 29 . service(actix_files::Files::new("/static", "./static"))
31 - . service( web::resource("/users")  
32 - . route(web::get().to(routes::get_users))  
33 - . route(web::put().to(routes::create_user)))  
34 - . service( web::resource("/users/{id}")  
35 - . route(web::delete().to(routes::delete_user))  
36 - . route(web::get().to(routes::get_user))  
37 - . route(web::put().to(routes::update_user))) 30 + . service( web::scope("/api/v0")
  31 + . service( web::resource("/users")
  32 + . route(web::get().to(routes::get_users))
  33 + . route(web::put().to(routes::create_user)))
  34 + . service( web::resource("/users/{id}")
  35 + . route(web::delete().to(routes::delete_user))
  36 + . route(web::get().to(routes::get_user))
  37 + . route(web::put().to(routes::update_user))))
38 . service( web::scope("") 38 . service( web::scope("")
39 . route("/", web::get().to(routes::root)) 39 . route("/", web::get().to(routes::root))
40 . route("/index", web::get().to(routes::root)) 40 . route("/index", web::get().to(routes::root))
41 . route("/index.html", web::get().to(routes::root)) 41 . route("/index.html", web::get().to(routes::root))
42 . route("/favicon", web::get().to(routes::favicon)) 42 . route("/favicon", web::get().to(routes::favicon))
43 . route("/favicon.ico", web::get().to(routes::favicon))) 43 . route("/favicon.ico", web::get().to(routes::favicon)))
44 - . default_service( web::resource("") 44 + . default_service(web::resource("")
45 . route(web::get().to(routes::p404)) 45 . route(web::get().to(routes::p404))
46 . route( web::route() 46 . route( web::route()
47 . guard(guard::Not(guard::Get())) 47 . guard(guard::Not(guard::Get()))
48 - . to(HttpResponse::MethodNotAllowed) )) 48 + . to(HttpResponse::MethodNotAllowed)))
49 }); 49 });
50 50
51 let server = match listenfd.take_tcp_listener(0).unwrap() { 51 let server = match listenfd.take_tcp_listener(0).unwrap() {
@@ -18,6 +18,7 @@ log = "^0.4" @@ -18,6 +18,7 @@ log = "^0.4"
18 serde = { version = "^1.0", features = ["derive"] } 18 serde = { version = "^1.0", features = ["derive"] }
19 serde_json = "^1.0" 19 serde_json = "^1.0"
20 wasm-bindgen = "^0.2" 20 wasm-bindgen = "^0.2"
  21 +wasm-bindgen-futures = "0.4"
21 22
22 # The `console_error_panic_hook` crate provides better debugging of panics by 23 # The `console_error_panic_hook` crate provides better debugging of panics by
23 # logging them with `console.error`. This is great for development, but requires 24 # logging them with `console.error`. This is great for development, but requires
@@ -34,10 +35,16 @@ version = "^0.3" @@ -34,10 +35,16 @@ version = "^0.3"
34 features = [ 35 features = [
35 "Document", 36 "Document",
36 "DomParser", 37 "DomParser",
  38 + "Headers",
37 "HtmlElement", 39 "HtmlElement",
38 "HtmlInputElement", 40 "HtmlInputElement",
39 "Node", 41 "Node",
40 - "SupportedType" 42 + "Request",
  43 + "RequestInit",
  44 + "RequestMode",
  45 + "Response",
  46 + "SupportedType",
  47 + "Window",
41 ] 48 ]
42 49
43 [dev-dependencies] 50 [dev-dependencies]
@@ -2,6 +2,7 @@ mod data; @@ -2,6 +2,7 @@ mod data;
2 2
3 use log::Level; 3 use log::Level;
4 use mogwai::prelude::*; 4 use mogwai::prelude::*;
  5 +use web_sys::{RequestInit, RequestMode, Request, Response, console};
5 use std::panic; 6 use std::panic;
6 use wasm_bindgen::prelude::*; 7 use wasm_bindgen::prelude::*;
7 8
@@ -25,6 +26,18 @@ fn md_to_html(source: &str) -> String { @@ -25,6 +26,18 @@ fn md_to_html(source: &str) -> String {
25 async fn editor_logic( mut rx_logic: broadcast::Receiver<AppLogic> 26 async fn editor_logic( mut rx_logic: broadcast::Receiver<AppLogic>
26 , tx_view: broadcast::Sender<String> 27 , tx_view: broadcast::Sender<String>
27 , mut rx_dom: broadcast::Receiver<Dom> ) { 28 , mut rx_dom: broadcast::Receiver<Dom> ) {
  29 + let window = web_sys::window().unwrap();
  30 + let mut opts = RequestInit::new();
  31 + opts.method("GET").mode(RequestMode::Cors);
  32 + let request = Request::new_with_str_and_init("/api/v0/users", &opts).unwrap();
  33 + request.headers().set("Accept", "application/json").unwrap();
  34 +
  35 + let response = JsFuture::from(window.fetch_with_request(&request))
  36 + . await.unwrap()
  37 + . dyn_into::<Response>().unwrap();
  38 + let data = JsFuture::from(response.json().unwrap()).await.unwrap();
  39 + console::log_1(&data);
  40 +
28 let dom = rx_dom.next().await.unwrap(); 41 let dom = rx_dom.next().await.unwrap();
29 let mut show_edit = false; 42 let mut show_edit = false;
30 43
Please register or login to post a comment