many fixes with http stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -26,9 +26,10 @@ var ( | ||||
| ) | ||||
|  | ||||
| type HttpOption struct { | ||||
| 	Path   []string | ||||
| 	Method string | ||||
| 	Body   string | ||||
| 	Path       string | ||||
| 	Method     string | ||||
| 	Body       string | ||||
| 	Additional []*HttpOption | ||||
| } | ||||
|  | ||||
| var ProtoHelpersFuncMap = template.FuncMap{ | ||||
| @@ -1351,18 +1352,64 @@ func httpOption(m *descriptor.MethodDescriptorProto) *HttpOption { | ||||
| 	if err != nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	_, ok := ext.(*options.HttpRule) | ||||
| 	hr, ok := ext.(*options.HttpRule) | ||||
| 	if !ok { | ||||
| 		panic(fmt.Sprintf("extension is %T; want an HttpRule", ext)) | ||||
| 	} | ||||
|  | ||||
| 	opt := &HttpOption{Method: httpVerb(m), Body: httpBody(m)} | ||||
| 	if path := httpPath(m); path != "" { | ||||
| 		opt.Path = append(opt.Path, path) | ||||
| 	var method string | ||||
| 	var path string | ||||
| 	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.Path = append(opt.Path, path) | ||||
| 	opt := &HttpOption{Method: method, Body: hr.Body, 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 | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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 := µ_api.Endpoint{ | ||||
|     {{- $httpOption := (httpOption .) }} | ||||
|     {{- if ne $httpOption.Method "" }} | ||||
|     endpoint = µ_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 = µ_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}} | ||||
|   | ||||
| @@ -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 }} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user