Change res to input parameter, update server
This commit is contained in:
parent
cb88fedc53
commit
ef122ccaea
97
server.go
97
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user