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 5715e48 commit 4f55b62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 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
27 changes: 27 additions & 0 deletions extra/fuegogin/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ginContext[B any] struct {
}

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

func (c ginContext[B]) Body() (B, error) {
var body B
Expand Down Expand Up @@ -77,10 +78,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 4f55b62

Please sign in to comment.