From 5ac86cc75cb56851fb25f1880323442648ccfee0 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 11 Feb 2024 00:15:57 -0500 Subject: [PATCH] refactor: split route setup to each set of routes --- api/route_manifests.go | 9 +++++++++ api/routes.go | 42 ++++++++++++++---------------------------- api/routes_app.go | 14 ++++++++++++++ api/routes_settings.go | 12 ++++++++++++ api/routes_setup.go | 11 +++++++++++ 5 files changed, 60 insertions(+), 28 deletions(-) diff --git a/api/route_manifests.go b/api/route_manifests.go index 7750801..80424ba 100644 --- a/api/route_manifests.go +++ b/api/route_manifests.go @@ -80,3 +80,12 @@ func (m *manifests) handlerConversion(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) w.Write(appData) } + +func setupManifestsRoutes(params RouteParams) { + r := params.R + + manifestApi := newManifests(params.Config, params.Db, params.Logger) + manifestsRouter := r.PathPrefix("/api/v3/app-manifests").Subrouter() + + manifestsRouter.HandleFunc("/{code}/conversions", manifestApi.handlerConversion).Methods("POST") +} diff --git a/api/routes.go b/api/routes.go index 8fc3b14..b0db9e0 100644 --- a/api/routes.go +++ b/api/routes.go @@ -8,37 +8,23 @@ import ( "gorm.io/gorm" ) -func SetupRoutes(r *mux.Router, cfg *config.Config, db *gorm.DB, logger *zap.Logger) { +type RouteParams struct { + Config *config.Config + Db *gorm.DB + Logger *zap.Logger + R *mux.Router +} + +func SetupRoutes(params RouteParams) { + logger := params.Logger + r := params.R r.Use(loggingMiddleware(logger)) - setupRouter := r.PathPrefix("/setup").Subrouter() - setupRouter.Use(giteaOauthVerifyMiddleware(cfg)) - - setupApi := newSetupApi(cfg, logger, newOauth(cfg, logger)) - setupRouter.HandleFunc("", setupApi.setupHandler).Methods("GET") - setupRouter.HandleFunc("/callback", setupApi.callbackHandler).Methods("GET") - - settingsRouter := r.PathPrefix("/settings").Subrouter() - settingsRouter.Use(giteaOauthVerifyMiddleware(cfg)) - settingsRouter.Use(requireAuthMiddleware(cfg)) - - settingsApi := newSettingsApi(cfg, db, logger) - - settingsRouter.HandleFunc("/apps/new", settingsApi.handlerNewApp).Methods("POST") - - manifestApi := newManifests(cfg, db, logger) - manifestsRouter := r.PathPrefix("/api/v3/app-manifests").Subrouter() - - manifestsRouter.HandleFunc("/{code}/conversions", manifestApi.handlerConversion).Methods("POST") - - appApi := newAppApi(cfg, logger) - appRouter := r.PathPrefix("/apps").Subrouter() - - appRouter.Use(giteaOauthVerifyMiddleware(cfg)) - appRouter.Use(requireAuthMiddleware(cfg)) - - appRouter.HandleFunc("/{app}/installations/new", appApi.handlerNewAppInstall).Methods("GET") + setupApiRoutes(params) + setupSettingsRoutes(params) + setupManifestsRoutes(params) + setupAppRoutes(params) } type ClientParams struct { diff --git a/api/routes_app.go b/api/routes_app.go index 34b8f65..e7ed615 100644 --- a/api/routes_app.go +++ b/api/routes_app.go @@ -20,3 +20,17 @@ func (a *appApi) handlerNewAppInstall(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "text/plain") } + +func setupAppRoutes(params RouteParams) { + logger := params.Logger + cfg := params.Config + r := params.R + + appApi := newAppApi(cfg, logger) + appRouter := r.PathPrefix("/apps").Subrouter() + + appRouter.Use(giteaOauthVerifyMiddleware(cfg)) + appRouter.Use(requireAuthMiddleware(cfg)) + + appRouter.HandleFunc("/{app}/installations/new", appApi.handlerNewAppInstall).Methods("GET") +} diff --git a/api/routes_settings.go b/api/routes_settings.go index d965c4f..ae591c3 100644 --- a/api/routes_settings.go +++ b/api/routes_settings.go @@ -69,3 +69,15 @@ func (s settingsApi) handlerNewApp(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, redirectUrl.String(), http.StatusFound) } + +func setupSettingsRoutes(params RouteParams) { + r := params.R + + settingsRouter := r.PathPrefix("/settings").Subrouter() + settingsRouter.Use(giteaOauthVerifyMiddleware(params.Config)) + settingsRouter.Use(requireAuthMiddleware(params.Config)) + + settingsApi := newSettingsApi(params.Config, params.Db, params.Logger) + + settingsRouter.HandleFunc("/apps/new", settingsApi.handlerNewApp).Methods("POST") +} diff --git a/api/routes_setup.go b/api/routes_setup.go index f3708ae..2681a0c 100644 --- a/api/routes_setup.go +++ b/api/routes_setup.go @@ -57,3 +57,14 @@ func (s setupApi) callbackHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/setup", http.StatusFound) } + +func setupApiRoutes(params RouteParams) { + r := params.R + + setupRouter := r.PathPrefix("/setup").Subrouter() + setupRouter.Use(giteaOauthVerifyMiddleware(params.Config)) + + setupApi := newSetupApi(params.Config, params.Logger, newOauth(params.Config, params.Logger)) + setupRouter.HandleFunc("", setupApi.setupHandler).Methods("GET") + setupRouter.HandleFunc("/callback", setupApi.callbackHandler).Methods("GET") +}