1
0
mirror of https://github.com/actix/actix-website synced 2025-03-15 20:53:07 +01:00

31 lines
671 B
Rust
Raw Normal View History

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