diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 389dd8d..8da63a9 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,4 +1,4 @@ -// example v0.0.1 87cdac57aac886a37b5caffc5962c93859970b68 +// example v0.0.1 388b5585cc00538cd2898bc666a664585923c3d1 // -- // Code generated by webrpc-gen@v0.14.0-dev with ../../../gen-golang generator. DO NOT EDIT. // @@ -33,7 +33,7 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "87cdac57aac886a37b5caffc5962c93859970b68" + return "388b5585cc00538cd2898bc666a664585923c3d1" } // @@ -115,6 +115,7 @@ type ExampleService interface { Version(ctx context.Context) (*Version, error) GetUser(ctx context.Context, header map[string]string, userID uint64) (*User, error) FindUser(ctx context.Context, s *SearchFilter) (string, *User, error) + LogEvent(ctx context.Context, event string) error } var WebRPCServices = map[string][]string{ @@ -124,6 +125,7 @@ var WebRPCServices = map[string][]string{ "Version", "GetUser", "FindUser", + "LogEvent", }, } @@ -171,6 +173,8 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) handler = s.serveGetUserJSON case "/rpc/ExampleService/FindUser": handler = s.serveFindUserJSON + case "/rpc/ExampleService/LogEvent": + handler = s.serveLogEventJSON default: err := ErrorWithCause(ErrWebrpcBadRoute, fmt.Errorf("no handler for path %q", r.URL.Path)) RespondWithError(w, err) @@ -266,7 +270,6 @@ func (s *exampleServiceServer) serveVersionJSON(ctx context.Context, w http.Resp } func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.ResponseWriter, r *http.Request) { - reqBody, err := io.ReadAll(r.Body) if err != nil { RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) @@ -307,7 +310,6 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp } func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.ResponseWriter, r *http.Request) { - reqBody, err := io.ReadAll(r.Body) if err != nil { RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) @@ -347,6 +349,36 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res w.Write(respBody) } +func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.ResponseWriter, r *http.Request) { + reqBody, err := io.ReadAll(r.Body) + if err != nil { + RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) + return + } + defer r.Body.Close() + + reqPayload := struct { + Arg0 string `json:"event"` + }{} + if err := json.Unmarshal(reqBody, &reqPayload); err != nil { + RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to unmarshal request data: %w", err))) + return + } + + ctx = context.WithValue(ctx, MethodNameCtxKey, "LogEvent") + + // Call service method implementation. + err = s.ExampleService.LogEvent(ctx, reqPayload.Arg0) + if err != nil { + RespondWithError(w, err) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write([]byte("{}")) +} + func RespondWithError(w http.ResponseWriter, err error) { rpcErr, ok := err.(WebRPCError) if !ok { @@ -368,17 +400,18 @@ const ExampleServicePathPrefix = "/rpc/ExampleService/" type exampleServiceClient struct { client HTTPClient - urls [5]string + urls [6]string } func NewExampleServiceClient(addr string, client HTTPClient) ExampleService { prefix := urlBase(addr) + ExampleServicePathPrefix - urls := [5]string{ + urls := [6]string{ prefix + "Ping", prefix + "Status", prefix + "Version", prefix + "GetUser", prefix + "FindUser", + prefix + "LogEvent", } return &exampleServiceClient{ client: client, @@ -436,6 +469,15 @@ func (c *exampleServiceClient) FindUser(ctx context.Context, s *SearchFilter) (s return out.Ret0, out.Ret1, err } +func (c *exampleServiceClient) LogEvent(ctx context.Context, event string) error { + in := struct { + Arg0 string `json:"event"` + }{event} + + err := doJSONRequest(ctx, c.client, c.urls[5], in, nil) + return err +} + // HTTPClient is the interface used by generated clients to send HTTP requests. // It is fulfilled by *(net/http).Client, which is sufficient for most users. // Users can provide their own implementation for special retry policies. diff --git a/_examples/golang-basics/example.ridl b/_examples/golang-basics/example.ridl index bba44de..97e6fb1 100644 --- a/_examples/golang-basics/example.ridl +++ b/_examples/golang-basics/example.ridl @@ -76,3 +76,4 @@ service ExampleService - Version() => (version: Version) - GetUser(header: map, userID: uint64) => (user: User) - FindUser(s: SearchFilter) => (name: string, user: User) + - LogEvent(event: string) diff --git a/_examples/golang-basics/main.go b/_examples/golang-basics/main.go index 06666f5..61b5a50 100644 --- a/_examples/golang-basics/main.go +++ b/_examples/golang-basics/main.go @@ -90,3 +90,7 @@ func (rpc *ExampleServiceRPC) FindUser(ctx context.Context, s *SearchFilter) (st Username: name, }, nil } + +func (rpc *ExampleServiceRPC) LogEvent(ctx context.Context, event string) error { + return nil +} diff --git a/server.go.tmpl b/server.go.tmpl index 4f98d07..f02f234 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -74,7 +74,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { } {{range .Methods }} func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context.Context, w http.ResponseWriter, r *http.Request) { - {{ if .Inputs|len}} + {{ if gt (len .Inputs) 0 -}} reqBody, err := io.ReadAll(r.Body) if err != nil { RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) @@ -96,13 +96,13 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context ctx = context.WithValue(ctx, MethodNameCtxKey, "{{.Name}}") // Call service method implementation. - {{range $i, $output := .Outputs}}ret{{$i}}, {{end}}err := s.{{$name}}.{{.Name}}(ctx{{range $i, $_ := .Inputs}}, reqPayload.Arg{{$i}}{{end}}) + {{range $i, $output := .Outputs}}ret{{$i}}, {{end}}err {{if or (eq (len .Inputs) 0) (gt (len .Outputs) 0)}}:{{end}}= s.{{$name}}.{{.Name}}(ctx{{range $i, $_ := .Inputs}}, reqPayload.Arg{{$i}}{{end}}) if err != nil { RespondWithError(w, err) return } - {{- if .Outputs | len}} + {{- if gt (len .Outputs) 0}} respPayload := struct { {{- range $i, $output := .Outputs}}