68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"unsafe"
|
|
)
|
|
|
|
// maxParams defines the maximum number of parameters per route.
|
|
const maxParams = 16
|
|
|
|
// Context represents the interface for a request & response context.
|
|
type Context interface {
|
|
Bytes([]byte) error
|
|
Error(int, error) error
|
|
Reader(io.Reader) error
|
|
String(string) error
|
|
}
|
|
|
|
// context represents a request & response context.
|
|
type context struct {
|
|
request *http.Request
|
|
response http.ResponseWriter
|
|
paramNames [maxParams]string
|
|
paramValues [maxParams]string
|
|
paramCount int
|
|
}
|
|
|
|
// newContext returns a new context from the pool.
|
|
func newContext(request *http.Request, response http.ResponseWriter) *context {
|
|
ctx := contextPool.Get().(*context)
|
|
ctx.request = request
|
|
ctx.response = response
|
|
ctx.paramCount = 0
|
|
return ctx
|
|
}
|
|
|
|
// Bytes responds with a raw byte slice.
|
|
func (ctx *context) Bytes(body []byte) error {
|
|
_, err := ctx.response.Write(body)
|
|
return err
|
|
}
|
|
|
|
// Error is used for sending error messages to the client.
|
|
func (ctx *context) Error(status int, err error) error {
|
|
ctx.response.WriteHeader(status)
|
|
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
|
|
ctx.paramValues[ctx.paramCount] = value
|
|
ctx.paramCount++
|
|
}
|