Handle a specific case where gitlab token or project id can be wrong
Also make BASE_URL an env variable Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
parent
eec669a980
commit
300b0c9a3c
1 changed files with 16 additions and 4 deletions
20
src/main.rs
20
src/main.rs
|
@ -23,7 +23,7 @@ struct Paste {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ResponseJSON {
|
struct GitLabSuccessJSON {
|
||||||
file_name: String,
|
file_name: String,
|
||||||
file_path: String,
|
file_path: String,
|
||||||
size: u64,
|
size: u64,
|
||||||
|
@ -37,23 +37,35 @@ struct ResponseJSON {
|
||||||
content: String,
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct GitLabFailedJSON {
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Responder {
|
async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Responder {
|
||||||
let my_data = data.get_ref();
|
let my_data = data.get_ref();
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
let gitlab_token = env::var("GITLAB_TOKEN").unwrap_or("".to_string());
|
let gitlab_token = env::var("GITLAB_TOKEN").unwrap_or("".to_string());
|
||||||
|
let base_url = env::var("BASE_URL").unwrap_or("https://gitlab.com".to_string());
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert("PRIVATE-TOKEN", gitlab_token.parse().unwrap());
|
headers.insert("PRIVATE-TOKEN", gitlab_token.parse().unwrap());
|
||||||
let paste_content: String;
|
let paste_content: String;
|
||||||
if paste.paste_id.is_some() {
|
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 = client.get(format!("{}/repository/files/paste%2F{}.md?ref=master", base_url, paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap();
|
||||||
let response_code = response.status().as_str().to_string();
|
let response_code = response.status().as_str().to_string();
|
||||||
let response_json = response.text().await.unwrap();
|
let response_json = response.text().await.unwrap();
|
||||||
if response_code != "200" {
|
if response_code != "200" {
|
||||||
paste_content = "Paste ID Invalid".to_string();
|
let resp_struct: GitLabFailedJSON = serde_json::from_str(&response_json).unwrap();
|
||||||
|
if resp_struct.message == "404 Project Not Found" {
|
||||||
|
paste_content = "Project ID Mismatch/GitLab Token Missing".to_string();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
paste_content = "Paste ID is Invalid".to_string();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let resp_struct: ResponseJSON = serde_json::from_str(&response_json).unwrap();
|
let resp_struct: GitLabSuccessJSON = serde_json::from_str(&response_json).unwrap();
|
||||||
paste_content = String::from_utf8(
|
paste_content = String::from_utf8(
|
||||||
general_purpose::STANDARD
|
general_purpose::STANDARD
|
||||||
.decode(resp_struct.content)
|
.decode(resp_struct.content)
|
||||||
|
|
Loading…
Reference in a new issue