From 069bbc84dd914e25c3ed4ffd899275c1a06de928 Mon Sep 17 00:00:00 2001 From: some Date: Tue, 8 Oct 2024 18:18:16 -0400 Subject: [PATCH] Cleanup, label parameters, change method names --- prototype.go | 34 +++++++++++++++++----------------- server.go | 10 +++++----- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/prototype.go b/prototype.go index abb89a2..e9a27d9 100644 --- a/prototype.go +++ b/prototype.go @@ -3,39 +3,39 @@ package router import "io" type RequestBuilder interface { - Allowed(method string) ErrorResponse - Header(Header) ErrorResponse - Body(body io.ReadCloser) ErrorResponse - Values(values Values) ErrorResponse + Allowed(method string) (errRes ErrorResponse) + Header(header Header) (errRes ErrorResponse) + Body(body io.ReadCloser) (errRes ErrorResponse) + Values(values Values) (errRes ErrorResponse) } type PrototypeRequestBuilder interface { - RequestBuilder() RequestBuilder + RequestBuilder() (req RequestBuilder) } type Response interface { - Header() Header - Response() (body []byte) + Header() (header Header) + Bytes() (body []byte) } type PrototypeResponse interface { - Response() Response + Response() (res Response) } type ErrorResponse interface { - Ok() bool - HttpStatus() int - ErrorResponse() []byte - String() string - Error() string + Ok() (ok bool) + HttpStatus() (code int) + Bytes() (body []byte) + String() (out string) + Error() (out string) } type PrototypeErrorResponse interface { - ErrorResponse() ErrorResponse + ErrorResponse() (errRes ErrorResponse) } type Prototype struct { - PrototypeRequestBuilder - PrototypeResponse - PrototypeErrorResponse + req PrototypeRequestBuilder + res PrototypeResponse + errRes PrototypeErrorResponse } diff --git a/server.go b/server.go index 2892c61..c891461 100644 --- a/server.go +++ b/server.go @@ -56,7 +56,7 @@ func (srv *server) PostServeFunc(serve ServeFunc) *server { func (srv *server) handleError(errRes ErrorResponse, w http.ResponseWriter) (ok bool) { if !errRes.Ok() { w.WriteHeader(errRes.HttpStatus()) - w.Write(errRes.ErrorResponse()) + w.Write(errRes.Bytes()) srv.logger.Logf(log.LevelError, errRes.String()) return false } @@ -64,8 +64,8 @@ func (srv *server) handleError(errRes ErrorResponse, w http.ResponseWriter) (ok } func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { - pro := srv.prototype - req := pro.RequestBuilder() + p := srv.prototype + req := p.req.RequestBuilder() if ok := srv.handleError(req.Allowed(r.Method), w); !ok { return } @@ -79,7 +79,7 @@ func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if ok := srv.handleError(req.Values(Values(r.Form)), w); !ok { return } - res := pro.Response() + res := p.res.Response() for _, stage := range srv.serve { for _, s := range stage { if ok := srv.handleError(s(req, res), w); !ok { @@ -95,5 +95,5 @@ func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } - w.Write(res.Response()) + w.Write(res.Bytes()) }