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"
|
"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 {
|
type Error interface {
|
||||||
Error(e Error) (err Error)
|
Error(e Error) (err Error)
|
||||||
Status() (code int)
|
Status() (code int)
|
||||||
@ -38,10 +24,14 @@ type GenericRequest struct {
|
|||||||
|
|
||||||
type Request[RQB RequestBuilder] struct {
|
type Request[RQB RequestBuilder] struct {
|
||||||
GenericRequest
|
GenericRequest
|
||||||
|
requestBuilder RQB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequest[RQB RequestBuilder](rqb RQB) *Request[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 {
|
func (*Request[RQB]) RequestBuilder() RequestBuilder {
|
||||||
@ -60,9 +50,31 @@ type RequestBuilder interface {
|
|||||||
Request() *GenericRequest
|
Request() *GenericRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Response[RSB]) ResponseBuilder() ResponseBuilder {
|
func NewRequestBuilder[RQB RequestBuilder]() RQB {
|
||||||
var rsb RSB
|
var rqb RQB
|
||||||
return rsb.New()
|
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 {
|
type ResponseBuilder interface {
|
||||||
@ -73,6 +85,11 @@ type ResponseBuilder interface {
|
|||||||
Response() *GenericResponse
|
Response() *GenericResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewResponseBuilder[RSB ResponseBuilder]() RSB {
|
||||||
|
var rsb RSB
|
||||||
|
return rsb.New().(RSB)
|
||||||
|
}
|
||||||
|
|
||||||
type writer struct {
|
type writer struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
log.Logger
|
log.Logger
|
||||||
@ -103,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], res *Response[RSB]) (err Error)
|
||||||
|
|
||||||
type server[RQB RequestBuilder, RSB ResponseBuilder] struct {
|
type server[RQB RequestBuilder, RSB ResponseBuilder] struct {
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
@ -123,29 +140,28 @@ func (srv *server[RQB, RSB]) SetLogger(logger log.Logger) *server[RQB, RSB] {
|
|||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *server[RQB, RSB]) addServeFunc(when serveStage, serve ServeFunc[RQB, RSB]) *server[RQB, RSB] {
|
func (srv *server[RQB, RSB]) addServeFunc(stage serveStage, serve ServeFunc[RQB, RSB]) *server[RQB, RSB] {
|
||||||
if srv.serve[when] == nil {
|
if srv.serve[stage] == nil {
|
||||||
srv.serve[when] = make([]ServeFunc[RQB, RSB], 0)
|
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
|
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)
|
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)
|
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)
|
return srv.addServeFunc(servePost, serve)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (srv *server[RQB, RSB]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
var rqb RQB
|
rqb := NewRequestBuilder[RQB]()
|
||||||
rqb = rqb.New().(RQB)
|
|
||||||
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
|
||||||
@ -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 {
|
if ok := wr.handleError(rqb.SetValues(r.Form)); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
req := NewRequest(rqb)
|
||||||
|
res := NewResponse(NewResponseBuilder[RSB]())
|
||||||
for _, stage := range srv.serve {
|
for _, stage := range srv.serve {
|
||||||
for _, s := range stage {
|
for _, serve := range stage {
|
||||||
res, err := s(NewRequest(rqb))
|
err := serve(req, res)
|
||||||
if ok := wr.handleError(err); !ok {
|
if ok := wr.handleError(err); !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if res.Header != nil {
|
}
|
||||||
for key, value := range res.Header {
|
}
|
||||||
for _, v := range value {
|
if res.Header != nil {
|
||||||
wr.Header().Add(key, v)
|
for key, value := range res.Header {
|
||||||
}
|
for _, v := range value {
|
||||||
}
|
wr.Header().Add(key, v)
|
||||||
}
|
|
||||||
if len(res.Body) > 0 {
|
|
||||||
wr.Write(res.Body)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
wr.WriteHeader(res.Status)
|
||||||
|
if len(res.Body) > 0 {
|
||||||
|
wr.Write(res.Body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user