Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render annotations map for request paths and call onRequest hook if defined #71

Merged
merged 18 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 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
49 changes: 46 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 MethodCtx struct {
Name string
Service string
Annotations map[string]string
}
LukasJenicek marked this conversation as resolved.
Show resolved Hide resolved

type contextKey struct {
name string
}
Expand All @@ -26,6 +32,8 @@ var (
ServiceNameCtxKey = &contextKey{"ServiceName"}

MethodNameCtxKey = &contextKey{"MethodName"}

methodAnnotationsCtxKey = &contextKey{"MethodAnnotations"}
)

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

{{- if $opts.server}}
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}}
func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter {
w, _ := ctx.Value(HTTPResponseWriterCtxKey).(http.ResponseWriter)
return w
Expand Down
13 changes: 13 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 @@ -41,6 +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])

var handler func(ctx context.Context, w http.ResponseWriter, r *http.Request)
switch r.URL.Path {
Expand Down Expand Up @@ -69,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
}
}

LukasJenicek marked this conversation as resolved.
Show resolved Hide resolved
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
14 changes: 14 additions & 0 deletions types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

{{- end }}

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

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