Skip to content

Commit

Permalink
refactor(main): move routing out into tree structure
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Sørensen <christina@cafkafk.com>
  • Loading branch information
cafkafk committed Oct 28, 2023
1 parent 00e9bf4 commit dd647ce
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 145 deletions.
8 changes: 8 additions & 0 deletions src/api/mod.rs
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;
11 changes: 11 additions & 0 deletions src/api/routes.rs
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())
}
50 changes: 50 additions & 0 deletions src/api/v1/github/endpoints/get_repo.rs
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()
}
}
39 changes: 39 additions & 0 deletions src/api/v1/github/endpoints/get_repo_branch.rs
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()
}
}
39 changes: 39 additions & 0 deletions src/api/v1/github/endpoints/get_repo_version.rs
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()
}
}
11 changes: 11 additions & 0 deletions src/api/v1/github/endpoints/mod.rs
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;
9 changes: 9 additions & 0 deletions src/api/v1/github/mod.rs
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;
25 changes: 25 additions & 0 deletions src/api/v1/github/routes.rs
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))
}
38 changes: 38 additions & 0 deletions src/api/v1/github/utils/github_api_get_latest_tag.rs
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())
}
7 changes: 7 additions & 0 deletions src/api/v1/github/utils/mod.rs
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;
8 changes: 8 additions & 0 deletions src/api/v1/mod.rs
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;
11 changes: 11 additions & 0 deletions src/api/v1/routes.rs
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())
}
Loading

0 comments on commit dd647ce

Please sign in to comment.