mirror of
https://github.com/actix/actix-website
synced 2025-03-15 12:43:07 +01:00
31 lines
671 B
Rust
31 lines
671 B
Rust
// <setup>
|
|
use actix_web::{get, web, App, HttpServer};
|
|
|
|
// This struct represents state
|
|
struct AppState {
|
|
app_name: String,
|
|
}
|
|
|
|
#[get("/")]
|
|
async fn index(data: web::Data<AppState>) -> String {
|
|
let app_name = &data.app_name; // <- get app_name
|
|
format!("Hello {app_name}!") // <- response with app_name
|
|
}
|
|
// </setup>
|
|
|
|
// <start_app>
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
.app_data(web::Data::new(AppState {
|
|
app_name: String::from("Actix Web"),
|
|
}))
|
|
.service(index)
|
|
})
|
|
.bind(("127.0.0.1", 8080))?
|
|
.run()
|
|
.await
|
|
}
|
|
// </start_app>
|