Skip to content

Commit

Permalink
Redefine handler interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Mar 31, 2024
1 parent bf7722b commit 492228f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 46 deletions.
15 changes: 10 additions & 5 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ package handlers

import (
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/services"
)

var handlers []Routable
var handlers []Handler

type Routable interface {
type Handler interface {
// Routes allows for self-registration of HTTP routes on the router
Routes(g *echo.Group)

// Init provides the service container to initialize
Init(*services.Container) error
}

func Register(r Routable) {
handlers = append(handlers, r)
func Register(h Handler) {
handlers = append(handlers, h)
}

func GetHandlers() []Routable {
func GetHandlers() []Handler {
return handlers
}
11 changes: 5 additions & 6 deletions pkg/handlers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildRouter(c *services.Container) error {
c.Web.Group("", middleware.CacheControl(c.Config.Cache.Expiration.StaticFile)).
Static(config.StaticPrefix, config.StaticDir)

// Non static file route group
// Non-static file route group
g := c.Web.Group("")

// Force HTTPS, if enabled
Expand Down Expand Up @@ -53,11 +53,10 @@ func BuildRouter(c *services.Container) error {
err := Error{Controller: controller.NewController(c)}
c.Web.HTTPErrorHandler = err.Page

for _, h := range handlers {
if h, okay := h.(services.Initializer); okay {
if err := h.Init(c); err != nil {
return err
}
// Initialize and register all handlers
for _, h := range GetHandlers() {
if err := h.Init(c); err != nil {
return err
}

h.Routes(g)
Expand Down
29 changes: 0 additions & 29 deletions pkg/handlers/tester.go

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/services/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
_ "github.com/mikestefanello/pagoda/ent/runtime"
)

// Initializer is implemented by anything that requires a service container to initialize
type Initializer interface {
// Init provides the service container to initialize
Init(*Container) error
}

// Container contains all services used by the application and provides an easy way to handle dependency
// injection including within tests
type Container struct {
Expand Down

0 comments on commit 492228f

Please sign in to comment.