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

File diff suppressed because one or more lines are too long

View File

@ -26,9 +26,10 @@ var (
) )
type HttpOption struct { type HttpOption struct {
Path []string Path string
Method string Method string
Body string Body string
Additional []*HttpOption
} }
var ProtoHelpersFuncMap = template.FuncMap{ var ProtoHelpersFuncMap = template.FuncMap{
@ -1351,18 +1352,64 @@ func httpOption(m *descriptor.MethodDescriptorProto) *HttpOption {
if err != nil { if err != nil {
return nil return nil
} }
_, ok := ext.(*options.HttpRule) hr, ok := ext.(*options.HttpRule)
if !ok { if !ok {
panic(fmt.Sprintf("extension is %T; want an HttpRule", ext)) panic(fmt.Sprintf("extension is %T; want an HttpRule", ext))
} }
opt := &HttpOption{Method: httpVerb(m), Body: httpBody(m)} var method string
if path := httpPath(m); path != "" { var path string
opt.Path = append(opt.Path, path) switch t := hr.Pattern.(type) {
default:
break
case *options.HttpRule_Get:
method = "GET"
path = t.Get
case *options.HttpRule_Post:
method = "POST"
path = t.Post
case *options.HttpRule_Put:
method = "PUT"
path = t.Put
case *options.HttpRule_Delete:
method = "DELETE"
path = t.Delete
case *options.HttpRule_Patch:
method = "PATCH"
path = t.Patch
case *options.HttpRule_Custom:
method = t.Custom.Kind
path = t.Custom.Path
} }
for _, path := range httpPathsAdditionalBindings(m) { opt := &HttpOption{Method: method, Body: hr.Body, Path: path}
opt.Path = append(opt.Path, path)
for _, ahr := range hr.AdditionalBindings {
switch t := ahr.Pattern.(type) {
default:
break
case *options.HttpRule_Get:
method = "GET"
path = t.Get
case *options.HttpRule_Post:
method = "POST"
path = t.Post
case *options.HttpRule_Put:
method = "PUT"
path = t.Put
case *options.HttpRule_Delete:
method = "DELETE"
path = t.Delete
case *options.HttpRule_Patch:
method = "PATCH"
path = t.Patch
case *options.HttpRule_Custom:
method = t.Custom.Kind
path = t.Custom.Path
}
aopt := &HttpOption{Method: method, Body: ahr.Body, Path: path}
opt.Additional = append(opt.Additional, aopt)
} }
return opt return opt

View File

@ -24,7 +24,7 @@ func Register(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h) v := reflect.ValueOf(h)
if v.NumMethod() < 1 { 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 { 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) v := reflect.ValueOf(h)
if v.NumMethod() < 1 { 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 { for _, ep := range eps {

View File

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

View File

@ -82,17 +82,6 @@ func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, re
{{- end}} {{- 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)}} {{if or (.ServerStreaming) (.ClientStreaming)}}
type {{$ServiceName | lowerFirst}}Service{{.Name}} struct { type {{$ServiceName | lowerFirst}}Service{{.Name}} struct {
stream micro_client.Stream stream micro_client.Stream
@ -235,3 +224,23 @@ func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}},
{{- end}} {{- end}}
{{- 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 }}