mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 17:01:11 +08:00
Compare commits
2 commits
2454a18422
...
17bab2dcfd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17bab2dcfd | ||
|
|
edca87906d |
4 changed files with 12 additions and 27 deletions
18
context.go
18
context.go
|
|
@ -267,15 +267,11 @@ func (c *Context) String(code int, format string, values ...interface{}) {
|
||||||
func (c *Context) JSON(code int, obj interface{}) {
|
func (c *Context) JSON(code int, obj interface{}) {
|
||||||
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
// JSON 编码
|
if err := json.MarshalWrite(c.Writer, obj); err != nil {
|
||||||
jsonBytes, err := json.Marshal(obj)
|
|
||||||
if err != nil {
|
|
||||||
c.AddError(fmt.Errorf("failed to marshal JSON: %w", err))
|
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))
|
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Writer.Write(jsonBytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GOB 向响应写入GOB数据
|
// GOB 向响应写入GOB数据
|
||||||
|
|
@ -287,7 +283,6 @@ func (c *Context) GOB(code int, obj interface{}) {
|
||||||
encoder := gob.NewEncoder(c.Writer)
|
encoder := gob.NewEncoder(c.Writer)
|
||||||
if err := encoder.Encode(obj); err != nil {
|
if err := encoder.Encode(obj); err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to encode GOB: %w", err))
|
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))
|
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to encode GOB: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -307,7 +302,6 @@ func (c *Context) HTML(code int, name string, obj interface{}) {
|
||||||
err := tpl.ExecuteTemplate(c.Writer, name, obj)
|
err := tpl.ExecuteTemplate(c.Writer, name, obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to render HTML template '%s': %w", name, err))
|
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))
|
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to render HTML template '%s': %w", name, err))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -333,12 +327,6 @@ func (c *Context) ShouldBindJSON(obj interface{}) error {
|
||||||
if c.Request.Body == nil {
|
if c.Request.Body == nil {
|
||||||
return errors.New("request body is empty")
|
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)
|
err := json.UnmarshalRead(c.Request.Body, obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("json binding error: %w", err)
|
return fmt.Errorf("json binding error: %w", err)
|
||||||
|
|
@ -447,7 +435,7 @@ func (c *Context) GetReqBodyFull() ([]byte, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
defer c.Request.Body.Close() // 确保请求体被关闭
|
defer c.Request.Body.Close() // 确保请求体被关闭
|
||||||
data, err := io.ReadAll(c.Request.Body)
|
data, err := copyb.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
||||||
return nil, fmt.Errorf("failed to read request body: %w", err)
|
return nil, fmt.Errorf("failed to read request body: %w", err)
|
||||||
|
|
@ -461,7 +449,7 @@ func (c *Context) GetReqBodyBuffer() (*bytes.Buffer, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
defer c.Request.Body.Close() // 确保请求体被关闭
|
defer c.Request.Body.Close() // 确保请求体被关闭
|
||||||
data, err := io.ReadAll(c.Request.Body)
|
data, err := copyb.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
c.AddError(fmt.Errorf("failed to read request body: %w", err))
|
||||||
return nil, fmt.Errorf("failed to read request body: %w", err)
|
return nil, fmt.Errorf("failed to read request body: %w", err)
|
||||||
|
|
|
||||||
|
|
@ -554,7 +554,6 @@ func NotFound() HandlerFunc {
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
engine := c.engine
|
engine := c.engine
|
||||||
engine.errorHandle.handler(c, http.StatusNotFound, errors.New("not found"))
|
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
|
go 1.24.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4
|
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.7.1
|
github.com/WJQSERVER-STUDIO/httpc v0.8.0
|
||||||
github.com/fenthope/reco v0.0.3
|
github.com/fenthope/reco v0.0.3
|
||||||
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8
|
github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/valyala/bytebufferpool v1.0.0 // indirect
|
require github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
|
|
||||||
14
go.sum
14
go.sum
|
|
@ -1,12 +1,10 @@
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4 h1:JLtFd00AdFg/TP+dtvIzLkdHwKUGPOAijN1sMtEYoFg=
|
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6 h1:/50VJYXd6jcu+p5BnEBDyiX0nAyGxas1W3DCnrYMxMY=
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.4/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc=
|
github.com/WJQSERVER-STUDIO/go-utils/copyb v0.0.6/go.mod h1:FZ6XE+4TKy4MOfX1xWKe6Rwsg0ucYFCdNh1KLvyKTfc=
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.7.0 h1:iHhqlxppJBjlmvsIjvLZKRbWXqSdbeSGGofjHGmqGJc=
|
github.com/WJQSERVER-STUDIO/httpc v0.8.0 h1:G7inJ5EEsg5+BkeFiNIo/6+Mj7Ygiq85yMT3Ld7frJY=
|
||||||
github.com/WJQSERVER-STUDIO/httpc v0.7.0/go.mod h1:M7KNUZjjhCkzzcg9lBPs9YfkImI+7vqjAyjdA19+joE=
|
github.com/WJQSERVER-STUDIO/httpc v0.8.0/go.mod h1:50297rvgppmgPbZEtWzTWgkomoqPREkGy9T3Y/NqN7o=
|
||||||
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 h1:RmnQ0D9a8PWtwOODawitTe4BztTnS9wYwrDbipISNq4=
|
||||||
github.com/fenthope/reco v0.0.3/go.mod h1:mDkGLHte5udWTIcjQTxrABRcf56SSdxBOCLgrRDwI/Y=
|
github.com/fenthope/reco v0.0.3/go.mod h1:mDkGLHte5udWTIcjQTxrABRcf56SSdxBOCLgrRDwI/Y=
|
||||||
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8 h1:o8UqXPI6SVwQt04RGsqKp3qqmbOfTNMqDrWsc4O47kk=
|
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-20250517221953-25912455fbc8/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
github.com/go-json-experiment/json v0.0.0-20250626171732-1a886bd29d1b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
||||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue