split generated server handler to http and grpc

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-02 17:52:14 +03:00
parent 45a0f7b969
commit a0e6d3d995
6 changed files with 67 additions and 40 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,5 @@
// Code generated by protoc-gen-micro // Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}} package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import ( import (

View File

@ -1,4 +1,5 @@
// Code generated by protoc-gen-micro // Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}} package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
import ( import (

View File

@ -188,3 +188,27 @@ func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}},
{{- end}} {{- end}}
{{- end}} {{- end}}
// Register{{$ServiceName}}Handler registers server handler
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
type {{$ServiceName | lowerFirst}} interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{.Name}}(context.Context, micro_server.Stream) error
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
}
type {{$ServiceName}} struct {
{{$ServiceName | lowerFirst}}
}
h := &{{$ServiceName | lowerFirst}}Handler{sh}
for _, endpoint := range New{{$ServiceName}}Endpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
}

View File

@ -25,6 +25,7 @@ import (
micro_client "github.com/unistack-org/micro/v3/client" micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server" micro_server "github.com/unistack-org/micro/v3/server"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client_http "github.com/unistack-org/micro-client-http/v3" micro_client_http "github.com/unistack-org/micro-client-http/v3"
) )
@ -231,3 +232,27 @@ func (e *{{- $v }}) Error() string {
return fmt.Sprintf("%#v", e) return fmt.Sprintf("%#v", e)
} }
{{- end }} {{- end }}
// Register{{$ServiceName}}Handler registers server handler
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
type {{$ServiceName | lowerFirst}} interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{.Name}}(context.Context, micro_server.Stream) error
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
}
type {{$ServiceName}} struct {
{{$ServiceName | lowerFirst}}
}
h := &{{$ServiceName | lowerFirst}}Handler{sh}
for _, endpoint := range New{{$ServiceName}}Endpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
}

View File

@ -7,7 +7,6 @@ import (
micro_api "github.com/unistack-org/micro/v3/api" micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client" micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
) )
{{- $ServiceName := .Service.Name | trimSuffix "Service" }} {{- $ServiceName := .Service.Name | trimSuffix "Service" }}
@ -120,29 +119,6 @@ type {{$ServiceName}}Handler interface {
{{- end}} {{- end}}
} }
// Register{{$ServiceName}}Handler registers server handler
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
type {{$ServiceName | lowerFirst}} interface {
{{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}}
{{- if or (.ServerStreaming) (.ClientStreaming)}}
{{.Name}}(context.Context, micro_server.Stream) error
{{- else}}
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
{{- end}}
{{- end}}
}
type {{$ServiceName}} struct {
{{$ServiceName | lowerFirst}}
}
h := &{{$ServiceName | lowerFirst}}Handler{sh}
for _, endpoint := range New{{$ServiceName}}Endpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
}
{{- range .Service.Method}} {{- range .Service.Method}}
{{- $reqMethod := .InputType | splitArray "." | last}} {{- $reqMethod := .InputType | splitArray "." | last}}
{{- $rspMethod := .OutputType | splitArray "." | last}} {{- $rspMethod := .OutputType | splitArray "." | last}}