Attempt generic NewServer

This commit is contained in:
some 2024-10-09 00:15:18 -04:00
parent f6982b6de5
commit c0dedb7d96
Signed by: some
GPG Key ID: 65D0589220B9BFC8
2 changed files with 13 additions and 5 deletions

View File

@ -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)
}

View File

@ -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
}