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
|
||||
}
|
||||
}
|
||||
7
internal/interfaces/router.go
Normal file
7
internal/interfaces/router.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package interfaces
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type Router interface {
|
||||
Set() *gin.Engine
|
||||
}
|
||||
48
internal/router/handler.go
Normal file
48
internal/router/handler.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"merch-parser-api/internal/interfaces"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type router struct {
|
||||
apiPrefix string
|
||||
engine *gin.Engine
|
||||
ginMode string
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
ApiPrefix string
|
||||
GinMode string
|
||||
}
|
||||
|
||||
func NewRouter(deps Deps) interfaces.Router {
|
||||
engine := gin.Default()
|
||||
|
||||
if deps.GinMode == "release" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
err := engine.SetTrustedProxies([]string{"172.20.0.0/16"})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Router | Set proxies failed")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &router{
|
||||
apiPrefix: deps.ApiPrefix,
|
||||
engine: engine,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *router) Set() *gin.Engine {
|
||||
r.engine.NoRoute(func(c *gin.Context) {
|
||||
c.Status(http.StatusForbidden)
|
||||
})
|
||||
|
||||
//version
|
||||
r.engine.GET("/version", func(c *gin.Context) { c.JSON(200, gin.H{"version": "2.0.0-alpha"}) })
|
||||
|
||||
return r.engine
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue