From ff231897dfc04976feaa71a2c5d74c68fa10c9f0 Mon Sep 17 00:00:00 2001 From: Angel Beltran Date: Sat, 24 Jan 2026 12:41:32 -0700 Subject: [PATCH] server better organized --- internal/server/api/templates/router.go | 4 +- internal/server/middleware/auth.go | 13 ++-- internal/server/server.go | 83 ++++++++++--------------- 3 files changed, 43 insertions(+), 57 deletions(-) diff --git a/internal/server/api/templates/router.go b/internal/server/api/templates/router.go index 3095213..26e122c 100644 --- a/internal/server/api/templates/router.go +++ b/internal/server/api/templates/router.go @@ -47,7 +47,7 @@ func SetupRoutes( rawEvents *raw_events.Store, accts *accounts.Store, etsy *etsy_platform.Platform, - auth *middleware.Auth, + authenticate gin.HandlerFunc, ) { s := &webpageRouter{ @@ -98,7 +98,7 @@ func SetupRoutes( } r.GET("", response.Handler(s.serveTemplate)) - r.GET("/*rest", response.Handler(auth.Authenticate()), response.Handler(s.serveTemplate)) + r.GET("/*rest", authenticate, response.Handler(s.serveTemplate)) } // GET / diff --git a/internal/server/middleware/auth.go b/internal/server/middleware/auth.go index bcb75af..9efed80 100644 --- a/internal/server/middleware/auth.go +++ b/internal/server/middleware/auth.go @@ -49,8 +49,8 @@ func NewAuth( } } -// AddIdentity will add an Identity to the context that can then be retrieved via GetIdentity. -func (a *Auth) AddIdentity(c *gin.Context) { +// Identify will add an Identity to the context that can then be retrieved via GetIdentity. +func (a *Auth) Identify(c *gin.Context) { if err := a.addIdentity(c); err != nil { c.Error(err) c.Abort() @@ -98,9 +98,14 @@ func (a *Auth) addIdentity(c *gin.Context) error { return nil } -// Authenticate should only be used along with and after AddIdentity +// Authenticate should only be used along with and after Identify // Typically used with response.Handler to make a gin.HandlerFunc. -func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) { +func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) { + return response.Handler(a.AuthenticateHandler(assertions...)) +} + +// See Authenticate. +func (a *Auth) AuthenticateHandler(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) { return func(c *gin.Context) (response.Response, error) { id, ok := getIdentity(c) if !ok { diff --git a/internal/server/server.go b/internal/server/server.go index 01962be..71950de 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -39,26 +39,38 @@ func NewRouter( etsy *etsy_platform.Platform, auth *authentication.Authenticator, ) *Router { - r := gin.Default() + authM := middleware.NewAuth( + logger.WithGroup("auth-middleware"), + auth, + accts, + ) + r := gin.Default() r.Use( + authM.Identify, response.HandleResponses, response.HandleErrors, ) - // TODO: shouldn't this ACTUALLY be a middleware? - // - only try to make this an actual middleware AFTER all the routers are broken out, so that way how the middleware is supposed to work can be known - authM := middleware.NewAuth( - logger.WithGroup("auth-middleware"), - auth, - //auth_api.NewLoginURL, - accts, - ) - // webpage content - // non-html content: scripts, styles, images, etc + // top level GET is assumed to be for the home page. + r.GET("/", func(c *gin.Context) { + c.Redirect(http.StatusMovedPermanently, "/ui") + }) + templates_api.SetupRoutes( + logger.WithGroup("templates"), + r.Group("/ui"), + "/ui", + contentDir, + rawEvents, + accts, + etsy, + authM.Authenticate(), + ) + + // non-html content: scripts, styles, images, etc r.Use(fileServer("/scripts", contentDir+"/scripts", func(c *gin.Context) { w := c.Writer w.Header().Set("Content-Type", "text/javascript") @@ -70,40 +82,17 @@ func NewRouter( r.Static("/favicon", "./favicon") r.Static("/images", "./images") - // html (must be before the api endpoints, because the webpage middleware has to be installed beforehand - - r.GET("/", func(c *gin.Context) { - c.Redirect(http.StatusMovedPermanently, "/ui") - }) - - // kind of a dumb way to capture routes for webpages - templates_api.SetupRoutes( - logger.WithGroup("templates"), - r.Group( - "/ui", - authM.AddIdentity, - ), - "/ui", - contentDir, - rawEvents, - accts, - etsy, - authM, - ) - - // sse setup - - sq := sse.NewQueue() - unp := middleware.NewUpdateNotificationPublisher( - logger.WithGroup("update.notification.publisher"), - sq, - ) - // api endpoints api := r.Group("/api") apiLogger := logger.WithGroup("/api") - unp = unp.Trim("/api") + + // sse setup + sq := sse.NewQueue() + unp := middleware.NewUpdateNotificationPublisher( + logger.WithGroup("update.notification.publisher"), + sq, + ).Trim("/api") auth_api.Routes( api.Group("/auth"), @@ -111,20 +100,12 @@ func NewRouter( auth, ) sse_api.Routes( - api.Group( - "/events", - authM.AddIdentity, - response.Handler(authM.Authenticate()), - ), + api.Group("/events", authM.Authenticate()), apiLogger.WithGroup("/events"), sq, ) accounts_api.Routes( - api.Group( - "/accounts", - authM.AddIdentity, - response.Handler(authM.Authenticate()), - ), + api.Group("/accounts", authM.Authenticate()), apiLogger.WithGroup("/accounts"), accts, unp.Group("/accounts"),