Attempt generic NewServer
This commit is contained in:
parent
f6982b6de5
commit
c0dedb7d96
@ -3,6 +3,7 @@ package router
|
|||||||
import "io"
|
import "io"
|
||||||
|
|
||||||
type RequestBuilder interface {
|
type RequestBuilder interface {
|
||||||
|
Prototype() (req PrototypeRequestBuilder)
|
||||||
Allowed(method string) (errRes ErrorResponse)
|
Allowed(method string) (errRes ErrorResponse)
|
||||||
Header(header Header) (errRes ErrorResponse)
|
Header(header Header) (errRes ErrorResponse)
|
||||||
ReadBody(body io.ReadCloser) (errRes ErrorResponse)
|
ReadBody(body io.ReadCloser) (errRes ErrorResponse)
|
||||||
@ -14,6 +15,7 @@ type PrototypeRequestBuilder interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Response interface {
|
type Response interface {
|
||||||
|
Prototype() (req PrototypeResponse)
|
||||||
Header() (header Header)
|
Header() (header Header)
|
||||||
BodyBytes() (body []byte)
|
BodyBytes() (body []byte)
|
||||||
}
|
}
|
||||||
|
16
server.go
16
server.go
@ -23,13 +23,19 @@ type server struct {
|
|||||||
serve [numStages][]ServeFunc
|
serve [numStages][]ServeFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(prototype Prototype, logger log.Logger, serve ServeFunc) (srv *server) {
|
func NewServer[RB RequestBuilder, R Response](requestBuilder RB, response R, logger log.Logger, serve func(RB) (R, ErrorResponse)) (srv *server) {
|
||||||
srv = &server{
|
srv = &server{
|
||||||
prototype: prototype,
|
prototype: Prototype{
|
||||||
logger: logger,
|
PrototypeRequestBuilder: requestBuilder.Prototype(),
|
||||||
serve: [numStages][]ServeFunc{},
|
PrototypeResponse: response.Prototype(),
|
||||||
|
},
|
||||||
|
logger: logger,
|
||||||
|
serve: [numStages][]ServeFunc{},
|
||||||
}
|
}
|
||||||
srv.serve[main] = append(srv.serve[main], serve)
|
srv.serve[main] = append(srv.serve[main], func(req RequestBuilder, res Response) (errRes ErrorResponse) {
|
||||||
|
res, errRes = serve(req.(RB))
|
||||||
|
return
|
||||||
|
})
|
||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user