mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 17:01:11 +08:00
Compare commits
No commits in common. "17bab2dcfda9fc23f9ada34a0e01524b1f9e1a01" and "2454a184229c79222ce6d8db0ca42aa144501599" have entirely different histories.
17bab2dcfd
...
2454a18422
4 changed files with 27 additions and 12 deletions
18
context.go
18
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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
6
go.mod
6
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
|
||||
|
|
|
|||
14
go.sum
14
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=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue