Skip to content

Commit

Permalink
render annotations map for request paths and call onRequest hook if d…
Browse files Browse the repository at this point in the history
…efined (#71)

* call onDeprecate hook when method is deprecated

* add request to deprecate hook

* regenerate examples with latest webrpc

* return error from OnRequest hook

* rename to MethodAnnotationsFromContext

* move annotations render to common (client, server) tmpl

* move annotations above WebRPC services

* rename to methodAnotations

* rename to MethodAnnotationsFromCtx

* call OnRequest before handler is called and handle err

* use tabs

* generate annotations for multiple services in the same map

* prevent nil panics

* render map[string]MethodCtx in template

* get method ctx using request

* dont export method

* set updated ctx to Request

* check if req != nil
  • Loading branch information
LukasJenicek authored Oct 2, 2024
1 parent 694c53c commit 0ab8431
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 8 deletions.
78 changes: 74 additions & 4 deletions _examples/golang-basics/example.gen.go

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

1 change: 1 addition & 0 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ service ExampleService
# Status endpoint
#
# gives you current status of running application
@internal
- Status() => (status: bool)
- Version() => (version: Version)
- GetUser(header: map<string,string>, userID: uint64) => (user: User)
Expand Down
62 changes: 59 additions & 3 deletions _examples/golang-imports/api.gen.go

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

22 changes: 21 additions & 1 deletion helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// Helpers
//

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

type contextKey struct {
name string
}
Expand Down Expand Up @@ -43,7 +49,21 @@ func RequestFromContext(ctx context.Context) *http.Request {
return r
}

{{- if $opts.server}}
func MethodCtx(ctx context.Context) (method, bool) {
req := RequestFromContext(ctx)
if req == nil {
return method{}, false
}

m, ok := methods[req.URL.Path]
if !ok {
return method{}, false
}

return m, true
}

{{ if $opts.server}}
func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter {
w, _ := ctx.Value(HTTPResponseWriterCtxKey).(http.ResponseWriter)
return w
Expand Down
14 changes: 14 additions & 0 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type WebRPCServer interface {
type {{$serviceName}} struct {
{{$typePrefix}}{{$service.Name}}
OnError func(r *http.Request, rpcErr *WebRPCError)
OnRequest func(w http.ResponseWriter, r *http.Request) error
}

func New{{firstLetterToUpper $service.Name}}Server(svc {{$typePrefix}}{{.Name}}) *{{$serviceName}} {
Expand All @@ -42,6 +43,8 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, HTTPRequestCtxKey, r)
ctx = context.WithValue(ctx, ServiceNameCtxKey, "{{.Name}}")

r = r.WithContext(ctx)

var handler func(ctx context.Context, w http.ResponseWriter, r *http.Request)
switch r.URL.Path {
{{- range $_, $method := $service.Methods}}
Expand Down Expand Up @@ -69,6 +72,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
18 changes: 18 additions & 0 deletions types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@

{{- end }}

var (
methods = map[string]method{
{{- range $_, $service := $services -}}
{{- range $_, $method := $service.Methods }}
"/rpc/{{$service.Name}}/{{$method.Name}}": {
Name: "{{$method.Name}}",
Service: "{{$service.Name}}",
Annotations: map[string]string{
{{- range $_, $annotation := $method.Annotations -}}
"{{$annotation.AnnotationType}}": "{{$annotation.Value}}",
{{- end -}}
},
},
{{- end -}}
{{ end }}
}
)

var WebRPCServices = map[string][]string{
{{- range $_, $service := $services}}
"{{$service.Name}}": {
Expand Down

0 comments on commit 0ab8431

Please sign in to comment.