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