mirror of
https://github.com/actix/actix-website
synced 2025-03-20 14:45:18 +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"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[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>
|
// <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() {
|
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>
|
// </flexible-responders>
|
||||||
|
@ -11,6 +11,12 @@
|
|||||||
<button onclick="submitJson()">Submit</button>
|
<button onclick="submitJson()">Submit</button>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
let payload = {
|
||||||
|
timestamp: 12345,
|
||||||
|
kind: "this is a kind",
|
||||||
|
tags: ['tag1', 'tag2', 'tag3'],
|
||||||
|
}
|
||||||
|
|
||||||
function submitJson() {
|
function submitJson() {
|
||||||
fetch('http://localhost:8000/event', {
|
fetch('http://localhost:8000/event', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -18,17 +24,18 @@
|
|||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(payload)
|
||||||
timestamp: 12345,
|
|
||||||
kind: "this is a kind",
|
|
||||||
tags: ['tag1', 'tag2', 'tag3'],
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.then(function (res) {
|
.then(function (res) {
|
||||||
return res.text();
|
return res.json();
|
||||||
})
|
})
|
||||||
.then(function (data) {
|
.then(function (data) {
|
||||||
console.log(data);
|
let expected = {
|
||||||
|
...payload,
|
||||||
|
id: 1
|
||||||
|
};
|
||||||
|
console.log("expected: ", expected);
|
||||||
|
console.log("received: ", data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -79,7 +79,7 @@ fn hello_world() -> impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn current_temperature(_req: HttpRequest) -> impl Responder {
|
fn current_temperature(_req: HttpRequest) -> impl Responder {
|
||||||
Json(Measurement { temperature: 42.3 })
|
web::Json(Measurement { temperature: 42.3 })
|
||||||
}` "rust" "" }}
|
}` "rust" "" }}
|
||||||
</div>
|
</div>
|
||||||
<div class="actix-feature" id="extractors">
|
<div class="actix-feature" id="extractors">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user