From ef122ccaea5a26dad68f1db587756b48bdab37b1 Mon Sep 17 00:00:00 2001 From: some Date: Thu, 10 Oct 2024 17:06:32 -0400 Subject: [PATCH] Change res to input parameter, update server --- server.go | 97 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/server.go b/server.go index 7e722c5..f910647 100644 --- a/server.go +++ b/server.go @@ -8,20 +8,6 @@ import ( "somehole.com/common/log" ) -type GenericResponse struct { - Status int - Header Header - Body Body -} - -type Response[RSB ResponseBuilder] struct { - GenericResponse -} - -func NewResponse[RSB ResponseBuilder](rsb RSB) *Response[RSB] { - return &Response[RSB]{*rsb.Response()} -} - type Error interface { Error(e Error) (err Error) Status() (code int) @@ -38,10 +24,14 @@ type GenericRequest struct { type Request[RQB RequestBuilder] struct { GenericRequest + requestBuilder RQB } func NewRequest[RQB RequestBuilder](rqb RQB) *Request[RQB] { - return &Request[RQB]{*rqb.Request()} + return &Request[RQB]{ + GenericRequest: *rqb.Request(), + requestBuilder: rqb, + } } func (*Request[RQB]) RequestBuilder() RequestBuilder { @@ -60,9 +50,31 @@ type RequestBuilder interface { Request() *GenericRequest } -func (*Response[RSB]) ResponseBuilder() ResponseBuilder { - var rsb RSB - return rsb.New() +func NewRequestBuilder[RQB RequestBuilder]() RQB { + var rqb RQB + return rqb.New().(RQB) +} + +type GenericResponse struct { + Status int + Header Header + Body Body +} + +type Response[RSB ResponseBuilder] struct { + GenericResponse + responseBuilder RSB +} + +func NewResponse[RSB ResponseBuilder](rsb RSB) *Response[RSB] { + return &Response[RSB]{ + GenericResponse: *rsb.Response(), + responseBuilder: rsb, + } +} + +func (res *Response[RSB]) ResponseBuilder() RSB { + return res.responseBuilder } type ResponseBuilder interface { @@ -73,6 +85,11 @@ type ResponseBuilder interface { Response() *GenericResponse } +func NewResponseBuilder[RSB ResponseBuilder]() RSB { + var rsb RSB + return rsb.New().(RSB) +} + type writer struct { http.ResponseWriter log.Logger @@ -103,7 +120,7 @@ const ( numServeStages ) -type ServeFunc[RQB RequestBuilder, RSB ResponseBuilder] func(req *Request[RQB]) (res *Response[RSB], err Error) +type ServeFunc[RQB RequestBuilder, RSB ResponseBuilder] func(req *Request[RQB], res *Response[RSB]) (err Error) type server[RQB RequestBuilder, RSB ResponseBuilder] struct { logger log.Logger @@ -123,29 +140,28 @@ func (srv *server[RQB, RSB]) SetLogger(logger log.Logger) *server[RQB, RSB] { return srv } -func (srv *server[RQB, RSB]) addServeFunc(when serveStage, serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { - if srv.serve[when] == nil { - srv.serve[when] = make([]ServeFunc[RQB, RSB], 0) +func (srv *server[RQB, RSB]) addServeFunc(stage serveStage, serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { + if srv.serve[stage] == nil { + srv.serve[stage] = make([]ServeFunc[RQB, RSB], 0) } - srv.serve[when] = append(srv.serve[when], serve) + srv.serve[stage] = append(srv.serve[stage], serve) return srv } -func (srv *server[RQB, RSB]) PreServeFunc(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { +func (srv *server[RQB, RSB]) PreServe(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { return srv.addServeFunc(servePre, serve) } -func (srv *server[RQB, RSB]) AddServeFunc(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { +func (srv *server[RQB, RSB]) AddServe(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { return srv.addServeFunc(serveMain, serve) } -func (srv *server[RQB, RSB]) PostServeFunc(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { +func (srv *server[RQB, RSB]) PostServe(serve ServeFunc[RQB, RSB]) *server[RQB, RSB] { return srv.addServeFunc(servePost, serve) } func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) { - var rqb RQB - rqb = rqb.New().(RQB) + rqb := NewRequestBuilder[RQB]() wr := writer{ResponseWriter: w, Logger: srv.logger} if ok := wr.handleError(rqb.SetAllowed(r.Method)); !ok { return @@ -163,22 +179,25 @@ func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) { if ok := wr.handleError(rqb.SetValues(r.Form)); !ok { return } + req := NewRequest(rqb) + res := NewResponse(NewResponseBuilder[RSB]()) for _, stage := range srv.serve { - for _, s := range stage { - res, err := s(NewRequest(rqb)) + for _, serve := range stage { + err := serve(req, res) if ok := wr.handleError(err); !ok { return } - if res.Header != nil { - for key, value := range res.Header { - for _, v := range value { - wr.Header().Add(key, v) - } - } - } - if len(res.Body) > 0 { - wr.Write(res.Body) + } + } + if res.Header != nil { + for key, value := range res.Header { + for _, v := range value { + wr.Header().Add(key, v) } } } + wr.WriteHeader(res.Status) + if len(res.Body) > 0 { + wr.Write(res.Body) + } }