diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index af47cad..cf59991 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ // example v0.0.1 05b7a5c86b98738f4fe6ce9bb1fccd4af064847a // -- -// Code generated by webrpc-gen@v0.19.3-11-g71ce490 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.19.3-14-g44bb43f with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -legacyErrors -fixEmptyArrays -out=./example.gen.go package main @@ -249,10 +249,6 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) ctx = context.WithValue(ctx, ServiceNameCtxKey, "ExampleService") 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 { case "/rpc/ExampleService/Ping": @@ -288,6 +284,17 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) switch contentType { case "application/json": + if s.OnRequest != nil { + if err := s.OnRequest(w, r); err != nil { + rpcErr, ok := err.(WebRPCError) + if !ok { + rpcErr = ErrWebrpcEndpoint.WithCause(err) + } + s.sendErrorJSON(w, r, rpcErr) + 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"))) diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 213d8fe..1a70e82 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ // example-api-service v1.0.0 cae4e128f4fb4c938bfe1ea312deeea3dfd6b6af // -- -// Code generated by webrpc-gen@v0.19.3-11-g71ce490 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.19.3-14-g44bb43f with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client -legacyErrors=true -fmt=false package main @@ -82,7 +82,9 @@ func (x *Location) Is(values ...Location) bool { } } return false -}var ( +} + +var ( methodAnnotations = map[string]map[string]string{ "/rpc/ExampleAPI/Ping": {}, "/rpc/ExampleAPI/Status": {}, @@ -161,10 +163,6 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = context.WithValue(ctx, ServiceNameCtxKey, "ExampleAPI") 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 { case "/rpc/ExampleAPI/Ping": @@ -194,6 +192,17 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch contentType { case "application/json": + if s.OnRequest != nil { + if err := s.OnRequest(w, r); err != nil { + rpcErr, ok := err.(WebRPCError) + if !ok { + rpcErr = ErrWebrpcEndpoint.WithCause(err) + } + s.sendErrorJSON(w, r, rpcErr) + 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"))) diff --git a/server.go.tmpl b/server.go.tmpl index 87039a1..b528791 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,17 @@ 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 { + rpcErr, ok := err.(WebRPCError) + if !ok { + rpcErr = ErrWebrpcEndpoint.WithCause(err) + } + s.sendErrorJSON(w, r, rpcErr) + 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"))) diff --git a/types.go.tmpl b/types.go.tmpl index 5e0af53..1a1b2dd 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -29,7 +29,7 @@ {{- end }} -{{- range $_, $service := $services -}} +{{ range $_, $service := $services -}} var ( methodAnnotations = map[string]map[string]string{ {{- range $_, $method := $service.Methods }}