104 lines
2.3 KiB
Go
Raw Normal View History

2023-07-18 19:48:38 +00:00
package server
2023-07-18 16:02:57 +00:00
import (
2023-07-21 21:23:49 +00:00
"errors"
2023-07-19 11:01:51 +00:00
"io"
2023-07-18 16:02:57 +00:00
"net/http"
2023-07-19 11:01:51 +00:00
"unsafe"
2023-07-18 16:02:57 +00:00
)
// 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
2023-07-21 21:23:49 +00:00
Error(status int, messages ...any) error
2023-07-22 09:48:35 +00:00
Get(param string) string
2023-07-19 11:01:51 +00:00
Reader(io.Reader) error
2023-07-22 10:32:52 +00:00
Request() Request
Response() Response
2023-07-19 11:01:51 +00:00
String(string) error
2023-07-18 16:02:57 +00:00
}
2024-03-12 21:31:45 +00:00
// ctx represents a request & response context.
type ctx struct {
2023-07-22 10:32:52 +00:00
request request
response response
2023-07-18 16:02:57 +00:00
paramNames [maxParams]string
paramValues [maxParams]string
paramCount int
}
// newContext returns a new context from the pool.
2024-03-12 21:31:45 +00:00
func newContext(req *http.Request, res http.ResponseWriter) *ctx {
ctx := contextPool.Get().(*ctx)
2023-07-22 10:32:52 +00:00
ctx.request.Request = req
ctx.response.ResponseWriter = res
2023-07-18 16:02:57 +00:00
ctx.paramCount = 0
return ctx
}
// Bytes responds with a raw byte slice.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Bytes(body []byte) error {
2023-07-18 16:02:57 +00:00
_, err := ctx.response.Write(body)
return err
}
// Error is used for sending error messages to the client.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Error(status int, messages ...any) error {
2023-07-21 21:23:49 +00:00
var combined []error
for _, msg := range messages {
switch err := msg.(type) {
case error:
combined = append(combined, err)
case string:
combined = append(combined, errors.New(err))
}
}
2023-07-22 12:14:50 +00:00
ctx.response.WriteHeader(status)
2023-07-21 21:23:49 +00:00
return errors.Join(combined...)
2023-07-18 16:02:57 +00:00
}
2023-07-22 09:48:35 +00:00
// Get retrieves a parameter.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Get(param string) string {
2023-07-22 09:48:35 +00:00
for i := 0; i < ctx.paramCount; i++ {
if ctx.paramNames[i] == param {
return ctx.paramValues[i]
}
}
return ""
}
2023-07-19 11:01:51 +00:00
// Reader sends the contents of the io.Reader without creating an in-memory copy.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Reader(reader io.Reader) error {
2023-07-19 11:01:51 +00:00
_, err := io.Copy(ctx.response, reader)
return err
}
2023-07-22 10:32:52 +00:00
// Request returns the HTTP request.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Request() Request {
2023-07-22 10:32:52 +00:00
return &ctx.request
}
// Response returns the HTTP response.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) Response() Response {
2023-07-22 10:32:52 +00:00
return &ctx.response
}
2023-07-19 11:01:51 +00:00
// String responds with the given string.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) String(body string) error {
2023-07-19 11:01:51 +00:00
slice := unsafe.Slice(unsafe.StringData(body), len(body))
return ctx.Bytes(slice)
}
2023-07-18 16:02:57 +00:00
// addParameter adds a new parameter to the context.
2024-03-12 21:31:45 +00:00
func (ctx *ctx) addParameter(name string, value string) {
2023-07-18 16:02:57 +00:00
ctx.paramNames[ctx.paramCount] = name
ctx.paramValues[ctx.paramCount] = value
ctx.paramCount++
}