Register routes per service
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Valentin Brandl 2022-01-29 21:19:38 +01:00
parent e282316769
commit f3e1d914c5
2 changed files with 32 additions and 34 deletions

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())); web::Data::new(AtomicUsize::new(count::count_repositories(&settings.repodir).unwrap()));
let state = web::Data::new(State { settings }); let state = web::Data::new(State { settings });
Ok(HttpServer::new(move || { Ok(HttpServer::new(move || {
App::new() let app = App::new()
.app_data(state.clone()) .app_data(state.clone())
.app_data(repo_count.clone()) .app_data(repo_count.clone())
.wrap(tracing_actix_web::TracingLogger::default()) .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("/tacit-css.min.css").route(web::get().to(css)))
.service(web::resource("/favicon.ico").route(web::get().to(favicon32))) .service(web::resource("/favicon.ico").route(web::get().to(favicon32)))
.service(generate) .service(generate)
.service(web::resource("/github/{user}/{repo}").to(calculate_hoc::<GitHub>)) .default_service(web::to(async_p404));
.service(web::resource("/gitlab/{user}/{repo}").to(calculate_hoc::<Gitlab>)) let app = GitHub::register_service(app);
.service(web::resource("/bitbucket/{user}/{repo}").to(calculate_hoc::<Bitbucket>)) let app = Gitlab::register_service(app);
.service(web::resource("/sourcehut/{user}/{repo}").to(calculate_hoc::<Sourcehut>)) let app = Bitbucket::register_service(app);
.service( let app = Sourcehut::register_service(app);
web::resource("/github/{user}/{repo}/delete") app
.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))
}) })
.workers(workers) .workers(workers)
.listen(listener)? .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 domain() -> &'static str;
fn url_path() -> &'static str; fn url_path() -> &'static str;
fn commit_url(repo: &str, commit_ref: &str) -> String; 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)] #[derive(Deserialize, Serialize)]