Simplify NewServer, add server SetLogger method

This commit is contained in:
some 2024-10-09 11:40:28 -04:00
parent 3c64d0b477
commit 9b1978a863
Signed by: some
GPG Key ID: 65D0589220B9BFC8

View File

@ -23,14 +23,17 @@ type server struct {
serve [numStages][]ServeFunc serve [numStages][]ServeFunc
} }
func NewServer[RB RequestBuilder, R Response](requestBuilder RB, response R, logger log.Logger, serve func(RB) (R, ErrorResponse)) (srv *server) { func NewServer[RB RequestBuilder, R Response](serve func(RB) (R, ErrorResponse)) (srv *server) {
var (
requestBuilder RB
response R
)
srv = &server{ srv = &server{
prototype: Prototype{ prototype: Prototype{
PrototypeRequestBuilder: requestBuilder.Prototype(), PrototypeRequestBuilder: requestBuilder.Prototype(),
PrototypeResponse: response.Prototype(), PrototypeResponse: response.Prototype(),
}, },
logger: logger, serve: [numStages][]ServeFunc{},
serve: [numStages][]ServeFunc{},
} }
srv.serve[main] = append(srv.serve[main], func(req RequestBuilder, res Response) (errRes ErrorResponse) { srv.serve[main] = append(srv.serve[main], func(req RequestBuilder, res Response) (errRes ErrorResponse) {
res, errRes = serve(req.(RB)) res, errRes = serve(req.(RB))
@ -39,6 +42,11 @@ func NewServer[RB RequestBuilder, R Response](requestBuilder RB, response R, log
return srv return srv
} }
func (srv *server) SetLogger(logger log.Logger) *server {
srv.logger = logger
return srv
}
func (srv *server) addServeFunc(when stage, serve ServeFunc) *server { func (srv *server) addServeFunc(when stage, serve ServeFunc) *server {
if srv.serve[when] == nil { if srv.serve[when] == nil {
srv.serve[when] = make([]ServeFunc, 0) srv.serve[when] = make([]ServeFunc, 0)