Useage
use askama::Template;
use tera::{Context, Tera};
use athene::headers::{authorization::Bearer, Authorization};
use athene::{prelude::*,json, status, text, html};
use tracing::{info, debug, error, warn};
pub async fn askama_template(_: Request) -> Result {
#[derive(Serialize,Template)]
#[template(path = "favorite.html")]
pub struct Favorite<'a> {
title: &'a str,
time: i32,
}
let love = Favorite {
title: "rust_lang",
time: 1314,
};
let love = love.render()?;
html!(love)
}
pub async fn tera_template(_: Request) -> Result {
#[derive(Serialize)]
struct Awesome<'a> {
url: &'a str,
name: &'a str,
}
let tera = Tera::new("templates/*")?;
let mut ctx = Context::new();
ctx.insert("title", "Welcome to rust-lang world!");
ctx.insert(
"product",
&vec![
Awesome {
url: "https://github.com/rust-lang",
name: "rust-lang",
},
Awesome {
url: "https://github.com/lapce/lapce",
name: "lapce",
},
],
);
let love = tera.render("index.html", &ctx)?;
html!(love)
}
struct Logger;
#[async_trait]
impl Middleware for Logger {
async fn apply(&self, req: Request, next: Next<'_>) -> Result<Response> {
let method = req.method().to_string();
let uri = req.uri().to_string();
debug!(%method, %uri, "request");
let result = next.next(req).await?;
log_response(method, uri, &result);
Ok(result)
}
}
fn log_response(method: String, uri: String, resp: &Response) {
let status = resp.as_ref().status();
if status.is_server_error() {
error!(%method, %uri, %status, "response");
} else if status.is_client_error() {
warn!(%method, %uri, %status, "response");
} else {
info!(%method, %uri, %status, "response");
}
}
struct AuthCheck;
#[async_trait]
impl Middleware for AuthCheck {
async fn apply(&self, req: Request, next: Next<'_>) -> Result<Response> {
let auth = req.header::<Authorization<Bearer>>();
match auth {
None => return Ok(Response::status(StatusCode::UNAUTHORIZED)),
Some(bearer) => {
info!("bearer token: {}", bearer.0.token());
next.next(req).await
}
}
}
}
#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().compact().init();
let mut app = athene::new();
app.middleware(Logger);
app
.get("/hello", |_| async { "Hello world!\n\n" })
.get("/askama_template",askama_template)
.get("/tera_template", tera_template)
.get("/param/:name",|req: Request| async move {
let name = req.param("name").unwrap();
name.to_string()
})
.get("/query", |req: Request| async move {
#[derive(Serialize,Deserialize)]
struct Book {
title: String,
is_publish: bool,
}
let book = req.query::<Book>()?;
json!(book)
})
.post("/form-urlencoded",|mut req: Request| async move {
#[derive(Serialize,Deserialize)]
struct Rust {
version: String,
edition: String,
}
let rust_lang = req.parse::<Rust>().await?;
json!(rust_lang)
})
.post("/json",|mut req: Request| async move {
#[derive(Serialize,Deserialize)]
struct User {
name: String,
age: i32,
}
let user = req.parse::<User>().await?;
json!(user)
})
.post("/file",|mut req: Request| async move {
let file = req.file("file").await?;
let name = file.name().unwrap().to_string();
text!(name)
})
.post("/files",|mut req: Request| async move {
let file = req.files("files").await?;
let fist_file_name = file[0].name().unwrap();
let second_file_name = file[1].name().unwrap();
let name = format!("fist {}, second {}",fist_file_name,second_file_name);
text!(name)
})
.get("/download",|_|async {
let mut res = Response::default();
res.write_file("templates/author.txt", DispositionType::Attachment)?;
Ok(res)
});
let mut api = athene::new();
api.middleware(AuthCheck);
api.get("check", |req: Request| async move {
let ok = StatusCode::OK;
let mut res = status!(ok);
res.text(req.uri().to_string())?;
Ok(res)
});
api.get("user/:name", |req: Request| async move {
let ok = StatusCode::OK;
let mut res = status!(ok);
let resp = format!("uri = {}\nparams = {:?}\n",req.uri(),req.params());
res.text(resp)?;
Ok(res)
});
app.group("/api/:version", api);
app.static_files("/public/*", "public");
app.listen("127.0.0.1:8888").await?;
Ok(())
}
templates/index.html
<title>{{title}}</title>
<ul>
{% for one in product -%}
<li><a href="{{ one.url | safe }}">{{ one.name }}</a></li>
{%- endfor %}
<img src="public/icon.jpg" alt="A rocket icon." height=200 width=200 />
</ul>
templates/favorite.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rust is one of my favorite programming languages</title>
</head>
<body>
<h4>{{title}}</h4>
<p>{{time}}</p>
</body>
</html>