Switch it to gitea
Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
parent
69a3c01a06
commit
683358f3c1
1 changed files with 17 additions and 15 deletions
32
src/main.rs
32
src/main.rs
|
@ -24,43 +24,45 @@ struct Paste {
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct GitLabSuccessJSON {
|
struct GitLabSuccessJSON {
|
||||||
file_name: String,
|
name: String,
|
||||||
file_path: String,
|
path: String,
|
||||||
size: u64,
|
size: u64,
|
||||||
encoding: String,
|
encoding: String,
|
||||||
content_sha256: String,
|
sha: String,
|
||||||
r#ref: String,
|
target: Option<String>,
|
||||||
blob_id: String,
|
url: String,
|
||||||
commit_id: String,
|
html_url: String,
|
||||||
last_commit_id: String,
|
git_url: String,
|
||||||
execute_filemode: bool,
|
submodule_git_url: Option<String>,
|
||||||
content: String,
|
content: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct GitLabFailedJSON {
|
struct GitLabFailedJSON {
|
||||||
|
errors: Option<String>,
|
||||||
message: String,
|
message: String,
|
||||||
|
url: 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("GITEA_TOKEN").unwrap_or("".to_string());
|
||||||
let base_url = env::var("BASE_URL").unwrap_or("https://gitlab.com".to_string());
|
let base_url = env::var("BASE_URL").unwrap_or("https://gitea.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("Authorization", 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!("{}/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_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" {
|
||||||
let resp_struct: GitLabFailedJSON = serde_json::from_str(&response_json).unwrap();
|
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");
|
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 {
|
else {
|
||||||
context.insert("md", "true");
|
context.insert("md", "true");
|
||||||
|
|
Loading…
Reference in a new issue