working commit

This commit is contained in:
2026-02-15 17:47:45 +02:00
parent 15a3379e5c
commit 2373af081e
10 changed files with 159 additions and 49 deletions
+12 -1
View File
@@ -15,6 +15,7 @@ import (
"encoding/json"
"io"
"net/http"
"strconv"
)
type Context struct {
@@ -123,18 +124,28 @@ func (rctx *Context) SetStatus(httpStatus int) {
}
func (rctx *Context) SendJSON(statusCode int, payload any) {
buffer := bytes.NewBuffer(nil)
json.NewEncoder(buffer).Encode(payload)
rctx.Writer.Header().Set("Content-Type", "application/json")
size := strconv.FormatInt(int64(len(buffer.Bytes())), 10)
rctx.Writer.Header().Set("Content-Length", size)
rctx.Writer.WriteHeader(statusCode)
json.NewEncoder(rctx.Writer).Encode(payload)
rctx.Writer.Write(buffer.Bytes())
}
func (rctx *Context) SendText(statusCode int, payload string) {
size := strconv.FormatInt(int64(len(payload)), 10)
rctx.Writer.Header().Set("Content-Type", "text/plain")
rctx.Writer.Header().Set("Content-Length", size)
rctx.Writer.WriteHeader(statusCode)
rctx.Writer.Write([]byte(payload))
}
func (rctx *Context) SendBytes(statusCode int, payload []byte) {
size := strconv.FormatInt(int64(len(payload)), 10)
rctx.Writer.Header().Set("Content-Type", "application/octet-stream")
rctx.Writer.Header().Set("Content-Length", size)
rctx.Writer.WriteHeader(statusCode)
rctx.Writer.Write(payload)
}