80 lines
2.5 KiB
Rust
80 lines
2.5 KiB
Rust
use actix_web::{get, App, HttpResponse, HttpServer, Responder, web::{Data, Query}};
|
|
use tera::{Tera, Context};
|
|
use serde::{Serialize, Deserialize};
|
|
use reqwest::{Client, header::HeaderMap};
|
|
use std::env;
|
|
|
|
struct Template {
|
|
paste: Tera,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Paste {
|
|
paste_id: Option<String>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
struct ResponseJSON {
|
|
file_name: String,
|
|
file_path: String,
|
|
size: u64,
|
|
encoding: String,
|
|
content_sha256: String,
|
|
r#ref: String,
|
|
blob_id: String,
|
|
commit_id: String,
|
|
last_commit_id: String,
|
|
execute_filemode: bool,
|
|
content: 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();
|
|
let gitlab_token = env::var("GITLAB_TOKEN").unwrap_or("".to_string());
|
|
let client = Client::new();
|
|
let mut headers = HeaderMap::new();
|
|
headers.insert("PRIVATE-TOKEN", gitlab_token.parse().unwrap());
|
|
let paste_content: String;
|
|
if paste.paste_id.is_some() {
|
|
let response = client.get(format!("https://git.baalajimaestro.me/api/v4/projects/12/repository/files/paste%2F{}.md?ref=master", paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap();
|
|
let response_code = response.status().as_str().to_string();
|
|
let response_json = response.text().await.unwrap();
|
|
if response_code != "200" {
|
|
paste_content = "Paste ID Invalid".to_string();
|
|
}
|
|
else {
|
|
let resp_struct: ResponseJSON = serde_json::from_str(&response_json).unwrap();
|
|
paste_content = String::from_utf8(base64::decode(resp_struct.content).unwrap()).unwrap();
|
|
}
|
|
}
|
|
else {
|
|
paste_content = "Provide a Paste ID".to_string();
|
|
}
|
|
context.insert("paste_content", &paste_content);
|
|
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 });
|
|
println!("Paste Server is launched on 0.0.0.0:8080");
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.app_data(Data::clone(&data))
|
|
.service(paste_render)
|
|
})
|
|
.bind(("0.0.0.0", 8080))?
|
|
.run()
|
|
.await
|
|
}
|