-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(main): move routing out into tree structure
Signed-off-by: Christina Sørensen <christina@cafkafk.com>
- Loading branch information
Showing
13 changed files
with
261 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pub mod routes; | ||
|
||
mod v1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use super::v1::routes::get_routes as get_v1_routes; | ||
use axum::Router; | ||
|
||
pub fn get_routes() -> Router { | ||
Router::new().merge(get_v1_routes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Path, | ||
http::{Request, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
}; | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
use super::super::utils::github_api_get_latest_tag; | ||
|
||
pub async fn get_repo( | ||
Path((forge, user, repo)): Path<(String, String, String)>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
if repo.ends_with(".tar.gz") { | ||
let version = github_api_get_latest_tag( | ||
user.clone(), | ||
repo.clone() | ||
.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix") | ||
.to_string(), | ||
) | ||
.await | ||
.expect("failed to await github_api_get_latest_tag"); | ||
let result_uri = format!( | ||
"http://{}.com/{}/{}/archive/refs/tags/{}.tar.gz", | ||
forge, | ||
user, | ||
repo.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix"), | ||
version, | ||
); | ||
trace!("{result_uri:#?}"); | ||
Redirect::to(&result_uri).into_response() | ||
} else { | ||
let body = format!( | ||
"Hi friend, you probably meant to request {:#?}{}.tar.gz, that should work <3", | ||
request.headers()["host"], | ||
request.uri() | ||
); | ||
(StatusCode::BAD_REQUEST, body).into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Path, | ||
http::{Request, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
}; | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
pub async fn get_repo_branch( | ||
Path((forge, user, repo, branch)): Path<(String, String, String, String)>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
if branch.ends_with(".tar.gz") { | ||
let uri = format!( | ||
"https://{}.com/{}/{}/archive/refs/heads/{}.tar.gz", | ||
forge, | ||
user, | ||
repo, | ||
branch | ||
.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix") | ||
); | ||
Redirect::to(&uri).into_response() | ||
} else { | ||
let body = format!( | ||
"Hi friend, you probably meant to request {:#?}{}.tar.gz, that should work <3", | ||
request.headers()["host"], | ||
request.uri() | ||
); | ||
(StatusCode::BAD_REQUEST, body).into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Path, | ||
http::{Request, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
}; | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
pub async fn get_repo_version( | ||
Path((forge, user, repo, version)): Path<(String, String, String, String)>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
if version.ends_with(".tar.gz") { | ||
let uri = format!( | ||
"https://{}.com/{}/{}/archive/refs/tags/{}.tar.gz", | ||
forge, | ||
user, | ||
repo, | ||
version | ||
.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix") | ||
); | ||
Redirect::to(&uri).into_response() | ||
} else { | ||
let body = format!( | ||
"Hi friend, you probably meant to request {:#?}{}.tar.gz, that should work <3", | ||
request.headers()["host"], | ||
request.uri() | ||
); | ||
(StatusCode::BAD_REQUEST, body).into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
mod get_repo; | ||
pub use self::get_repo::get_repo; | ||
mod get_repo_branch; | ||
pub use self::get_repo_branch::get_repo_branch; | ||
mod get_repo_version; | ||
pub use self::get_repo_version::get_repo_version; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pub mod routes; | ||
|
||
mod endpoints; | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use super::endpoints::{get_repo, get_repo_branch, get_repo_version}; | ||
|
||
use axum::{routing::get, Router}; | ||
|
||
pub fn get_routes() -> Router { | ||
Router::new() | ||
.route("/v1/:forge/:user/:repo/b/:branch", get(get_repo_branch)) | ||
.route( | ||
"/v1/:forge/:user/:repo/branch/:branch", | ||
get(get_repo_branch), | ||
) | ||
.route("/v1/:forge/:user/:repo/v/:version", get(get_repo_version)) | ||
.route( | ||
"/v1/:forge/:user/:repo/version/:version", | ||
get(get_repo_version), | ||
) | ||
.route("/v1/:forge/:user/:repo/t/:version", get(get_repo_version)) | ||
.route("/v1/:forge/:user/:repo/tag/:version", get(get_repo_version)) | ||
.route("/v1/:forge/:user/:repo", get(get_repo)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
pub async fn github_api_get_latest_tag( | ||
user: String, | ||
repo: String, | ||
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { | ||
use reqwest::{ | ||
header::{ACCEPT, USER_AGENT}, | ||
Url, | ||
}; | ||
let version_uri = Url::parse(&format!( | ||
"http://api.github.com/repos/{}/{}/releases?per_page=1", | ||
user, repo | ||
))?; | ||
trace!("{:#?}", version_uri); | ||
let client = reqwest::Client::builder().user_agent(USER_AGENT).build()?; | ||
let res = client | ||
.get(version_uri) | ||
.header(ACCEPT, "application/vnd.github+json") | ||
.header("X-GitHub-Api-Version", "2022-11-28") | ||
.send() | ||
.await? | ||
.json::<serde_json::Value>() | ||
.await?; | ||
|
||
trace!("got:\n {:#?}", res[0]["tag_name"]); | ||
|
||
Ok(res[0]["tag_name"] | ||
.as_str() | ||
.expect("failed to get release tag-name as_str()") | ||
.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
mod github_api_get_latest_tag; | ||
pub use self::github_api_get_latest_tag::github_api_get_latest_tag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pub mod routes; | ||
|
||
mod github; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: 2023 Christina Sørensen | ||
// SPDX-FileContributor: Christina Sørensen | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use super::github::routes::get_routes as get_github_routes; | ||
use axum::Router; | ||
|
||
pub fn get_routes() -> Router { | ||
Router::new().merge(get_github_routes()) | ||
} |
Oops, something went wrong.