Skip to content

Commit

Permalink
Use fuego.Flow instead of redefining the flow in GinHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 22, 2024
1 parent 785b66d commit 2140538
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
14 changes: 1 addition & 13 deletions extra/fuegogin/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,7 @@ func GinHandler[B, T any](engine *fuego.Engine, handler func(c fuego.ContextWith
ginCtx: c,
}

resp, err := handler(context)
if err != nil {
err = engine.ErrorHandler(err)
c.JSON(getErrorCode(err), err)
return
}

if c.Request.Header.Get("Accept") == "application/xml" {
c.XML(200, resp)
return
}

c.JSON(200, resp)
fuego.Flow(engine, context, handler)
}
}

Expand Down
31 changes: 30 additions & 1 deletion extra/fuegogin/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ type ginContext[B any] struct {
ginCtx *gin.Context
}

var _ fuego.ContextWithBody[any] = &ginContext[any]{}
var (
_ fuego.ContextWithBody[any] = &ginContext[any]{}
_ fuego.ContextFlowable[any] = &ginContext[any]{}
)

func (c ginContext[B]) Body() (B, error) {
var body B
Expand Down Expand Up @@ -77,10 +80,36 @@ func (c ginContext[B]) SetCookie(cookie http.Cookie) {
c.ginCtx.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly)
}

func (c ginContext[B]) HasCookie(name string) bool {
_, err := c.Cookie(name)
return err == nil
}

func (c ginContext[B]) HasHeader(key string) bool {
_, ok := c.ginCtx.Request.Header[key]
return ok
}

func (c ginContext[B]) SetHeader(key, value string) {
c.ginCtx.Header(key, value)
}

func (c ginContext[B]) SetStatus(code int) {
c.ginCtx.Status(code)
}

func (c ginContext[B]) Serialize(data any) error {
c.ginCtx.JSON(http.StatusOK, data)
return nil
}

func (c ginContext[B]) SerializeError(err error) {
c.ginCtx.JSON(http.StatusInternalServerError, err)
}

func (c ginContext[B]) SetDefaultStatusCode() {
if c.DefaultStatusCode == 0 {
c.DefaultStatusCode = http.StatusOK
}
c.SetStatus(c.DefaultStatusCode)
}

0 comments on commit 2140538

Please sign in to comment.