2024-03-22 14:08:24 +00:00
|
|
|
package web
|
2023-07-18 16:02:57 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-21 21:23:49 +00:00
|
|
|
"errors"
|
2023-07-18 16:02:57 +00:00
|
|
|
)
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// Context is the interface for a request and its response.
|
2023-07-18 16:02:57 +00:00
|
|
|
type Context interface {
|
|
|
|
Bytes([]byte) error
|
2024-03-16 14:30:05 +00:00
|
|
|
Error(...any) error
|
2024-03-13 19:18:01 +00:00
|
|
|
Next() error
|
2024-03-16 14:22:47 +00:00
|
|
|
Request() Request
|
|
|
|
Response() Response
|
2024-03-16 14:30:05 +00:00
|
|
|
Status(int) Context
|
2023-07-19 11:01:51 +00:00
|
|
|
String(string) error
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// context contains the request and response data.
|
|
|
|
type context struct {
|
|
|
|
request
|
|
|
|
response
|
2024-03-14 11:52:03 +00:00
|
|
|
server *server
|
2024-03-14 14:11:00 +00:00
|
|
|
handlerCount uint8
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// Bytes adds the raw byte slice to the response body.
|
|
|
|
func (ctx *context) Bytes(body []byte) error {
|
|
|
|
ctx.response.body = append(ctx.response.body, body...)
|
|
|
|
return nil
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// Error provides a convenient way to wrap multiple errors.
|
|
|
|
func (ctx *context) Error(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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Join(combined...)
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 19:18:01 +00:00
|
|
|
// Next executes the next handler in the middleware chain.
|
2024-03-26 21:46:16 +00:00
|
|
|
func (ctx *context) Next() error {
|
2024-03-13 19:18:01 +00:00
|
|
|
ctx.handlerCount++
|
|
|
|
return ctx.server.handlers[ctx.handlerCount](ctx)
|
|
|
|
}
|
|
|
|
|
2024-03-16 14:22:47 +00:00
|
|
|
// Request returns the HTTP request.
|
2024-03-26 21:46:16 +00:00
|
|
|
func (ctx *context) Request() Request {
|
2024-03-16 14:22:47 +00:00
|
|
|
return &ctx.request
|
2024-03-13 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 14:22:47 +00:00
|
|
|
// Response returns the HTTP response.
|
2024-03-26 21:46:16 +00:00
|
|
|
func (ctx *context) Response() Response {
|
2024-03-16 14:22:47 +00:00
|
|
|
return &ctx.response
|
2024-03-14 11:52:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// Status sets the HTTP status of the response
|
|
|
|
// and returns the context for method chaining.
|
|
|
|
func (ctx *context) Status(status int) Context {
|
|
|
|
ctx.response.SetStatus(status)
|
2024-03-13 15:57:36 +00:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2024-03-26 21:46:16 +00:00
|
|
|
// String adds the given string to the response body.
|
|
|
|
func (ctx *context) String(body string) error {
|
|
|
|
ctx.response.body = append(ctx.response.body, body...)
|
|
|
|
return nil
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|