diff --git a/Context.go b/Context.go index 89ded47..22a69b6 100644 --- a/Context.go +++ b/Context.go @@ -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)) diff --git a/Server.go b/Server.go index f23d6b6..d4ffd8e 100644 --- a/Server.go +++ b/Server.go @@ -2,7 +2,6 @@ package server import ( "context" - "fmt" "io" "log" "net" @@ -70,7 +69,7 @@ func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Requ } // Run starts the server on the given address. -func (server *Server) Run(address string) { +func (server *Server) Run(address string) error { srv := &http.Server{ Addr: address, Handler: server, @@ -83,8 +82,7 @@ func (server *Server) Run(address string) { listener, err := net.Listen("tcp", address) if err != nil { - fmt.Println(err) - return + return err } go srv.Serve(listener) @@ -96,5 +94,5 @@ func (server *Server) Run(address string) { ctx, cancel := context.WithTimeout(context.Background(), server.Config.Timeout.Shutdown) defer cancel() - srv.Shutdown(ctx) + return srv.Shutdown(ctx) } diff --git a/Server_test.go b/Server_test.go index 4977f1f..e88b791 100644 --- a/Server_test.go +++ b/Server_test.go @@ -27,11 +27,11 @@ func TestRouter(t *testing.T) { }) 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 { - 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 { diff --git a/go.mod b/go.mod index 402d15c..c14016a 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.22 require ( git.akyoto.dev/go/assert v0.1.3 - git.akyoto.dev/go/router v0.1.2 + git.akyoto.dev/go/router v0.1.3 ) diff --git a/go.sum b/go.sum index a127934..3a16cd6 100644 --- a/go.sum +++ b/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/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM= -git.akyoto.dev/go/router v0.1.2 h1:UZq92uOqQwxH+fS8geEhQ5RLZS7sb/QLvwrRzXjaTcg= -git.akyoto.dev/go/router v0.1.2/go.mod h1:VfSsK/Z6fUhT3pWaAAnuAcj++bWRZD+bzNaqJoTAunU= +git.akyoto.dev/go/router v0.1.3 h1:H4wJCYdDD3/i9miYSK/e5sCoGiXe9OX7KmgH4/Toa60= +git.akyoto.dev/go/router v0.1.3/go.mod h1:VfSsK/Z6fUhT3pWaAAnuAcj++bWRZD+bzNaqJoTAunU=