1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00
actix-website/examples/application/src/state.rs

32 lines
651 B
Rust
Raw Normal View History

2018-05-23 22:01:33 +02:00
// <setup>
2020-09-12 17:21:54 +02:00
use actix_web::{get, web, App, HttpServer};
2018-05-23 22:01:33 +02:00
// This struct represents state
struct AppState {
2019-07-15 11:35:50 +02:00
app_name: String,
2018-05-23 22:01:33 +02:00
}
2020-09-12 17:21:54 +02:00
#[get("/")]
2019-12-28 17:15:22 +01:00
async fn index(data: web::Data<AppState>) -> String {
2019-07-22 07:10:21 +02:00
let app_name = &data.app_name; // <- get app_name
2018-05-23 22:01:33 +02:00
2019-07-15 11:35:50 +02:00
format!("Hello {}!", app_name) // <- response with app_name
2018-05-23 22:01:33 +02:00
}
// </setup>
// <start_app>
2020-09-12 17:21:54 +02:00
#[actix_web::main]
2019-12-28 17:15:22 +01:00
async fn main() -> std::io::Result<()> {
2019-06-13 09:24:25 +02:00
HttpServer::new(|| {
App::new()
.data(AppState {
2019-07-22 07:10:21 +02:00
app_name: String::from("Actix-web"),
})
2020-09-12 17:21:54 +02:00
.service(index)
2019-06-13 09:24:25 +02:00
})
2020-09-12 17:21:54 +02:00
.bind("127.0.0.1:8080")?
2019-07-15 12:12:37 +02:00
.run()
2019-12-28 17:15:22 +01:00
.await
2018-05-24 18:31:40 +02:00
}
// </start_app>