58 lines
1,022 B
Go
58 lines
1,022 B
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"merch-parser-api/internal/interfaces"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type App struct {
|
|
address string
|
|
routerHandler interfaces.Router
|
|
router *gin.Engine
|
|
}
|
|
|
|
type Deps struct {
|
|
Host string
|
|
Port string
|
|
Router interfaces.Router
|
|
}
|
|
|
|
func NewApp(deps Deps) *App {
|
|
app := &App{
|
|
address: deps.Host + ":" + deps.Port,
|
|
routerHandler: deps.Router,
|
|
}
|
|
|
|
app.router = app.routerHandler.Set()
|
|
|
|
return app
|
|
}
|
|
|
|
func (a *App) Run(ctx context.Context) error {
|
|
server := &http.Server{
|
|
Addr: a.address,
|
|
Handler: a.router,
|
|
}
|
|
|
|
serverErr := make(chan error, 1)
|
|
|
|
go func() {
|
|
log.Info("Starting server on: ", a.address)
|
|
serverErr <- server.ListenAndServe()
|
|
}()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Info("Shutting down server")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
return server.Shutdown(shutdownCtx)
|
|
|
|
case err := <-serverErr:
|
|
return err
|
|
}
|
|
}
|