Switch it to gitea

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2023-09-24 22:03:32 +05:30
parent 69a3c01a06
commit 683358f3c1
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -24,43 +24,45 @@ struct Paste {
#[derive(Serialize, Deserialize)]
struct GitLabSuccessJSON {
file_name: String,
file_path: String,
name: String,
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,
sha: String,
target: Option<String>,
url: String,
html_url: String,
git_url: String,
submodule_git_url: Option<String>,
content: String
}
#[derive(Serialize, Deserialize)]
struct GitLabFailedJSON {
errors: Option<String>,
message: String,
url: 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 base_url = env::var("BASE_URL").unwrap_or("https://gitlab.com".to_string());
let gitlab_token = env::var("GITEA_TOKEN").unwrap_or("".to_string());
let base_url = env::var("BASE_URL").unwrap_or("https://gitea.com".to_string());
let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert("PRIVATE-TOKEN", gitlab_token.parse().unwrap());
headers.insert("Authorization", gitlab_token.parse().unwrap());
let paste_content: String;
if paste.paste_id.is_some() {
let response = client.get(format!("{}/repository/files/{}?ref=master", base_url, paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap();
let response = client.get(format!("{}/contents/{}?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_json = response.text().await.unwrap();
if response_code != "200" {
let resp_struct: GitLabFailedJSON = serde_json::from_str(&response_json).unwrap();
if resp_struct.message == "404 Project Not Found" {
if resp_struct.message == "The target couldn't be found." {
context.insert("md", "true");
paste_content = "Project ID Mismatch/GitLab Token Missing".to_string();
paste_content = "Project ID Mismatch/Gitea Token Missing".to_string();
}
else {
context.insert("md", "true");