Added Status method
This commit is contained in:
parent
c3e549ecfd
commit
02328cb41e
12
Context.go
12
Context.go
@ -13,11 +13,12 @@ const maxParams = 16
|
|||||||
// Context represents the interface for a request & response context.
|
// Context represents the interface for a request & response context.
|
||||||
type Context interface {
|
type Context interface {
|
||||||
Bytes([]byte) error
|
Bytes([]byte) error
|
||||||
Error(status int, messages ...any) error
|
Error(messages ...any) error
|
||||||
Get(param string) string
|
Get(param string) string
|
||||||
Reader(io.Reader) error
|
Reader(io.Reader) error
|
||||||
Request() Request
|
Request() Request
|
||||||
Response() Response
|
Response() Response
|
||||||
|
Status(status int) Context
|
||||||
String(string) error
|
String(string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ func (ctx *ctx) Bytes(body []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Error is used for sending error messages to the client.
|
// 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
|
var combined []error
|
||||||
|
|
||||||
for _, msg := range messages {
|
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...)
|
return errors.Join(combined...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,6 +89,12 @@ func (ctx *ctx) Response() Response {
|
|||||||
return &ctx.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.
|
// String responds with the given string.
|
||||||
func (ctx *ctx) String(body string) error {
|
func (ctx *ctx) String(body string) error {
|
||||||
slice := unsafe.Slice(unsafe.StringData(body), len(body))
|
slice := unsafe.Slice(unsafe.StringData(body), len(body))
|
||||||
|
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -70,7 +69,7 @@ func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the server on the given address.
|
// Run starts the server on the given address.
|
||||||
func (server *Server) Run(address string) {
|
func (server *Server) Run(address string) error {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: address,
|
Addr: address,
|
||||||
Handler: server,
|
Handler: server,
|
||||||
@ -83,8 +82,7 @@ func (server *Server) Run(address string) {
|
|||||||
listener, err := net.Listen("tcp", address)
|
listener, err := net.Listen("tcp", address)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go srv.Serve(listener)
|
go srv.Serve(listener)
|
||||||
@ -96,5 +94,5 @@ func (server *Server) Run(address string) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), server.Config.Timeout.Shutdown)
|
ctx, cancel := context.WithTimeout(context.Background(), server.Config.Timeout.Shutdown)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
srv.Shutdown(ctx)
|
return srv.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@ func TestRouter(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
s.Get("/error", func(ctx server.Context) error {
|
s.Get("/error", func(ctx server.Context) error {
|
||||||
return ctx.Error(http.StatusUnauthorized, "Not logged in")
|
return ctx.Status(http.StatusUnauthorized).Error("Not logged in")
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Get("/error2", func(ctx server.Context) error {
|
s.Get("/error2", func(ctx server.Context) error {
|
||||||
return ctx.Error(http.StatusUnauthorized, "Not logged in", errors.New("Missing auth token"))
|
return ctx.Status(http.StatusUnauthorized).Error("Not logged in", errors.New("Missing auth token"))
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Get("/reader", func(ctx server.Context) error {
|
s.Get("/reader", func(ctx server.Context) error {
|
||||||
|
2
go.mod
2
go.mod
@ -4,5 +4,5 @@ go 1.22
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
git.akyoto.dev/go/assert v0.1.3
|
git.akyoto.dev/go/assert v0.1.3
|
||||||
git.akyoto.dev/go/router v0.1.2
|
git.akyoto.dev/go/router v0.1.3
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -1,4 +1,4 @@
|
|||||||
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
|
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
|
||||||
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=
|
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=
|
||||||
git.akyoto.dev/go/router v0.1.2 h1:UZq92uOqQwxH+fS8geEhQ5RLZS7sb/QLvwrRzXjaTcg=
|
git.akyoto.dev/go/router v0.1.3 h1:H4wJCYdDD3/i9miYSK/e5sCoGiXe9OX7KmgH4/Toa60=
|
||||||
git.akyoto.dev/go/router v0.1.2/go.mod h1:VfSsK/Z6fUhT3pWaAAnuAcj++bWRZD+bzNaqJoTAunU=
|
git.akyoto.dev/go/router v0.1.3/go.mod h1:VfSsK/Z6fUhT3pWaAAnuAcj++bWRZD+bzNaqJoTAunU=
|
||||||
|
Loading…
Reference in New Issue
Block a user