modules + collect routes
This commit is contained in:
parent
8c38107ff2
commit
a6be7a9e21
2 changed files with 33 additions and 6 deletions
|
|
@ -2,8 +2,10 @@ 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"
|
||||
)
|
||||
|
|
@ -11,9 +13,11 @@ import (
|
|||
type App struct {
|
||||
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,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
internal/app/interfaces.go
Normal file
7
internal/app/interfaces.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package app
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type Module interface {
|
||||
RegisterRoutes(r *gin.RouterGroup)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue