Skip to content

Commit

Permalink
fix/unmarshal apiv1 response (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 authored Jul 17, 2024
2 parents 957fe6e + 777f37c commit 46e4780
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 141 deletions.
35 changes: 35 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/anilibria/alice/internal/cache"
"github.com/anilibria/alice/internal/utils"
"github.com/gofiber/fiber/v2"
futils "github.com/gofiber/fiber/v2/utils"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -52,6 +53,14 @@ func (m *Proxy) ProxyFiberRequest(c *fiber.Ctx) (e error) {
return
}

var ok bool
if ok, e = m.unmarshalApiResponse(c, rsp); e != nil {
rlog(c).Warn().Msg(e.Error())
m.bypassCache(c)
} else if !ok {
m.bypassCache(c)
}

if !m.IsCacheBypass(c) {
return m.cacheAndRespond(c, rsp)
}
Expand Down Expand Up @@ -104,6 +113,32 @@ func (m *Proxy) doRequest(c *fiber.Ctx, req *fasthttp.Request, rsp *fasthttp.Res
return
}

func (*Proxy) unmarshalApiResponse(c *fiber.Ctx, rsp *fasthttp.Response) (ok bool, e error) {
var apirsp *utils.ApiResponse
if apirsp, e = utils.UnmarshalApiResponse(rsp.Body()); e != nil || apirsp == nil {
return
}

if apirsp.Status && apirsp.Error == nil {
ok = true
return
}

if apirsp.Error == nil {
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
rlog(c).Debug().Msg(futils.UnsafeString(rsp.Body()))
rlog(c).Debug().Msgf("%+v", apirsp)
rlog(c).Debug().Msgf("%+v", apirsp.Error)
}

rlog(c).Error().Msg("smth is wrong in dst response - status false and err == nil")
return
}

rlog(c).Warn().Msgf("api server respond with %d - %s", apirsp.Error.Code, apirsp.Error.Message)
return
}

func (*Proxy) bypassCache(c *fiber.Ctx) {
key := c.Context().UserValue(utils.UVCacheKey).(*Key)
key.Reset()
Expand Down
68 changes: 0 additions & 68 deletions internal/service/error.go

This file was deleted.

22 changes: 21 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewService(c *cli.Context, l *zerolog.Logger, s io.Writer) *Service {
fiber.StatusOK,
errdesc,
func(status int, msg, desc string) {
if e := respondWithError(status, msg, desc, c); e != nil {
if e := utils.RespondWithApiError(status, msg, desc, c); e != nil {
rlog(c).Error().Msg("could not respond with JSON error - " + e.Error())
}
}
Expand Down Expand Up @@ -241,3 +241,23 @@ LOOP:
gLog.Error().Err(e).Msg("fiber Shutdown() error")
}
}

// TODO 2delete
// I think this block of code is not profitable
// so may be it must be reverted

var ferrPool = sync.Pool{
New: func() interface{} {
return new(fiber.Error)
},
}

func AcquireFErr() *fiber.Error {
return ferrPool.Get().(*fiber.Error)
}

func ReleaseFErr(e *fiber.Error) {
// ? is it required
e.Code, e.Message = 0, ""
ferrPool.Put(e)
}
47 changes: 47 additions & 0 deletions internal/utils/apiv1_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package utils

import (
"io"

"github.com/mailru/easyjson"
)

type (
ApiResponse struct {
Status bool
Data interface{} `json:"-"`
Error *ApiError
}
ApiError struct {
Code int
Message string
Description string
}
)

func newApiResponse(status int, msg, desc string) *ApiResponse {
return &ApiResponse{
Error: &ApiError{
Code: status,
Message: msg,
Description: desc,
},
}
}

func RespondWithApiError(status int, msg, desc string, w io.Writer) (e error) {
apirsp := newApiResponse(status, msg, desc)

var buf []byte
if buf, e = easyjson.Marshal(apirsp); e != nil {
return
}

_, e = w.Write(buf)
return
}

func UnmarshalApiResponse(payload []byte) (_ *ApiResponse, e error) {
apirsp := new(ApiResponse)
return apirsp, easyjson.Unmarshal(payload, apirsp)
}
Loading

0 comments on commit 46e4780

Please sign in to comment.