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 6ce325b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 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,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")))
Expand Down

0 comments on commit 6ce325b

Please sign in to comment.