Merge pull request #408 from vbrandl/feature/service-registration
All checks were successful
continuous-integration/drone/push Build is passing

Unify registration of service routes
This commit is contained in:
Valentin Brandl 2022-01-29 21:29:06 +01:00 committed by GitHub
commit d91e6bf580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 300 additions and 432 deletions

660
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@ name = "hoc"
[dependencies]
actix-rt = "2.6.0"
actix-web = "4.0.0-beta.8"
actix-web = "4.0.0-beta.21"
badge = "0.3.0"
bytes = "1.1.0"
config = { version = "0.11.0", features = ["toml"] }
@ -29,7 +29,7 @@ serde = "1.0.136"
serde_derive = "1.0.130"
serde_json = "1.0.78"
tracing = "0.1.29"
tracing-actix-web = "0.5.0-beta.1"
tracing-actix-web = "0.5.0-beta.11"
tracing-bunyan-formatter = "0.3.2"
tracing-futures = "0.2.5"
tracing-log = "0.1.2"
@ -40,7 +40,7 @@ ructe = "0.13.4"
vergen = { version = "6.0.2", default-features = false, features = ["git"] }
[dev-dependencies]
awc = "3.0.0-beta.10"
awc = "3.0.0-beta.19"
ructe = "0.13.4"
tempfile = "3.3.0"
tokio = "1.16.0"

View File

@ -477,7 +477,7 @@ async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Res
web::Data::new(AtomicUsize::new(count::count_repositories(&settings.repodir).unwrap()));
let state = web::Data::new(State { settings });
Ok(HttpServer::new(move || {
App::new()
let app = App::new()
.app_data(state.clone())
.app_data(repo_count.clone())
.wrap(tracing_actix_web::TracingLogger::default())
@ -487,38 +487,12 @@ async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Res
.service(web::resource("/tacit-css.min.css").route(web::get().to(css)))
.service(web::resource("/favicon.ico").route(web::get().to(favicon32)))
.service(generate)
.service(web::resource("/github/{user}/{repo}").to(calculate_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}").to(calculate_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}").to(calculate_hoc::<Bitbucket>))
.service(web::resource("/sourcehut/{user}/{repo}").to(calculate_hoc::<Sourcehut>))
.service(
web::resource("/github/{user}/{repo}/delete")
.route(web::post().to(delete_repo_and_cache::<GitHub>)),
)
.service(
web::resource("/gitlab/{user}/{repo}/delete")
.route(web::post().to(delete_repo_and_cache::<Gitlab>)),
)
.service(
web::resource("/bitbucket/{user}/{repo}/delete")
.route(web::post().to(delete_repo_and_cache::<Bitbucket>)),
)
.service(
web::resource("/sourcehut/{user}/{repo}/delete")
.route(web::post().to(delete_repo_and_cache::<Sourcehut>)),
)
.service(web::resource("/github/{user}/{repo}/json").to(json_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}/json").to(json_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}/json").to(json_hoc::<Bitbucket>))
.service(web::resource("/sourcehut/{user}/{repo}/json").to(json_hoc::<Sourcehut>))
.service(web::resource("/view/github/{user}/{repo}").to(overview::<GitHub>))
.service(web::resource("/view/gitlab/{user}/{repo}").to(overview::<Gitlab>))
.service(web::resource("/view/bitbucket/{user}/{repo}").to(overview::<Bitbucket>))
.service(web::resource("/github/{user}/{repo}/view").to(overview::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}/view").to(overview::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}/view").to(overview::<Bitbucket>))
.service(web::resource("/sourcehut/{user}/{repo}/view").to(overview::<Sourcehut>))
.default_service(web::to(async_p404))
.default_service(web::to(async_p404));
let app = GitHub::register_service(app);
let app = Gitlab::register_service(app);
let app = Bitbucket::register_service(app);
let app = Sourcehut::register_service(app);
app
})
.workers(workers)
.listen(listener)?

View File

@ -1,7 +1,31 @@
pub(crate) trait Service {
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 {
fn domain() -> &'static str;
fn url_path() -> &'static str;
fn commit_url(repo: &str, commit_ref: &str) -> String;
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>))
}
}
#[derive(Deserialize, Serialize)]