From f3e1d914c536f0c4ec79492ad63f721183fd9e61 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sat, 29 Jan 2022 21:19:38 +0100 Subject: [PATCH] Register routes per service --- src/lib.rs | 40 +++++++--------------------------------- src/service.rs | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 48b9fa5..301b000 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::)) - .service(web::resource("/gitlab/{user}/{repo}").to(calculate_hoc::)) - .service(web::resource("/bitbucket/{user}/{repo}").to(calculate_hoc::)) - .service(web::resource("/sourcehut/{user}/{repo}").to(calculate_hoc::)) - .service( - web::resource("/github/{user}/{repo}/delete") - .route(web::post().to(delete_repo_and_cache::)), - ) - .service( - web::resource("/gitlab/{user}/{repo}/delete") - .route(web::post().to(delete_repo_and_cache::)), - ) - .service( - web::resource("/bitbucket/{user}/{repo}/delete") - .route(web::post().to(delete_repo_and_cache::)), - ) - .service( - web::resource("/sourcehut/{user}/{repo}/delete") - .route(web::post().to(delete_repo_and_cache::)), - ) - .service(web::resource("/github/{user}/{repo}/json").to(json_hoc::)) - .service(web::resource("/gitlab/{user}/{repo}/json").to(json_hoc::)) - .service(web::resource("/bitbucket/{user}/{repo}/json").to(json_hoc::)) - .service(web::resource("/sourcehut/{user}/{repo}/json").to(json_hoc::)) - .service(web::resource("/view/github/{user}/{repo}").to(overview::)) - .service(web::resource("/view/gitlab/{user}/{repo}").to(overview::)) - .service(web::resource("/view/bitbucket/{user}/{repo}").to(overview::)) - .service(web::resource("/github/{user}/{repo}/view").to(overview::)) - .service(web::resource("/gitlab/{user}/{repo}/view").to(overview::)) - .service(web::resource("/bitbucket/{user}/{repo}/view").to(overview::)) - .service(web::resource("/sourcehut/{user}/{repo}/view").to(overview::)) - .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)? diff --git a/src/service.rs b/src/service.rs index e80c744..ce95c97 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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(app: App) -> App + where + T: ServiceFactory, + { + let url_path = Self::url_path(); + app.service( + web::resource(format!("/{url_path}/{{user}}/{{repo}}")).to(calculate_hoc::), + ) + .service( + web::resource(format!("/{url_path}/{{user}}/{{repo}}/delete")) + .route(web::post().to(delete_repo_and_cache::)), + ) + .service(web::resource(format!("/{url_path}/{{user}}/{{repo}}/json")).to(json_hoc::)) + .service(web::resource(format!("/view/{url_path}/{{user}}/{{repo}}")).to(overview::)) + .service(web::resource(format!("/{url_path}/{{user}}/{{repo}}/view")).to(overview::)) + } } #[derive(Deserialize, Serialize)]