goext/ginext/engine.go

53 lines
1006 B
Go
Raw Normal View History

2023-07-18 14:40:10 +02:00
package ginext
2023-07-18 15:12:06 +02:00
import (
"github.com/gin-gonic/gin"
2023-07-18 16:01:34 +02:00
"net/http"
2023-07-18 15:12:06 +02:00
"time"
)
2023-07-18 14:40:10 +02:00
type GinWrapper struct {
engine *gin.Engine
SuppressGinLogs bool
allowCors bool
ginDebug bool
returnRawErrors bool
2023-07-18 15:12:06 +02:00
requestTimeout time.Duration
2023-07-18 14:40:10 +02:00
}
2023-07-18 15:12:06 +02:00
func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time.Duration) *GinWrapper {
2023-07-18 14:40:10 +02:00
engine := gin.New()
wrapper := &GinWrapper{
engine: engine,
SuppressGinLogs: false,
allowCors: allowCors,
ginDebug: ginDebug,
returnRawErrors: returnRawErrors,
2023-07-18 15:12:06 +02:00
requestTimeout: timeout,
2023-07-18 14:40:10 +02:00
}
engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false
if allowCors {
engine.Use(CorsMiddleware())
}
if ginDebug {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if !wrapper.SuppressGinLogs {
ginlogger(context)
}
})
}
return wrapper
}
2023-07-18 16:01:34 +02:00
func (w *GinWrapper) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
w.engine.ServeHTTP(writer, request)
}