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 1afdb0d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 32 deletions.
41 changes: 31 additions & 10 deletions _examples/golang-basics/example.gen.go

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

46 changes: 35 additions & 11 deletions _examples/golang-imports/api.gen.go

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

23 changes: 18 additions & 5 deletions helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// Helpers
//

type MethodCtx struct {
Name string
Service string
Annotations map[string]string
}

type contextKey struct {
name string
}
Expand All @@ -27,7 +33,7 @@ var (

MethodNameCtxKey = &contextKey{"MethodName"}

MethodAnnotationsCtxKey = &contextKey{"MethodAnnotations"}
methodAnnotationsCtxKey = &contextKey{"MethodAnnotations"}
)

func ServiceNameFromContext(ctx context.Context) string {
Expand All @@ -45,12 +51,19 @@ func RequestFromContext(ctx context.Context) *http.Request {
return r
}

func MethodAnnotationsFromContext(ctx context.Context) map[string]string {
annotations, _ := ctx.Value(MethodAnnotationsCtxKey).(map[string]string)
return annotations
func MethodFromContext(ctx context.Context) MethodCtx {
name, _ := ctx.Value(MethodNameCtxKey).(string)
service, _ := ctx.Value(ServiceNameCtxKey).(string)
annotations, _ := ctx.Value(methodAnnotationsCtxKey).(map[string]string)

return MethodCtx{
Name: name,
Service: service,
Annotations: annotations,
}
}

{{- if $opts.server}}
{{ if $opts.server}}
func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter {
w, _ := ctx.Value(HTTPResponseWriterCtxKey).(http.ResponseWriter)
return w
Expand Down
17 changes: 12 additions & 5 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w)
ctx = context.WithValue(ctx, HTTPRequestCtxKey, r)
ctx = context.WithValue(ctx, ServiceNameCtxKey, "{{.Name}}")
ctx = context.WithValue(ctx, MethodAnnotationsCtxKey, methodAnnotations[r.URL.Path])

if s.OnRequest != nil {
s.OnRequest(w, r)
}
ctx = context.WithValue(ctx, methodAnnotationsCtxKey, methodAnnotations[r.URL.Path])

var handler func(ctx context.Context, w http.ResponseWriter, r *http.Request)
switch r.URL.Path {
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 1afdb0d

Please sign in to comment.