mirror of
https://github.com/actix/actix-website
synced 2024-11-28 02:22:57 +01:00
32 lines
651 B
Rust
32 lines
651 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()
|
|
.data(AppState {
|
|
app_name: String::from("Actix-web"),
|
|
})
|
|
.service(index)
|
|
})
|
|
.bind("127.0.0.1:8080")?
|
|
.run()
|
|
.await
|
|
}
|
|
// </start_app>
|