refactor: split route setup to each set of routes

This commit is contained in:
Derrick Hammer 2024-02-11 00:15:57 -05:00
parent 4456b54550
commit 5ac86cc75c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 60 additions and 28 deletions

View File

@ -80,3 +80,12 @@ func (m *manifests) handlerConversion(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
w.Write(appData) 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")
}

View File

@ -8,37 +8,23 @@ import (
"gorm.io/gorm" "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)) r.Use(loggingMiddleware(logger))
setupRouter := r.PathPrefix("/setup").Subrouter() setupApiRoutes(params)
setupRouter.Use(giteaOauthVerifyMiddleware(cfg)) setupSettingsRoutes(params)
setupManifestsRoutes(params)
setupApi := newSetupApi(cfg, logger, newOauth(cfg, logger)) setupAppRoutes(params)
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")
} }
type ClientParams struct { type ClientParams struct {

View File

@ -20,3 +20,17 @@ func (a *appApi) handlerNewAppInstall(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain") 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")
}

View File

@ -69,3 +69,15 @@ func (s settingsApi) handlerNewApp(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, redirectUrl.String(), http.StatusFound) 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")
}

View File

@ -57,3 +57,14 @@ func (s setupApi) callbackHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/setup", http.StatusFound) 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")
}