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 all 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
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)
LukasJenicek marked this conversation as resolved.
Show resolved Hide resolved
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
Loading