server better organized

This commit is contained in:
2026-01-24 12:41:32 -07:00
parent 7154494016
commit ff231897df
3 changed files with 43 additions and 57 deletions
+32 -51
View File
@@ -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"),