1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00

30 lines
649 B
Rust
Raw Normal View History

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