1
0
mirror of https://github.com/actix/actix-website synced 2025-06-30 08:44:27 +02:00

Updates front-page flexible-responders to v1.0

This commit is contained in:
Cameron Dershem
2019-06-12 23:25:00 -04:00
parent 118286afb1
commit d2a2fc6979
4 changed files with 42 additions and 9 deletions

View File

@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "1.0"
serde = "1.0"

View File

@ -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>