Pass in ResponseBuilder instead

This commit is contained in:
some 2024-10-10 17:16:09 -04:00
parent ef122ccaea
commit 00090ec2bd
Signed by: some
GPG Key ID: 65D0589220B9BFC8

View File

@ -120,7 +120,7 @@ const (
numServeStages 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], rsb RSB) (err Error)
type server[RQB RequestBuilder, RSB ResponseBuilder] struct { type server[RQB RequestBuilder, RSB ResponseBuilder] struct {
logger log.Logger logger log.Logger
@ -161,7 +161,10 @@ func (srv *server[RQB, RSB]) PostServe(serve ServeFunc[RQB, RSB]) *server[RQB, R
} }
func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rqb := NewRequestBuilder[RQB]() var (
rqb = NewRequestBuilder[RQB]()
rsb = NewResponseBuilder[RSB]()
)
wr := writer{ResponseWriter: w, Logger: srv.logger} wr := writer{ResponseWriter: w, Logger: srv.logger}
if ok := wr.handleError(rqb.SetAllowed(r.Method)); !ok { if ok := wr.handleError(rqb.SetAllowed(r.Method)); !ok {
return return
@ -180,15 +183,15 @@ func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
req := NewRequest(rqb) req := NewRequest(rqb)
res := NewResponse(NewResponseBuilder[RSB]())
for _, stage := range srv.serve { for _, stage := range srv.serve {
for _, serve := range stage { for _, serve := range stage {
err := serve(req, res) err := serve(req, rsb)
if ok := wr.handleError(err); !ok { if ok := wr.handleError(err); !ok {
return return
} }
} }
} }
res := NewResponse(rsb)
if res.Header != nil { if res.Header != nil {
for key, value := range res.Header { for key, value := range res.Header {
for _, v := range value { for _, v := range value {