Added Status method

This commit is contained in:
2024-03-13 16:57:36 +01:00
parent c3e549ecfd
commit 02328cb41e
5 changed files with 17 additions and 13 deletions

View File

@ -13,11 +13,12 @@ const maxParams = 16
// Context represents the interface for a request & response context.
type Context interface {
Bytes([]byte) error
Error(status int, messages ...any) error
Error(messages ...any) error
Get(param string) string
Reader(io.Reader) error
Request() Request
Response() Response
Status(status int) Context
String(string) error
}
@ -46,7 +47,7 @@ func (ctx *ctx) Bytes(body []byte) error {
}
// Error is used for sending error messages to the client.
func (ctx *ctx) Error(status int, messages ...any) error {
func (ctx *ctx) Error(messages ...any) error {
var combined []error
for _, msg := range messages {
@ -58,7 +59,6 @@ func (ctx *ctx) Error(status int, messages ...any) error {
}
}
ctx.response.WriteHeader(status)
return errors.Join(combined...)
}
@ -89,6 +89,12 @@ func (ctx *ctx) Response() Response {
return &ctx.response
}
// Status sets the HTTP status of the response.
func (ctx *ctx) Status(status int) Context {
ctx.response.WriteHeader(status)
return ctx
}
// String responds with the given string.
func (ctx *ctx) String(body string) error {
slice := unsafe.Slice(unsafe.StringData(body), len(body))