Skip to content

Commit

Permalink
feat: add util to http handler, support write bytes with status code (#…
Browse files Browse the repository at this point in the history
…55)

* feat: add util to write bytes with status code

* chore: update docs
  • Loading branch information
medcl authored Jan 10, 2025
1 parent 08f968c commit 2632f53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,22 @@ func (handler Handler) WriteError(w http.ResponseWriter, errMessage string, stat
}

func (handler Handler) WriteJSON(w http.ResponseWriter, v interface{}, statusCode int) error {
if !handler.wroteHeader {
handler.WriteJSONHeader(w)
w.WriteHeader(statusCode)
}

b, err := handler.EncodeJSON(v)
if err != nil {
w.Write([]byte(err.Error()))
return err
}
_, err = w.Write(b)

return handler.WriteBytes(w,b,statusCode)
}

func (handler Handler) WriteBytes(w http.ResponseWriter, b []byte, statusCode int) error {
if !handler.wroteHeader {
handler.WriteJSONHeader(w)
w.WriteHeader(statusCode)
}

_, err := w.Write(b)
if err != nil {
w.Write([]byte(err.Error()))
return err
Expand Down
2 changes: 2 additions & 0 deletions docs/content.en/docs/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Information about release notes of INFINI Framework is provided here.
- Add `min_bucket_size` and `hits_total` to metric configurations (#29)
- Add proxy settings to `http_client` config section (#33)
- Add new condition to check item length (eg: array,string) (#38)
- Add util to http handler, support write bytes with status code (#55)


### Breaking changes
- Update WebSocket greeting message header to use `websocket-session-id`
Expand Down

0 comments on commit 2632f53

Please sign in to comment.