working commit
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -47,6 +49,37 @@ func (rctx *Context) GetContext() context.Context {
|
||||
return rctx.Request.Context()
|
||||
}
|
||||
|
||||
// Binding
|
||||
|
||||
const emptyJSON = "{}"
|
||||
|
||||
func (ctx *Context) BindJSON(obj any) error {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
_, err := io.Copy(buffer, ctx.Request.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reqBody := buffer.Bytes()
|
||||
if len(reqBody) == 0 {
|
||||
reqBody = []byte(emptyJSON)
|
||||
}
|
||||
err = json.Unmarshal(reqBody, obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (ctx *Context) BindQuery(obj any) error {
|
||||
qMap := make(map[string]string)
|
||||
for key, val := range ctx.Request.URL.Query() {
|
||||
if len(val) == 1 {
|
||||
qMap[key] = val[0]
|
||||
}
|
||||
}
|
||||
return bindObj(obj, qMap, "param")
|
||||
}
|
||||
|
||||
// Response
|
||||
func (rctx *Context) SetHeader(key, value string) {
|
||||
rctx.Writer.Header().Set(key, value)
|
||||
|
||||
Reference in New Issue
Block a user