obsolute-paste/src/main.rs

45 lines
1 KiB
Rust
Raw Normal View History

use actix_web::{get, App, HttpResponse, HttpServer, Responder, web::{Data, Query}};
use tera::{Tera, Context};
use serde::Deserialize;
struct Template {
paste: Tera,
}
#[derive(Deserialize)]
struct Paste {
paste_id: String,
}
#[get("/")]
async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Responder {
let my_data = data.get_ref();
let mut context = Context::new();
context.insert("paste_content", &paste.paste_id);
let rendered = my_data.paste.render("paste.html", &context);
HttpResponse::Ok().body(rendered.unwrap())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let tera = match Tera::new("templates/*.html") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
}
};
let data = Data::new(Template { paste: tera });
HttpServer::new(move || {
App::new()
.app_data(Data::clone(&data))
.service(paste_render)
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}