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")))