Initial
This commit is contained in:
commit
995ea60f34
11 changed files with 449 additions and 0 deletions
58
internal/app/handler.go
Normal file
58
internal/app/handler.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue