From a6be7a9e21a43ed76b5933ce5e44b83143657b73 Mon Sep 17 00:00:00 2001 From: nquidox Date: Mon, 23 Feb 2026 20:02:53 +0300 Subject: [PATCH] modules + collect routes --- internal/app/handler.go | 32 ++++++++++++++++++++++++++------ internal/app/interfaces.go | 7 +++++++ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 internal/app/interfaces.go diff --git a/internal/app/handler.go b/internal/app/handler.go index d523013..fe65524 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -2,18 +2,22 @@ package app import ( "context" + "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-api/config" + "merch-api/internal/merch" "merch-api/pkg/router" "time" ) type App struct { - cfg config.Config - router *router.Router + cfg config.Config + router *router.Router + modules []Module } func New(cfg config.Config) *App { + //providers r := router.NewRouter(router.Deps{ Host: cfg.Http.Host, Port: cfg.Http.Port, @@ -21,15 +25,25 @@ func New(cfg config.Config) *App { GinMode: cfg.Http.GinMode, }) + //modules + var modules []Module + + m := merch.New(nil) + modules = append(modules, m) + return &App{ - cfg: cfg, - router: r, + cfg: cfg, + router: r, + modules: modules, } } func (app *App) Run(ctx context.Context) error { log.Info("Starting application...") + baseGroup := app.router.BaseGroup() + app.collectRoutes(baseGroup) + errCh := make(chan error, 10) go func() { @@ -40,7 +54,7 @@ func (app *App) Run(ctx context.Context) error { select { case <-ctx.Done(): - app.Shutdown(ctx) + app.shutdown(ctx) case err := <-errCh: return err @@ -48,7 +62,7 @@ func (app *App) Run(ctx context.Context) error { return nil } -func (app *App) Shutdown(ctx context.Context) { +func (app *App) shutdown(ctx context.Context) { log.Info("Shutting down application...") shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second) defer shutdownCancel() @@ -59,3 +73,9 @@ func (app *App) Shutdown(ctx context.Context) { log.Info("Application shutdown complete") } + +func (app *App) collectRoutes(group *gin.RouterGroup) { + for _, m := range app.modules { + m.RegisterRoutes(group) + } +} diff --git a/internal/app/interfaces.go b/internal/app/interfaces.go new file mode 100644 index 0000000..95f3712 --- /dev/null +++ b/internal/app/interfaces.go @@ -0,0 +1,7 @@ +package app + +import "github.com/gin-gonic/gin" + +type Module interface { + RegisterRoutes(r *gin.RouterGroup) +}