working commit

This commit is contained in:
2026-02-06 15:17:16 +02:00
parent 97c58bb283
commit d7e57c5f42
4 changed files with 461 additions and 0 deletions
+33
View File
@@ -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)