2022-01-29 21:19:38 +01:00
|
|
|
use crate::{calculate_hoc, delete_repo_and_cache, json_hoc, overview};
|
|
|
|
|
|
|
|
use actix_web::{
|
|
|
|
dev::{ServiceFactory, ServiceRequest},
|
|
|
|
web, App,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub(crate) trait Service: Sized + 'static {
|
2019-04-29 20:37:53 +02:00
|
|
|
fn domain() -> &'static str;
|
|
|
|
fn url_path() -> &'static str;
|
|
|
|
fn commit_url(repo: &str, commit_ref: &str) -> String;
|
2022-01-29 21:19:38 +01:00
|
|
|
|
|
|
|
fn register_service<T>(app: App<T>) -> App<T>
|
|
|
|
where
|
|
|
|
T: ServiceFactory<ServiceRequest, Config = (), Error = actix_web::Error, InitError = ()>,
|
|
|
|
{
|
|
|
|
let url_path = Self::url_path();
|
|
|
|
app.service(
|
|
|
|
web::resource(format!("/{url_path}/{{user}}/{{repo}}")).to(calculate_hoc::<Self>),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource(format!("/{url_path}/{{user}}/{{repo}}/delete"))
|
|
|
|
.route(web::post().to(delete_repo_and_cache::<Self>)),
|
|
|
|
)
|
|
|
|
.service(web::resource(format!("/{url_path}/{{user}}/{{repo}}/json")).to(json_hoc::<Self>))
|
|
|
|
.service(web::resource(format!("/view/{url_path}/{{user}}/{{repo}}")).to(overview::<Self>))
|
|
|
|
.service(web::resource(format!("/{url_path}/{{user}}/{{repo}}/view")).to(overview::<Self>))
|
|
|
|
}
|
2019-04-29 20:37:53 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 10:34:37 +02:00
|
|
|
#[derive(Deserialize, Serialize, Clone, Copy)]
|
|
|
|
pub enum FormService {
|
2019-05-04 15:37:29 +02:00
|
|
|
#[serde(rename = "github")]
|
|
|
|
GitHub,
|
|
|
|
#[serde(rename = "gitlab")]
|
|
|
|
Gitlab,
|
|
|
|
#[serde(rename = "bitbucket")]
|
|
|
|
Bitbucket,
|
2021-09-03 11:44:22 +02:00
|
|
|
#[serde(rename = "sourcehut")]
|
|
|
|
Sourcehut,
|
2019-05-04 15:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FormService {
|
|
|
|
pub(crate) fn url(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
FormService::GitHub => "github.com",
|
|
|
|
FormService::Gitlab => "gitlab.com",
|
|
|
|
FormService::Bitbucket => "bitbucket.org",
|
2021-09-03 11:44:22 +02:00
|
|
|
FormService::Sourcehut => "git.sr.ht",
|
2019-05-04 15:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn service(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
FormService::GitHub => "github",
|
|
|
|
FormService::Gitlab => "gitlab",
|
|
|
|
FormService::Bitbucket => "bitbucket",
|
2021-09-03 11:44:22 +02:00
|
|
|
FormService::Sourcehut => "sourcehut",
|
2019-05-04 15:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:37:53 +02:00
|
|
|
pub(crate) struct GitHub;
|
|
|
|
|
|
|
|
impl Service for GitHub {
|
|
|
|
fn domain() -> &'static str {
|
|
|
|
"github.com"
|
|
|
|
}
|
|
|
|
fn url_path() -> &'static str {
|
|
|
|
"github"
|
|
|
|
}
|
|
|
|
fn commit_url(repo: &str, commit_ref: &str) -> String {
|
|
|
|
format!("https://{}/{}/commit/{}", Self::domain(), repo, commit_ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Gitlab;
|
|
|
|
|
|
|
|
impl Service for Gitlab {
|
|
|
|
fn domain() -> &'static str {
|
|
|
|
"gitlab.com"
|
|
|
|
}
|
|
|
|
fn url_path() -> &'static str {
|
|
|
|
"gitlab"
|
|
|
|
}
|
|
|
|
fn commit_url(repo: &str, commit_ref: &str) -> String {
|
|
|
|
format!("https://{}/{}/commit/{}", Self::domain(), repo, commit_ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Bitbucket;
|
|
|
|
|
|
|
|
impl Service for Bitbucket {
|
|
|
|
fn domain() -> &'static str {
|
|
|
|
"bitbucket.org"
|
|
|
|
}
|
|
|
|
fn url_path() -> &'static str {
|
|
|
|
"bitbucket"
|
|
|
|
}
|
|
|
|
fn commit_url(repo: &str, commit_ref: &str) -> String {
|
|
|
|
format!("https://{}/{}/commits/{}", Self::domain(), repo, commit_ref)
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 11:44:22 +02:00
|
|
|
|
|
|
|
pub(crate) struct Sourcehut;
|
|
|
|
|
|
|
|
impl Service for Sourcehut {
|
|
|
|
fn domain() -> &'static str {
|
|
|
|
"git.sr.ht"
|
|
|
|
}
|
|
|
|
fn url_path() -> &'static str {
|
|
|
|
"sourcehut"
|
|
|
|
}
|
|
|
|
fn commit_url(repo: &str, commit_ref: &str) -> String {
|
|
|
|
format!("https://{}/{}/commit/{}", Self::domain(), repo, commit_ref)
|
|
|
|
}
|
|
|
|
}
|