gin router added
This commit is contained in:
parent
d827debd7f
commit
f56a39d065
5 changed files with 319 additions and 9 deletions
|
|
@ -4,26 +4,58 @@ import (
|
|||
"context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"merch-api/config"
|
||||
"merch-api/pkg/router"
|
||||
"time"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
cfg config.Config
|
||||
cfg config.Config
|
||||
router *router.Router
|
||||
}
|
||||
|
||||
func New(cfg config.Config) *App {
|
||||
return &App{cfg: cfg}
|
||||
r := router.NewRouter(router.Deps{
|
||||
Host: cfg.Http.Host,
|
||||
Port: cfg.Http.Port,
|
||||
Prefix: cfg.Http.Prefix,
|
||||
GinMode: cfg.Http.GinMode,
|
||||
})
|
||||
|
||||
return &App{
|
||||
cfg: cfg,
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (app *App) Run(ctx context.Context) error {
|
||||
log.Info("Starting application...")
|
||||
|
||||
errCh := make(chan error, 10)
|
||||
|
||||
go func() {
|
||||
if err := app.router.Run(); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
app.Shutdown(ctx)
|
||||
|
||||
case err := <-errCh:
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *App) Shutdown(ctx context.Context) {
|
||||
log.Info("Shutting down application...")
|
||||
return
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer shutdownCancel()
|
||||
|
||||
if err := app.router.Shutdown(shutdownCtx); err != nil {
|
||||
log.Warnf("Error shutting down application: %v", err)
|
||||
}
|
||||
|
||||
log.Info("Application shutdown complete")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue