diff --git a/context.go b/context.go index 23fa92f..c9586dd 100644 --- a/context.go +++ b/context.go @@ -267,11 +267,15 @@ 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) - if err := json.MarshalWrite(c.Writer, obj); 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) } // GOB 向响应写入GOB数据 @@ -283,6 +287,7 @@ func (c *Context) GOB(code int, obj interface{}) { encoder := gob.NewEncoder(c.Writer) if err := encoder.Encode(obj); err != nil { c.AddError(fmt.Errorf("failed to encode GOB: %w", err)) + //c.String(http.StatusInternalServerError, "Internal Server Error: Failed to encode GOB") c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to encode GOB: %w", err)) return } @@ -302,6 +307,7 @@ func (c *Context) HTML(code int, name string, obj interface{}) { err := tpl.ExecuteTemplate(c.Writer, name, obj) if err != nil { c.AddError(fmt.Errorf("failed to render HTML template '%s': %w", name, err)) + //c.String(http.StatusInternalServerError, "Internal Server Error: Failed to render HTML template") c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to render HTML template '%s': %w", name, err)) } return @@ -327,6 +333,12 @@ func (c *Context) ShouldBindJSON(obj interface{}) error { if c.Request.Body == nil { return errors.New("request body is empty") } + /* + decoder := json.NewDecoder(c.Request.Body) + if err := decoder.Decode(obj); err != nil { + return fmt.Errorf("json binding error: %w", err) + } + */ err := json.UnmarshalRead(c.Request.Body, obj) if err != nil { return fmt.Errorf("json binding error: %w", err) @@ -435,7 +447,7 @@ func (c *Context) GetReqBodyFull() ([]byte, error) { return nil, nil } defer c.Request.Body.Close() // 确保请求体被关闭 - data, err := copyb.ReadAll(c.Request.Body) + data, err := io.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) @@ -449,7 +461,7 @@ func (c *Context) GetReqBodyBuffer() (*bytes.Buffer, error) { return nil, nil } defer c.Request.Body.Close() // 确保请求体被关闭 - data, err := copyb.ReadAll(c.Request.Body) + data, err := io.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) diff --git a/engine.go b/engine.go index 3d39cf0..babe365 100644 --- a/engine.go +++ b/engine.go @@ -554,6 +554,7 @@ func NotFound() HandlerFunc { return func(c *Context) { engine := c.engine engine.errorHandle.handler(c, http.StatusNotFound, errors.New("not found")) + return } } diff --git a/go.mod b/go.mod index 18194de..4fb176d 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module github.com/infinite-iroha/touka go 1.24.4 require ( - github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6 - github.com/WJQSERVER-STUDIO/httpc v0.8.0 + github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 + github.com/WJQSERVER-STUDIO/httpc v0.7.1 github.com/fenthope/reco v0.0.3 - github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b + github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 ) require github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index f90f774..6e1be02 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,12 @@ -github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6 h1:/50VJYXd6jcu+p5BnEBDyiX0nAyGxas1W3DCnrYMxMY= -github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc= -github.com/WJQSERVER-STUDIO/httpc v0.8.0 h1:G7inJ5EEsg5+BkeFiNIo/6+Mj7Ygiq85yMT3Ld7frJY= -github.com/WJQSERVER-STUDIO/httpc v0.8.0/go.mod h1:50297rvgppmgPbZEtWzTWgkomoqPREkGy9T3Y/NqN7o= +github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 h1:JLtFd00AdFg/TP+dtvIzLkdHwKUGPOAijN1sMtEYoFg= +github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc= +github.com/WJQSERVER-STUDIO/httpc v0.7.0 h1:iHhqlxppJBjlmvsIjvLZKRbWXqSdbeSGGofjHGmqGJc= +github.com/WJQSERVER-STUDIO/httpc v0.7.0/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE= +github.com/WJQSERVER-STUDIO/httpc v0.7.1 h1:D3NlfY52pwKIOSzkdRrLinUynyKELrcPZEO8QjlBq2M= +github.com/WJQSERVER-STUDIO/httpc v0.7.1/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE= github.com/fenthope/reco v0.0.3 h1:RmnQ0D9a8PWtwOODawitTe4BztTnS9wYwrDbipISNq4= github.com/fenthope/reco v0.0.3/go.mod h1:mDkGLHte5udWTIcjQTxrABRcf56SSdxBOCLgrRDwI/Y= -github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b h1:ooF9/NzXkXL3OOLRwtPuQT/D7Kx2S5w/Kl1GnMF9h2s= -github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= +github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 h1:o8UqXPI6SVwQt04RGsqKp3qqmbOfTNMqDrWsc4O47kk= +github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=