2022-11-20 09:09:56 +05:30
|
|
|
use actix_web::{get, App, HttpResponse, HttpServer, Responder, web::{Data, Query}};
|
2022-11-11 20:01:58 +05:30
|
|
|
use tera::{Tera, Context};
|
2022-11-20 09:09:56 +05:30
|
|
|
use serde::Deserialize;
|
2022-11-11 19:11:33 +05:30
|
|
|
|
2022-11-11 20:01:58 +05:30
|
|
|
struct Template {
|
|
|
|
paste: Tera,
|
2022-11-11 19:11:33 +05:30
|
|
|
}
|
|
|
|
|
2022-11-20 09:09:56 +05:30
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Paste {
|
|
|
|
paste_id: String,
|
|
|
|
}
|
|
|
|
|
2022-11-11 19:11:33 +05:30
|
|
|
|
2022-11-20 09:09:56 +05:30
|
|
|
#[get("/")]
|
|
|
|
async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Responder {
|
2022-11-11 20:01:58 +05:30
|
|
|
let my_data = data.get_ref();
|
|
|
|
let mut context = Context::new();
|
2022-11-20 09:09:56 +05:30
|
|
|
context.insert("paste_content", &paste.paste_id);
|
2022-11-11 20:01:58 +05:30
|
|
|
let rendered = my_data.paste.render("paste.html", &context);
|
|
|
|
HttpResponse::Ok().body(rendered.unwrap())
|
2022-11-11 19:11:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
2022-11-11 20:01:58 +05:30
|
|
|
|
2022-11-11 19:11:33 +05:30
|
|
|
async fn main() -> std::io::Result<()> {
|
2022-11-11 20:01:58 +05:30
|
|
|
|
|
|
|
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 || {
|
2022-11-11 19:11:33 +05:30
|
|
|
App::new()
|
2022-11-11 20:01:58 +05:30
|
|
|
.app_data(Data::clone(&data))
|
|
|
|
.service(paste_render)
|
2022-11-11 19:11:33 +05:30
|
|
|
})
|
2022-11-19 22:56:05 +05:30
|
|
|
.bind(("0.0.0.0", 8080))?
|
2022-11-11 19:11:33 +05:30
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|