From 6ce325bcea82d26a48d53310d9cce4690b17220f Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Mon, 30 Sep 2024 12:30:05 +0200 Subject: [PATCH] call OnRequest before handler is called and handle err --- server.go.tmpl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server.go.tmpl b/server.go.tmpl index 87039a1..20e1022 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -44,10 +44,6 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = context.WithValue(ctx, ServiceNameCtxKey, "{{.Name}}") ctx = context.WithValue(ctx, MethodAnnotationsCtxKey, methodAnnotations[r.URL.Path]) - if s.OnRequest != nil { - s.OnRequest(w, r) - } - var handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) switch r.URL.Path { {{- range $_, $method := $service.Methods}} @@ -75,6 +71,13 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch contentType { case "application/json": + if s.OnRequest != nil { + if err := s.OnRequest(w, r); err != nil { + s.sendErrorJSON(w, r, err) + return + } + } + handler(ctx, w, r) default: err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type")))