2019-06-28 20:22:51 +02:00
|
|
|
use actix_web::{web, App, HttpServer, Responder};
|
2019-06-13 05:25:00 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-06-13 00:14:10 +02:00
|
|
|
// <flexible-responders>
|
2019-06-13 05:25:00 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Measurement {
|
|
|
|
temperature: f32,
|
|
|
|
}
|
|
|
|
|
2019-12-28 17:32:43 +01:00
|
|
|
async fn hello_world() -> impl Responder {
|
2019-06-13 05:25:00 +02:00
|
|
|
"Hello World!"
|
|
|
|
}
|
|
|
|
|
2019-12-28 17:32:43 +01:00
|
|
|
async fn current_temperature() -> impl Responder {
|
2019-06-13 05:25:00 +02:00
|
|
|
web::Json(Measurement { temperature: 42.3 })
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
#[actix_web::main]
|
2019-12-28 17:32:43 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-06-13 05:25:00 +02:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.service(web::resource("/").to(hello_world))
|
|
|
|
.service(web::resource("/temp").to(current_temperature))
|
|
|
|
})
|
2020-09-12 17:21:54 +02:00
|
|
|
.bind("127.0.0.1:8080")?
|
2019-06-13 05:25:00 +02:00
|
|
|
.run()
|
2019-12-28 17:32:43 +01:00
|
|
|
.await
|
2019-06-13 00:14:10 +02:00
|
|
|
}
|
|
|
|
// </flexible-responders>
|