From 93adaba60a0b77dcf75382f1984fd30761708c7c Mon Sep 17 00:00:00 2001 From: longxboy Date: Tue, 7 Jun 2022 17:40:55 +0800 Subject: [PATCH] add http operation const (#2086) --- cmd/protoc-gen-go-http/template.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/protoc-gen-go-http/template.go b/cmd/protoc-gen-go-http/template.go index d2ffcd41d..54b9e4b28 100644 --- a/cmd/protoc-gen-go-http/template.go +++ b/cmd/protoc-gen-go-http/template.go @@ -9,6 +9,11 @@ import ( var httpTemplate = ` {{$svrType := .ServiceType}} {{$svrName := .ServiceName}} + +{{- range .Methods}} +const Operation{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}" +{{- end}} + type {{.ServiceType}}HTTPServer interface { {{- range .MethodSets}} {{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) @@ -46,7 +51,7 @@ func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) fu return err } {{- end}} - http.SetOperation(ctx,"/{{$svrName}}/{{.OriginalName}}") + http.SetOperation(ctx,Operation{{.OriginalName}}) h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { return srv.{{.Name}}(ctx, req.(*{{.Request}})) }) @@ -79,7 +84,7 @@ func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Reque var out {{.Reply}} pattern := "{{.Path}}" path := binding.EncodeURL(pattern, in, {{not .HasBody}}) - opts = append(opts, http.Operation("/{{$svrName}}/{{.OriginalName}}")) + opts = append(opts, http.Operation(Operation{{.OriginalName}})) opts = append(opts, http.PathTemplate(pattern)) {{if .HasBody -}} err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...)