Attempt generic NewServer
This commit is contained in:
parent
f6982b6de5
commit
c0dedb7d96
@ -3,6 +3,7 @@ package router
|
||||
import "io"
|
||||
|
||||
type RequestBuilder interface {
|
||||
Prototype() (req PrototypeRequestBuilder)
|
||||
Allowed(method string) (errRes ErrorResponse)
|
||||
Header(header Header) (errRes ErrorResponse)
|
||||
ReadBody(body io.ReadCloser) (errRes ErrorResponse)
|
||||
@ -14,6 +15,7 @@ type PrototypeRequestBuilder interface {
|
||||
}
|
||||
|
||||
type Response interface {
|
||||
Prototype() (req PrototypeResponse)
|
||||
Header() (header Header)
|
||||
BodyBytes() (body []byte)
|
||||
}
|
||||
|
16
server.go
16
server.go
@ -23,13 +23,19 @@ type server struct {
|
||||
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{
|
||||
prototype: prototype,
|
||||
logger: logger,
|
||||
serve: [numStages][]ServeFunc{},
|
||||
prototype: Prototype{
|
||||
PrototypeRequestBuilder: requestBuilder.Prototype(),
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user