mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
restructure folders
This commit is contained in:
12
templating/askama/Cargo.toml
Normal file
12
templating/askama/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "template-askama"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.9.0"
|
||||
actix-web = "4.0.0-beta.21"
|
||||
askama = "0.11.0"
|
||||
|
||||
[build-dependencies]
|
||||
askama = "0.11.0"
|
45
templating/askama/src/main.rs
Normal file
45
templating/askama/src/main.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{middleware, 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;
|
||||
|
||||
async fn index(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = query.get("name") {
|
||||
UserTemplate {
|
||||
name,
|
||||
text: "Welcome!",
|
||||
}
|
||||
.render()
|
||||
.unwrap()
|
||||
} else {
|
||||
Index.render().unwrap()
|
||||
};
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
// start http server
|
||||
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
|
||||
}
|
17
templating/askama/templates/index.html
Normal file
17
templating/askama/templates/index.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome!</h1>
|
||||
<p>
|
||||
<h3>What is your name?</h3>
|
||||
<form>
|
||||
<input type="text" name="name" /><br/>
|
||||
<p><input type="submit"></p>
|
||||
</form>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
13
templating/askama/templates/user.html
Normal file
13
templating/askama/templates/user.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Actix Web</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hi, {{ name }}!</h1>
|
||||
<p>
|
||||
{{ text }}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user