modules + collect routes
This commit is contained in:
parent
8c38107ff2
commit
a6be7a9e21
2 changed files with 33 additions and 6 deletions
|
|
@ -2,18 +2,22 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"merch-api/config"
|
"merch-api/config"
|
||||||
|
"merch-api/internal/merch"
|
||||||
"merch-api/pkg/router"
|
"merch-api/pkg/router"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
cfg config.Config
|
cfg config.Config
|
||||||
router *router.Router
|
router *router.Router
|
||||||
|
modules []Module
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg config.Config) *App {
|
func New(cfg config.Config) *App {
|
||||||
|
//providers
|
||||||
r := router.NewRouter(router.Deps{
|
r := router.NewRouter(router.Deps{
|
||||||
Host: cfg.Http.Host,
|
Host: cfg.Http.Host,
|
||||||
Port: cfg.Http.Port,
|
Port: cfg.Http.Port,
|
||||||
|
|
@ -21,15 +25,25 @@ func New(cfg config.Config) *App {
|
||||||
GinMode: cfg.Http.GinMode,
|
GinMode: cfg.Http.GinMode,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//modules
|
||||||
|
var modules []Module
|
||||||
|
|
||||||
|
m := merch.New(nil)
|
||||||
|
modules = append(modules, m)
|
||||||
|
|
||||||
return &App{
|
return &App{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
router: r,
|
router: r,
|
||||||
|
modules: modules,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Run(ctx context.Context) error {
|
func (app *App) Run(ctx context.Context) error {
|
||||||
log.Info("Starting application...")
|
log.Info("Starting application...")
|
||||||
|
|
||||||
|
baseGroup := app.router.BaseGroup()
|
||||||
|
app.collectRoutes(baseGroup)
|
||||||
|
|
||||||
errCh := make(chan error, 10)
|
errCh := make(chan error, 10)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -40,7 +54,7 @@ func (app *App) Run(ctx context.Context) error {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
app.Shutdown(ctx)
|
app.shutdown(ctx)
|
||||||
|
|
||||||
case err := <-errCh:
|
case err := <-errCh:
|
||||||
return err
|
return err
|
||||||
|
|
@ -48,7 +62,7 @@ func (app *App) Run(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Shutdown(ctx context.Context) {
|
func (app *App) shutdown(ctx context.Context) {
|
||||||
log.Info("Shutting down application...")
|
log.Info("Shutting down application...")
|
||||||
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
|
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
|
||||||
defer shutdownCancel()
|
defer shutdownCancel()
|
||||||
|
|
@ -59,3 +73,9 @@ func (app *App) Shutdown(ctx context.Context) {
|
||||||
|
|
||||||
log.Info("Application shutdown complete")
|
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