many fixes with http stuff

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-01-30 19:34:30 +03:00
parent 2e4e0eecfb
commit 57187e75dd
6 changed files with 124 additions and 46 deletions

View File

@@ -24,7 +24,7 @@ func Register(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
if v.NumMethod() < 1 {
return fmt.Errorf("invalid handler specified: %#+v", h)
return fmt.Errorf("handler has no methods: %T", h)
}
for _, ep := range eps {

View File

@@ -15,7 +15,7 @@ func Register(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
if v.NumMethod() < 1 {
return fmt.Errorf("invalid handler specified: %#+v", h)
return fmt.Errorf("handler has no methods: %T", h)
}
for _, ep := range eps {

View File

@@ -11,30 +11,52 @@ import (
)
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
{{- $epnum := 0 }}
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- if ne (httpVerb .) "" }}
{{- $epnum = add $epnum 1 }}
{{- range httpPathsAdditionalBindings . }}
{{- $epnum = add $epnum 1 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
// New{{$ServiceName}}Endpoints provides api endpoints metdata for {{$ServiceName}} service
func New{{$ServiceName}}Endpoints() []*micro_api.Endpoint {
var endpoints []*micro_api.Endpoint
endpoints := make([]*micro_api.Endpoint, 0, {{ $epnum }})
{{- if ne $epnum 0 }}
var endpoint *micro_api.Endpoint
{{- end }}
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- if ne (httpVerb .) "" }}
endpoint := &micro_api.Endpoint{
{{- $httpOption := (httpOption .) }}
{{- if ne $httpOption.Method "" }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{httpPath .}}"},
Method: []string{"{{httpVerb .}}"},
{{- if ne (httpBody .) "" }}
Body: "{{httpBody .}}",
{{- end}}
Path: []string{"{{$httpOption.Path}}"},
Method: []string{"{{$httpOption.Method}}"},
Body: "{{$httpOption.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
{{- range httpPathsAdditionalBindings . }}
endpoint.Path = append(endpoint.Path, "{{.}}")
{{- end}}
endpoints = append(endpoints, endpoint)
{{- range $index, $element := $httpOption.Additional }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{$element.Path}}"},
Method: []string{"{{$element.Method}}"},
Body: "{{$element.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
{{- end}}
{{- end}}
{{- end}}
{{- end}}

View File

@@ -82,17 +82,6 @@ func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, re
{{- end}}
}
{{- if not (contains (json (openapiOption .)) "null") }}
{{- if (openapiOption .).Responses }}
{{ range $k, $v := (openapiOption .).Responses }}
// Error method to satisfy error interface
func (e *{{- (getMessageType $File $v.Schema.JsonSchema.Ref).Name }}) Error() string {
return fmt.Sprintf("%#v", e)
}
{{- end }}
{{- end }}
{{- end }}
{{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}Service{{.Name}} struct {
stream micro_client.Stream
@@ -235,3 +224,23 @@ func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}},
{{- end}}
{{- end}}
{{- $errmsg := list }}
{{range .Service.Method}}
{{- if not (contains (json (openapiOption .)) "null") }}
{{- if (openapiOption .).Responses }}
{{ range $k, $v := (openapiOption .).Responses }}
{{- $msgtype := (getMessageType $File $v.Schema.JsonSchema.Ref) }}
{{- $errmsg = append $errmsg $msgtype.Name }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{range $k, $v := ($errmsg | uniq) }}
// Error method to satisfy error interface
func (e *{{- $v }}) Error() string {
return fmt.Sprintf("%#v", e)
}
{{- end }}