Skip to content

Commit

Permalink
call OnRequest before handler is called and handle err
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Sep 30, 2024
1 parent c5c5528 commit 74fd14a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
17 changes: 12 additions & 5 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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")))
Expand Down
2 changes: 1 addition & 1 deletion types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

{{- end }}

{{- range $_, $service := $services -}}
{{ range $_, $service := $services -}}
var (
methodAnnotations = map[string]map[string]string{
{{- range $_, $method := $service.Methods }}
Expand Down

0 comments on commit 74fd14a

Please sign in to comment.