mirror of
https://github.com/actix/actix-website
synced 2024-11-27 18:12:57 +01:00
Updates front-page flexible-responders to v1.0
This commit is contained in:
parent
118286afb1
commit
d2a2fc6979
@ -4,3 +4,5 @@ version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
serde = "1.0"
|
||||
|
@ -1,5 +1,29 @@
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use serde::Serialize;
|
||||
|
||||
// <flexible-responders>
|
||||
#[derive(Serialize)]
|
||||
struct Measurement {
|
||||
temperature: f32,
|
||||
}
|
||||
|
||||
fn hello_world() -> impl Responder {
|
||||
"Hello World!"
|
||||
}
|
||||
|
||||
fn current_temperature(_req: HttpRequest) -> impl Responder {
|
||||
web::Json(Measurement { temperature: 42.3 })
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(web::resource("/").to(hello_world))
|
||||
.service(web::resource("/temp").to(current_temperature))
|
||||
})
|
||||
.bind("127.0.0.1:8000")
|
||||
.expect("Can not bind to port 8000")
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </flexible-responders>
|
||||
|
@ -11,6 +11,12 @@
|
||||
<button onclick="submitJson()">Submit</button>
|
||||
|
||||
<script>
|
||||
let payload = {
|
||||
timestamp: 12345,
|
||||
kind: "this is a kind",
|
||||
tags: ['tag1', 'tag2', 'tag3'],
|
||||
}
|
||||
|
||||
function submitJson() {
|
||||
fetch('http://localhost:8000/event', {
|
||||
method: 'POST',
|
||||
@ -18,17 +24,18 @@
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
timestamp: 12345,
|
||||
kind: "this is a kind",
|
||||
tags: ['tag1', 'tag2', 'tag3'],
|
||||
})
|
||||
body: JSON.stringify(payload)
|
||||
})
|
||||
.then(function (res) {
|
||||
return res.text();
|
||||
return res.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
console.log(data);
|
||||
let expected = {
|
||||
...payload,
|
||||
id: 1
|
||||
};
|
||||
console.log("expected: ", expected);
|
||||
console.log("received: ", data);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
@ -79,7 +79,7 @@ fn hello_world() -> impl Responder {
|
||||
}
|
||||
|
||||
fn current_temperature(_req: HttpRequest) -> impl Responder {
|
||||
Json(Measurement { temperature: 42.3 })
|
||||
web::Json(Measurement { temperature: 42.3 })
|
||||
}` "rust" "" }}
|
||||
</div>
|
||||
<div class="actix-feature" id="extractors">
|
||||
|
Loading…
Reference in New Issue
Block a user