mirror of
https://github.com/actix/examples
synced 2024-12-01 01:24:35 +01:00
48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use actix_web::{middleware, web, App, HttpServer, Responder, Result};
|
|
use actix_web_lab::respond::Html;
|
|
use askama::Template;
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "user.html")]
|
|
struct UserTemplate<'a> {
|
|
name: &'a str,
|
|
text: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "index.html")]
|
|
struct Index;
|
|
|
|
async fn index(query: web::Query<HashMap<String, String>>) -> Result<impl Responder> {
|
|
let html = if let Some(name) = query.get("name") {
|
|
UserTemplate {
|
|
name,
|
|
text: "Welcome!",
|
|
}
|
|
.render()
|
|
.expect("template should be valid")
|
|
} else {
|
|
Index.render().expect("template should be valid")
|
|
};
|
|
|
|
Ok(Html(html))
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
|
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.wrap(middleware::Logger::default())
|
|
.service(web::resource("/").route(web::get().to(index)))
|
|
})
|
|
.bind(("127.0.0.1", 8080))?
|
|
.run()
|
|
.await
|
|
}
|