From 0edbef31734665ea07b588fdcd163a92db88dc67 Mon Sep 17 00:00:00 2001 From: Daniel Thomas Date: Fri, 1 Sep 2023 11:11:36 +0200 Subject: [PATCH] feat: add gotsrpc clients and proxy --- gotsrpc.yml | 10 ++- validation/gotsrpc_gen.go | 144 ++++++++++++++++++++++++++++++++ validation/gotsrpcclient_gen.go | 81 ++++++++++++++++++ webhook/gotsrpc_gen.go | 79 ++++++++++++++++++ webhook/gotsrpcclient_gen.go | 47 +++++++++++ 5 files changed, 358 insertions(+), 3 deletions(-) create mode 100644 validation/gotsrpc_gen.go create mode 100644 validation/gotsrpcclient_gen.go create mode 100644 webhook/gotsrpc_gen.go create mode 100644 webhook/gotsrpcclient_gen.go diff --git a/gotsrpc.yml b/gotsrpc.yml index a76cf2d..c08b1bd 100644 --- a/gotsrpc.yml +++ b/gotsrpc.yml @@ -3,11 +3,15 @@ module: path: ./ targets: - services: + validation: services: /services/contenfulvalidation/validation: Validation - /services/contenfulvalidation/webhook: Webhook - package: github.com/foomo/contentfulvalidation/services + package: github.com/foomo/contentfulvalidation/validation tsrpc: - Validation + webhook: + services: + /services/contenfulvalidation/webhook: Webhook + package: github.com/foomo/contentfulvalidation/webhook + tsrpc: - Webhook diff --git a/validation/gotsrpc_gen.go b/validation/gotsrpc_gen.go new file mode 100644 index 0000000..1b5510a --- /dev/null +++ b/validation/gotsrpc_gen.go @@ -0,0 +1,144 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package validation + +import ( + io "io" + http "net/http" + time "time" + + github_com_foomo_contentfulvalidation_validator "github.com/foomo/contentfulvalidation/validator" + gotsrpc "github.com/foomo/gotsrpc/v2" +) + +const ( + ValidationGoTSRPCProxyListModelTypes = "ListModelTypes" + ValidationGoTSRPCProxyValidateEntity = "ValidateEntity" + ValidationGoTSRPCProxyValidationResult = "ValidationResult" + ValidationGoTSRPCProxyValidationResults = "ValidationResults" +) + +type ValidationGoTSRPCProxy struct { + EndPoint string + service Validation +} + +func NewDefaultValidationGoTSRPCProxy(service Validation) *ValidationGoTSRPCProxy { + return NewValidationGoTSRPCProxy(service, "/services/contenfulvalidation/validation") +} + +func NewValidationGoTSRPCProxy(service Validation, endpoint string) *ValidationGoTSRPCProxy { + return &ValidationGoTSRPCProxy{ + EndPoint: endpoint, + service: service, + } +} + +// ServeHTTP exposes your service +func (p *ValidationGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodOptions { + return + } else if r.Method != http.MethodPost { + gotsrpc.ErrorMethodNotAllowed(w) + return + } + defer io.Copy(io.Discard, r.Body) // Drain Request Body + + funcName := gotsrpc.GetCalledFunc(r, p.EndPoint) + callStats, _ := gotsrpc.GetStatsForRequest(r) + callStats.Func = funcName + callStats.Package = "github.com/foomo/contentfulvalidation/validation" + callStats.Service = "Validation" + switch funcName { + case ValidationGoTSRPCProxyListModelTypes: + var ( + args []interface{} + rets []interface{} + ) + executionStart := time.Now() + listModelTypesAvailableModelTypes := p.service.ListModelTypes() + callStats.Execution = time.Since(executionStart) + rets = []interface{}{listModelTypesAvailableModelTypes} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + case ValidationGoTSRPCProxyValidateEntity: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_modelType github_com_foomo_contentfulvalidation_validator.ModelType + arg_modelID github_com_foomo_contentfulvalidation_validator.ModelID + arg_commit bool + ) + args = []interface{}{&arg_modelType, &arg_modelID, &arg_commit} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + validateEntityValidationResult, validateEntityValidationError := p.service.ValidateEntity(arg_modelType, arg_modelID, arg_commit) + callStats.Execution = time.Since(executionStart) + rets = []interface{}{validateEntityValidationResult, validateEntityValidationError} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + case ValidationGoTSRPCProxyValidationResult: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_modelType github_com_foomo_contentfulvalidation_validator.ModelType + arg_modelID github_com_foomo_contentfulvalidation_validator.ModelID + ) + args = []interface{}{&arg_modelType, &arg_modelID} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + validationResultValidationResult, validationResultValidationError := p.service.ValidationResult(arg_modelType, arg_modelID) + callStats.Execution = time.Since(executionStart) + rets = []interface{}{validationResultValidationResult, validationResultValidationError} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + case ValidationGoTSRPCProxyValidationResults: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_modelType github_com_foomo_contentfulvalidation_validator.ModelType + ) + args = []interface{}{&arg_modelType} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + validationResultsValidationResults, validationResultsValidationError := p.service.ValidationResults(arg_modelType) + callStats.Execution = time.Since(executionStart) + rets = []interface{}{validationResultsValidationResults, validationResultsValidationError} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + default: + gotsrpc.ClearStats(r) + gotsrpc.ErrorFuncNotFound(w) + } +} diff --git a/validation/gotsrpcclient_gen.go b/validation/gotsrpcclient_gen.go new file mode 100644 index 0000000..3a1a898 --- /dev/null +++ b/validation/gotsrpcclient_gen.go @@ -0,0 +1,81 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package validation + +import ( + go_context "context" + go_net_http "net/http" + + github_com_foomo_contentfulvalidation_errors "github.com/foomo/contentfulvalidation/errors" + github_com_foomo_contentfulvalidation_validator "github.com/foomo/contentfulvalidation/validator" + gotsrpc "github.com/foomo/gotsrpc/v2" + pkg_errors "github.com/pkg/errors" +) + +type ValidationGoTSRPCClient interface { + ListModelTypes(ctx go_context.Context) (availableModelTypes []*github_com_foomo_contentfulvalidation_validator.ModelTypeInfo, clientErr error) + ValidateEntity(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID, commit bool) (validationResult *github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) + ValidationResult(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID) (validationResult *github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) + ValidationResults(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType) (validationResults map[github_com_foomo_contentfulvalidation_validator.ModelID]*github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) +} + +type HTTPValidationGoTSRPCClient struct { + URL string + EndPoint string + Client gotsrpc.Client +} + +func NewDefaultValidationGoTSRPCClient(url string) *HTTPValidationGoTSRPCClient { + return NewValidationGoTSRPCClient(url, "/services/contenfulvalidation/validation") +} + +func NewValidationGoTSRPCClient(url string, endpoint string) *HTTPValidationGoTSRPCClient { + return NewValidationGoTSRPCClientWithClient(url, endpoint, nil) +} + +func NewValidationGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPValidationGoTSRPCClient { + return &HTTPValidationGoTSRPCClient{ + URL: url, + EndPoint: endpoint, + Client: gotsrpc.NewClientWithHttpClient(client), + } +} +func (tsc *HTTPValidationGoTSRPCClient) ListModelTypes(ctx go_context.Context) (availableModelTypes []*github_com_foomo_contentfulvalidation_validator.ModelTypeInfo, clientErr error) { + args := []interface{}{} + reply := []interface{}{&availableModelTypes} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "ListModelTypes", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call validation.ValidationGoTSRPCProxy ListModelTypes") + } + return +} + +func (tsc *HTTPValidationGoTSRPCClient) ValidateEntity(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID, commit bool) (validationResult *github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) { + args := []interface{}{modelType, modelID, commit} + reply := []interface{}{&validationResult, &validationError} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "ValidateEntity", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call validation.ValidationGoTSRPCProxy ValidateEntity") + } + return +} + +func (tsc *HTTPValidationGoTSRPCClient) ValidationResult(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID) (validationResult *github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) { + args := []interface{}{modelType, modelID} + reply := []interface{}{&validationResult, &validationError} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "ValidationResult", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call validation.ValidationGoTSRPCProxy ValidationResult") + } + return +} + +func (tsc *HTTPValidationGoTSRPCClient) ValidationResults(ctx go_context.Context, modelType github_com_foomo_contentfulvalidation_validator.ModelType) (validationResults map[github_com_foomo_contentfulvalidation_validator.ModelID]*github_com_foomo_contentfulvalidation_validator.ValidationResult, validationError *github_com_foomo_contentfulvalidation_errors.ValidationError, clientErr error) { + args := []interface{}{modelType} + reply := []interface{}{&validationResults, &validationError} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "ValidationResults", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call validation.ValidationGoTSRPCProxy ValidationResults") + } + return +} diff --git a/webhook/gotsrpc_gen.go b/webhook/gotsrpc_gen.go new file mode 100644 index 0000000..f3fe430 --- /dev/null +++ b/webhook/gotsrpc_gen.go @@ -0,0 +1,79 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package webhook + +import ( + io "io" + http "net/http" + time "time" + + github_com_foomo_contentfulvalidation_validator "github.com/foomo/contentfulvalidation/validator" + gotsrpc "github.com/foomo/gotsrpc/v2" +) + +const ( + WebhookGoTSRPCProxyUpdateCache = "UpdateCache" +) + +type WebhookGoTSRPCProxy struct { + EndPoint string + service Webhook +} + +func NewDefaultWebhookGoTSRPCProxy(service Webhook) *WebhookGoTSRPCProxy { + return NewWebhookGoTSRPCProxy(service, "/services/contenfulvalidation/webhook") +} + +func NewWebhookGoTSRPCProxy(service Webhook, endpoint string) *WebhookGoTSRPCProxy { + return &WebhookGoTSRPCProxy{ + EndPoint: endpoint, + service: service, + } +} + +// ServeHTTP exposes your service +func (p *WebhookGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodOptions { + return + } else if r.Method != http.MethodPost { + gotsrpc.ErrorMethodNotAllowed(w) + return + } + defer io.Copy(io.Discard, r.Body) // Drain Request Body + + funcName := gotsrpc.GetCalledFunc(r, p.EndPoint) + callStats, _ := gotsrpc.GetStatsForRequest(r) + callStats.Func = funcName + callStats.Package = "github.com/foomo/contentfulvalidation/webhook" + callStats.Service = "Webhook" + switch funcName { + case WebhookGoTSRPCProxyUpdateCache: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_sysType github_com_foomo_contentfulvalidation_validator.SysType + arg_modelType github_com_foomo_contentfulvalidation_validator.ModelType + arg_modelID github_com_foomo_contentfulvalidation_validator.ModelID + ) + args = []interface{}{&arg_sysType, &arg_modelType, &arg_modelID} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + p.service.UpdateCache(arg_sysType, arg_modelType, arg_modelID) + callStats.Execution = time.Since(executionStart) + rets = []interface{}{} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + default: + gotsrpc.ClearStats(r) + gotsrpc.ErrorFuncNotFound(w) + } +} diff --git a/webhook/gotsrpcclient_gen.go b/webhook/gotsrpcclient_gen.go new file mode 100644 index 0000000..9d8a554 --- /dev/null +++ b/webhook/gotsrpcclient_gen.go @@ -0,0 +1,47 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package webhook + +import ( + go_context "context" + go_net_http "net/http" + + github_com_foomo_contentfulvalidation_validator "github.com/foomo/contentfulvalidation/validator" + gotsrpc "github.com/foomo/gotsrpc/v2" + pkg_errors "github.com/pkg/errors" +) + +type WebhookGoTSRPCClient interface { + UpdateCache(ctx go_context.Context, sysType github_com_foomo_contentfulvalidation_validator.SysType, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID) (clientErr error) +} + +type HTTPWebhookGoTSRPCClient struct { + URL string + EndPoint string + Client gotsrpc.Client +} + +func NewDefaultWebhookGoTSRPCClient(url string) *HTTPWebhookGoTSRPCClient { + return NewWebhookGoTSRPCClient(url, "/services/contenfulvalidation/webhook") +} + +func NewWebhookGoTSRPCClient(url string, endpoint string) *HTTPWebhookGoTSRPCClient { + return NewWebhookGoTSRPCClientWithClient(url, endpoint, nil) +} + +func NewWebhookGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPWebhookGoTSRPCClient { + return &HTTPWebhookGoTSRPCClient{ + URL: url, + EndPoint: endpoint, + Client: gotsrpc.NewClientWithHttpClient(client), + } +} +func (tsc *HTTPWebhookGoTSRPCClient) UpdateCache(ctx go_context.Context, sysType github_com_foomo_contentfulvalidation_validator.SysType, modelType github_com_foomo_contentfulvalidation_validator.ModelType, modelID github_com_foomo_contentfulvalidation_validator.ModelID) (clientErr error) { + args := []interface{}{sysType, modelType, modelID} + reply := []interface{}{} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "UpdateCache", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call webhook.WebhookGoTSRPCProxy UpdateCache") + } + return +}