Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(main): Use nested routing #6

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ use super::v1::routes::get_routes as get_v1_routes;
use axum::Router;

pub fn get_routes() -> Router {
Router::new().merge(get_v1_routes())
Router::new().nest("/v1", get_v1_routes())
}
7 changes: 2 additions & 5 deletions src/api/v1/flakehub/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use axum::{routing::get, Router};

pub fn get_routes() -> Router {
Router::new()
.route("/v1/flakehub/:user/:repo/v/:version", get(get_repo_version))
.route(
"/v1/flakehub/:user/:repo/version/:version",
get(get_repo_version),
)
.route("/:user/:repo/v/:version", get(get_repo_version))
.route("/:user/:repo/version/:version", get(get_repo_version))
}
34 changes: 11 additions & 23 deletions src/api/v1/forgejo/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ use axum::{routing::get, Router};

pub fn get_routes() -> Router {
Router::new()
.route("/v1/forgejo/:host/:user/:repo/b/:branch", get(get_repo_ref))
.route(
"/v1/forgejo/:host/:user/:repo/branch/:branch",
get(get_repo_ref),
)
.route(
"/v1/forgejo/:host/:user/:repo/v/:version",
get(get_repo_ref),
)
.route(
"/v1/forgejo/:host/:user/:repo/version/:version",
get(get_repo_ref),
)
.route(
"/v1/forgejo/:host/:user/:repo/t/:version",
get(get_repo_ref),
)
.route(
"/v1/forgejo/:host/:user/:repo/tag/:version",
get(get_repo_ref),
)
.route("/v1/forgejo/:host/:user/:repo", get(get_repo))
.route("/v1/codeberg/*req", get(codeberg_redirect))
.route("/:host/:user/:repo/b/:branch", get(get_repo_ref))
.route("/:host/:user/:repo/branch/:branch", get(get_repo_ref))
.route("/:host/:user/:repo/v/:version", get(get_repo_ref))
.route("/:host/:user/:repo/version/:version", get(get_repo_ref))
.route("/:host/:user/:repo/t/:version", get(get_repo_ref))
.route("/:host/:user/:repo/tag/:version", get(get_repo_ref))
.route("/:host/:user/:repo", get(get_repo))
}

pub fn get_redirect_routes() -> Router {
Router::new().route("/codeberg/*req", get(codeberg_redirect))
}
5 changes: 2 additions & 3 deletions src/api/v1/github/endpoints/get_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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)>,
Path((user, repo)): Path<(String, String)>,
request: Request<Body>,
) -> impl IntoResponse {
if repo.ends_with(".tar.gz") {
Expand All @@ -30,8 +30,7 @@ pub async fn get_repo(
.await
.expect("failed to await github_api_get_latest_tag");
let result_uri = format!(
"http://{}.com/{}/{}/archive/refs/tags/{}.tar.gz",
forge,
"http://github.com/{}/{}/archive/refs/tags/{}.tar.gz",
user,
repo.strip_suffix(".tar.gz")
.expect("couldn't strip .tar.gz suffix"),
Expand Down
5 changes: 2 additions & 3 deletions src/api/v1/github/endpoints/get_repo_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ use axum::{
use log::{debug, error, info, trace, warn};

pub async fn get_repo_branch(
Path((forge, user, repo, branch)): Path<(String, String, String, String)>,
Path((user, repo, branch)): Path<(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,
"https://github.com/{}/{}/archive/refs/heads/{}.tar.gz",
user,
repo,
branch
Expand Down
5 changes: 2 additions & 3 deletions src/api/v1/github/endpoints/get_repo_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ use axum::{
use log::{debug, error, info, trace, warn};

pub async fn get_repo_version(
Path((forge, user, repo, version)): Path<(String, String, String, String)>,
Path((user, repo, version)): Path<(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,
"https://github.com/{}/{}/archive/refs/tags/{}.tar.gz",
user,
repo,
version
Expand Down
20 changes: 7 additions & 13 deletions src/api/v1/github/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ 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))
.route("/:user/:repo/b/:branch", get(get_repo_branch))
.route("/:user/:repo/branch/:branch", get(get_repo_branch))
.route("/:user/:repo/v/:version", get(get_repo_version))
.route("/:user/:repo/version/:version", get(get_repo_version))
.route("/:user/:repo/t/:version", get(get_repo_version))
.route("/:user/:repo/tag/:version", get(get_repo_version))
.route("/:user/:repo", get(get_repo))
}
9 changes: 6 additions & 3 deletions src/api/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
// SPDX-License-Identifier: AGPL-3.0-only

use super::flakehub::routes::get_routes as get_flakehub_routes;
use super::forgejo::routes::get_redirect_routes as get_forgejo_redirect_routes;
use super::forgejo::routes::get_routes as get_forgejo_routes;
use super::github::routes::get_routes as get_github_routes;
use axum::Router;

pub fn get_routes() -> Router {
Router::new()
.merge(get_github_routes())
.merge(get_flakehub_routes())
.merge(get_forgejo_routes())
.nest("/github", get_github_routes())
.nest("/flakehub", get_flakehub_routes())
.nest("/forgejo", get_forgejo_routes())
.nest("/gitea", get_forgejo_routes())
.merge(get_forgejo_redirect_routes())
}