mirror of
https://github.com/actix/examples
synced 2024-12-05 03:01:55 +01:00
39 lines
887 B
Rust
39 lines
887 B
Rust
use std::collections::HashMap;
|
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer, Result};
|
|
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;
|
|
|
|
fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
|
let s = if let Some(name) = query.get("name") {
|
|
UserTemplate {
|
|
name: name,
|
|
text: "Welcome!",
|
|
}
|
|
.render()
|
|
.unwrap()
|
|
} else {
|
|
Index.render().unwrap()
|
|
};
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
}
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
// start http server
|
|
HttpServer::new(move || {
|
|
App::new().service(web::resource("/").route(web::get()).to(index))
|
|
})
|
|
.bind("127.0.0.1:8080")?
|
|
.run()
|
|
}
|