github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/examples/service/protocol/pb/greeter.http.pb.go (about)

     1  // Code generated by protoc-gen-go-http. DO NOT EDIT.
     2  // Source: greeter.proto
     3  
     4  package pb
     5  
     6  import (
     7  	context "context"
     8  	http1 "net/http"
     9  	strings "strings"
    10  
    11  	transport "github.com/erda-project/erda-infra/pkg/transport"
    12  	http "github.com/erda-project/erda-infra/pkg/transport/http"
    13  	httprule "github.com/erda-project/erda-infra/pkg/transport/http/httprule"
    14  	runtime "github.com/erda-project/erda-infra/pkg/transport/http/runtime"
    15  	urlenc "github.com/erda-project/erda-infra/pkg/urlenc"
    16  )
    17  
    18  // This is a compile-time assertion to ensure that this generated file
    19  // is compatible with the "github.com/erda-project/erda-infra/pkg/transport/http" package it is being compiled against.
    20  const _ = http.SupportPackageIsVersion1
    21  
    22  // GreeterServiceHandler is the server API for GreeterService service.
    23  type GreeterServiceHandler interface {
    24  	// say hello
    25  	// GET /api/greeter/{name}
    26  	SayHello(context.Context, *HelloRequest) (*HelloResponse, error)
    27  }
    28  
    29  // RegisterGreeterServiceHandler register GreeterServiceHandler to http.Router.
    30  func RegisterGreeterServiceHandler(r http.Router, srv GreeterServiceHandler, opts ...http.HandleOption) {
    31  	h := http.DefaultHandleOptions()
    32  	for _, op := range opts {
    33  		op(h)
    34  	}
    35  	encodeFunc := func(fn func(http1.ResponseWriter, *http1.Request) (interface{}, error)) http.HandlerFunc {
    36  		return func(w http1.ResponseWriter, r *http1.Request) {
    37  			out, err := fn(w, r)
    38  			if err != nil {
    39  				h.Error(w, r, err)
    40  				return
    41  			}
    42  			if err := h.Encode(w, r, out); err != nil {
    43  				h.Error(w, r, err)
    44  			}
    45  		}
    46  	}
    47  
    48  	add_SayHello := func(method, path string, fn func(context.Context, *HelloRequest) (*HelloResponse, error)) {
    49  		handler := func(ctx context.Context, req interface{}) (interface{}, error) {
    50  			return fn(ctx, req.(*HelloRequest))
    51  		}
    52  		var SayHello_info transport.ServiceInfo
    53  		if h.Interceptor != nil {
    54  			SayHello_info = transport.NewServiceInfo("erda.infra.example.GreeterService", "SayHello", srv)
    55  			handler = h.Interceptor(handler)
    56  		}
    57  		compiler, _ := httprule.Parse(path)
    58  		temp := compiler.Compile()
    59  		pattern, _ := runtime.NewPattern(httprule.SupportPackageIsVersion1, temp.OpCodes, temp.Pool, temp.Verb)
    60  		r.Add(method, path, encodeFunc(
    61  			func(w http1.ResponseWriter, r *http1.Request) (interface{}, error) {
    62  				var in HelloRequest
    63  				if err := h.Decode(r, &in); err != nil {
    64  					return nil, err
    65  				}
    66  				var input interface{} = &in
    67  				if u, ok := (input).(urlenc.URLValuesUnmarshaler); ok {
    68  					if err := u.UnmarshalURLValues("", r.URL.Query()); err != nil {
    69  						return nil, err
    70  					}
    71  				}
    72  				path := r.URL.Path
    73  				if len(path) > 0 {
    74  					components := strings.Split(path[1:], "/")
    75  					last := len(components) - 1
    76  					var verb string
    77  					if idx := strings.LastIndex(components[last], ":"); idx >= 0 {
    78  						c := components[last]
    79  						components[last], verb = c[:idx], c[idx+1:]
    80  					}
    81  					vars, err := pattern.Match(components, verb)
    82  					if err != nil {
    83  						return nil, err
    84  					}
    85  					for k, val := range vars {
    86  						switch k {
    87  						case "name":
    88  							in.Name = val
    89  						}
    90  					}
    91  				}
    92  				ctx := http.WithRequest(r.Context(), r)
    93  				ctx = transport.WithHTTPHeaderForServer(ctx, r.Header)
    94  				if h.Interceptor != nil {
    95  					ctx = context.WithValue(ctx, transport.ServiceInfoContextKey, SayHello_info)
    96  				}
    97  				out, err := handler(ctx, &in)
    98  				if err != nil {
    99  					return out, err
   100  				}
   101  				return out, nil
   102  			}),
   103  		)
   104  	}
   105  
   106  	add_SayHello("GET", "/api/greeter/{name}", srv.SayHello)
   107  }