37 řádky
865 B
Rust
37 řádky
865 B
Rust
use actix_web::{
|
|
get,
|
|
web::{Data, Path},
|
|
HttpRequest, HttpResponse,
|
|
};
|
|
use askama::Template;
|
|
use tokio::fs::read_to_string;
|
|
|
|
use crate::{ctx::Ctx, error::NekrochanError, web::template_response};
|
|
|
|
use super::tcx::TemplateCtx;
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "page.html")]
|
|
struct PageTemplate {
|
|
pub tcx: TemplateCtx,
|
|
pub name: String,
|
|
pub content: String,
|
|
}
|
|
|
|
#[get("/page/{name}")]
|
|
pub async fn page(
|
|
ctx: Data<Ctx>,
|
|
req: HttpRequest,
|
|
name: Path<String>,
|
|
) -> Result<HttpResponse, NekrochanError> {
|
|
let tcx = TemplateCtx::new(&ctx, &req).await?;
|
|
let name = name.into_inner();
|
|
let content = read_to_string(format!("./pages/{name}.html"))
|
|
.await
|
|
.map_err(|_| NekrochanError::PageNotFound(name.clone()))?;
|
|
|
|
let template = PageTemplate { tcx, name, content };
|
|
|
|
template_response(&template)
|
|
}
|