Added Reader and String
This commit is contained in:
16
Context.go
16
Context.go
@ -1,7 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// maxParams defines the maximum number of parameters per route.
|
||||
@ -11,6 +13,8 @@ const maxParams = 16
|
||||
type Context interface {
|
||||
Bytes([]byte) error
|
||||
Error(int, error) error
|
||||
Reader(io.Reader) error
|
||||
String(string) error
|
||||
}
|
||||
|
||||
// context represents a request & response context.
|
||||
@ -43,6 +47,18 @@ func (ctx *context) Error(status int, err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Reader sends the contents of the io.Reader without creating an in-memory copy.
|
||||
func (ctx *context) Reader(reader io.Reader) error {
|
||||
_, err := io.Copy(ctx.response, reader)
|
||||
return err
|
||||
}
|
||||
|
||||
// String responds with the given string.
|
||||
func (ctx *context) String(body string) error {
|
||||
slice := unsafe.Slice(unsafe.StringData(body), len(body))
|
||||
return ctx.Bytes(slice)
|
||||
}
|
||||
|
||||
// addParameter adds a new parameter to the context.
|
||||
func (ctx *context) addParameter(name string, value string) {
|
||||
ctx.paramNames[ctx.paramCount] = name
|
||||
|
Reference in New Issue
Block a user