Skip to content

Commit dca5855

Browse files
committed
handle params values, convert string arg to bytes for handle multi strings
1 parent 02d8650 commit dca5855

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

context.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package routing
22

3-
import "github.com/valyala/fasthttp"
3+
import (
4+
"bytes"
5+
"github.com/valyala/fasthttp"
6+
)
47

58
type handlerFunc func(ctx *Context) error
69

@@ -13,10 +16,10 @@ type Context struct {
1316
}
1417

1518
// NewContext returns a new Context.
16-
func NewContext(ctx *fasthttp.RequestCtx) *Context {
19+
func NewContext(ctx *fasthttp.RequestCtx, params map[string]string) *Context {
1720
return &Context{
1821
ctx: ctx,
19-
params: make(map[string]string),
22+
params: params,
2023
paramValues: make([]string, 0, 10),
2124
handlers: nil,
2225
handlerIdx: -1,
@@ -28,15 +31,26 @@ func (c *Context) Context() *fasthttp.RequestCtx {
2831
return c.ctx
2932
}
3033

34+
// WithParams sets the params for the context.
35+
func (c *Context) WithParams(params map[string]string) *Context {
36+
c.params = params
37+
return c
38+
}
39+
3140
// Param returns the param value for the given key.
3241
func (c *Context) Param(key string) string {
3342
return c.params[key]
3443
}
3544

3645
// String sets the response body to the given string.
37-
func (c *Context) String(value string) error {
38-
c.ctx.SetBodyString(value)
39-
return nil
46+
func (c *Context) String(value string) {
47+
if c.ctx.Response.Body() == nil {
48+
c.ctx.Response.SetBodyString(value)
49+
} else {
50+
buf := bytes.NewBuffer(c.ctx.Response.Body())
51+
buf.WriteString(value)
52+
c.ctx.Response.SetBody(buf.Bytes())
53+
}
4054
}
4155

4256
// SetData sets the http header value to the given key.

0 commit comments

Comments
 (0)