mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
update deps & use copyb high perfromance pool & switch to stream json encoder
This commit is contained in:
parent
2454a18422
commit
edca87906d
3 changed files with 23 additions and 18 deletions
21
context.go
21
context.go
|
|
@ -267,15 +267,22 @@ func (c *Context) String(code int, format string, values ...interface{}) {
|
|||
func (c *Context) JSON(code int, obj interface{}) {
|
||||
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
c.Writer.WriteHeader(code)
|
||||
// JSON 编码
|
||||
jsonBytes, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
/*
|
||||
// JSON 编码
|
||||
jsonBytes, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
c.AddError(fmt.Errorf("failed to marshal JSON: %w", err))
|
||||
//c.String(http.StatusInternalServerError, "Internal Server Error: Failed to marshal JSON")
|
||||
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err))
|
||||
return
|
||||
}
|
||||
c.Writer.Write(jsonBytes)
|
||||
*/
|
||||
if err := json.MarshalWrite(c.Writer, obj); err != nil {
|
||||
c.AddError(fmt.Errorf("failed to marshal JSON: %w", err))
|
||||
//c.String(http.StatusInternalServerError, "Internal Server Error: Failed to marshal JSON")
|
||||
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err))
|
||||
return
|
||||
}
|
||||
c.Writer.Write(jsonBytes)
|
||||
}
|
||||
|
||||
// GOB 向响应写入GOB数据
|
||||
|
|
@ -447,7 +454,7 @@ func (c *Context) GetReqBodyFull() ([]byte, error) {
|
|||
return nil, nil
|
||||
}
|
||||
defer c.Request.Body.Close() // 确保请求体被关闭
|
||||
data, err := io.ReadAll(c.Request.Body)
|
||||
data, err := copyb.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
||||
return nil, fmt.Errorf("failed to read request body: %w", err)
|
||||
|
|
@ -461,7 +468,7 @@ func (c *Context) GetReqBodyBuffer() (*bytes.Buffer, error) {
|
|||
return nil, nil
|
||||
}
|
||||
defer c.Request.Body.Close() // 确保请求体被关闭
|
||||
data, err := io.ReadAll(c.Request.Body)
|
||||
data, err := copyb.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
||||
return nil, fmt.Errorf("failed to read request body: %w", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue