diff --git a/src/main.rs b/src/main.rs index 93c39a9..fac4e19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,6 +114,10 @@ fn main() -> Result<()> { "/github/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(handle_request::), ) + .route( + "/bitbucket/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(handle_request::), + ) // .default_service(web::resource("").route(web::get().to_async(p404))) }) // .workers(OPT.workers) diff --git a/src/service.rs b/src/service.rs index 60d19f1..0bf53cb 100644 --- a/src/service.rs +++ b/src/service.rs @@ -15,6 +15,22 @@ impl ApiResponse for GitHubApiResponse { } } +#[derive(Deserialize)] +pub(crate) struct BitbucketApiResponse { + values: Vec, +} + +#[derive(Deserialize)] +pub(crate) struct BitbucketEntry { + pub(crate) hash: String, +} + +impl ApiResponse for BitbucketApiResponse { + fn commit_ref(&self) -> &str { + &self.values[0].hash + } +} + pub(crate) trait Service { type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static; fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String; @@ -45,3 +61,27 @@ impl Service for Github { format!("/github/{}/{}/{}/{}", user, repo, commit, file) } } + +pub(crate) struct Bitbucket; + +impl Service for Bitbucket { + type Response = BitbucketApiResponse; + + fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String { + format!( + "https://bitbucket.org/{}/{}/raw/{}/{}", + user, repo, commit, file + ) + } + + fn api_url(path: &FilePath) -> String { + format!( + "https://api.bitbucket.org/2.0/repositories/{}/{}/commits/{}?pagelen=1", + path.user, path.repo, path.commit + ) + } + + fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String { + format!("/bitbucket/{}/{}/{}/{}", user, repo, commit, file) + } +}