Removed engine from context

This commit is contained in:
perp 2024-06-04 16:02:59 +01:00
parent 29f0900068
commit 96268f8d7e
4 changed files with 20 additions and 17 deletions

View File

@ -48,11 +48,11 @@ func main() {
ctx := context.New(cfg, db)
// Create router
router.New(ctx)
r := router.New(ctx)
// Create server
server := &http.Server{
Handler: ctx.Engine,
Handler: r,
Addr: cfg.Server.Address,
ReadTimeout: time.Second * time.Duration(cfg.Server.ReadTimeout),
WriteTimeout: time.Second * time.Duration(cfg.Server.WriteTimeout),

View File

@ -24,24 +24,13 @@ import (
// Request context
type Context struct {
*gin.Context
*gin.Engine
Db *database.Database
Config *config.Config
}
// Return a new Context
func New(cfg *config.Config, db *database.Database) *Context {
// Create engine
engine := gin.New()
engine.Use(gin.Recovery())
// Log level
if cfg.Log.Level == "debug" {
engine.Use(gin.Logger())
}
return &Context{
Engine: engine,
Db: db,
Config: cfg,
}

View File

@ -17,6 +17,7 @@ package v1
import (
"git.supernets.org/perp/gopay/internal/context"
"git.supernets.org/perp/gopay/internal/router/api/v1/account"
"github.com/gin-gonic/gin"
)
// @title GoPay API v1
@ -30,8 +31,8 @@ import (
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// Register v1 routes
func Register(ctx *context.Context) {
v1 := ctx.Group("v1")
func Register(ctx *context.Context, engine *gin.Engine) {
v1 := engine.Group("v1")
{
v1.GET("scalar", ctx.Route(Spec))

View File

@ -17,9 +17,22 @@ package router
import (
"git.supernets.org/perp/gopay/internal/context"
v1 "git.supernets.org/perp/gopay/internal/router/api/v1"
"github.com/gin-gonic/gin"
)
// Create a new router
func New(ctx *context.Context) {
v1.Register(ctx)
func New(ctx *context.Context) *gin.Engine {
// Create engine
engine := gin.New()
engine.Use(gin.Recovery())
// Log level
if ctx.Config.Log.Level == "debug" {
engine.Use(gin.Logger())
}
// Register v1
v1.Register(ctx, engine)
return engine
}