github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/examples/service/protocol/pb/user.http.pb.go (about) 1 // Code generated by protoc-gen-go-http. DO NOT EDIT. 2 // Source: user.proto 3 4 package pb 5 6 import ( 7 context "context" 8 http1 "net/http" 9 strconv "strconv" 10 strings "strings" 11 12 transport "github.com/erda-project/erda-infra/pkg/transport" 13 http "github.com/erda-project/erda-infra/pkg/transport/http" 14 httprule "github.com/erda-project/erda-infra/pkg/transport/http/httprule" 15 runtime "github.com/erda-project/erda-infra/pkg/transport/http/runtime" 16 urlenc "github.com/erda-project/erda-infra/pkg/urlenc" 17 ) 18 19 // This is a compile-time assertion to ensure that this generated file 20 // is compatible with the "github.com/erda-project/erda-infra/pkg/transport/http" package it is being compiled against. 21 const _ = http.SupportPackageIsVersion1 22 23 // UserServiceHandler is the server API for UserService service. 24 type UserServiceHandler interface { 25 // get user 26 // GET /api/user/{id} 27 GetUser(context.Context, *GetUserRequest) (*GetUserResponse, error) 28 // update user 29 // PUT /api/user/{user.id} 30 UpdateUser(context.Context, *UpdateUserRequest) (*UpdateUserResponse, error) 31 } 32 33 // RegisterUserServiceHandler register UserServiceHandler to http.Router. 34 func RegisterUserServiceHandler(r http.Router, srv UserServiceHandler, opts ...http.HandleOption) { 35 h := http.DefaultHandleOptions() 36 for _, op := range opts { 37 op(h) 38 } 39 encodeFunc := func(fn func(http1.ResponseWriter, *http1.Request) (interface{}, error)) http.HandlerFunc { 40 return func(w http1.ResponseWriter, r *http1.Request) { 41 out, err := fn(w, r) 42 if err != nil { 43 h.Error(w, r, err) 44 return 45 } 46 if err := h.Encode(w, r, out); err != nil { 47 h.Error(w, r, err) 48 } 49 } 50 } 51 52 add_GetUser := func(method, path string, fn func(context.Context, *GetUserRequest) (*GetUserResponse, error)) { 53 handler := func(ctx context.Context, req interface{}) (interface{}, error) { 54 return fn(ctx, req.(*GetUserRequest)) 55 } 56 var GetUser_info transport.ServiceInfo 57 if h.Interceptor != nil { 58 GetUser_info = transport.NewServiceInfo("erda.infra.example.UserService", "GetUser", srv) 59 handler = h.Interceptor(handler) 60 } 61 compiler, _ := httprule.Parse(path) 62 temp := compiler.Compile() 63 pattern, _ := runtime.NewPattern(httprule.SupportPackageIsVersion1, temp.OpCodes, temp.Pool, temp.Verb) 64 r.Add(method, path, encodeFunc( 65 func(w http1.ResponseWriter, r *http1.Request) (interface{}, error) { 66 var in GetUserRequest 67 if err := h.Decode(r, &in); err != nil { 68 return nil, err 69 } 70 var input interface{} = &in 71 if u, ok := (input).(urlenc.URLValuesUnmarshaler); ok { 72 if err := u.UnmarshalURLValues("", r.URL.Query()); err != nil { 73 return nil, err 74 } 75 } 76 path := r.URL.Path 77 if len(path) > 0 { 78 components := strings.Split(path[1:], "/") 79 last := len(components) - 1 80 var verb string 81 if idx := strings.LastIndex(components[last], ":"); idx >= 0 { 82 c := components[last] 83 components[last], verb = c[:idx], c[idx+1:] 84 } 85 vars, err := pattern.Match(components, verb) 86 if err != nil { 87 return nil, err 88 } 89 for k, val := range vars { 90 switch k { 91 case "id": 92 in.Id = val 93 } 94 } 95 } 96 ctx := http.WithRequest(r.Context(), r) 97 ctx = transport.WithHTTPHeaderForServer(ctx, r.Header) 98 if h.Interceptor != nil { 99 ctx = context.WithValue(ctx, transport.ServiceInfoContextKey, GetUser_info) 100 } 101 out, err := handler(ctx, &in) 102 if err != nil { 103 return out, err 104 } 105 return out, nil 106 }), 107 ) 108 } 109 110 add_UpdateUser := func(method, path string, fn func(context.Context, *UpdateUserRequest) (*UpdateUserResponse, error)) { 111 handler := func(ctx context.Context, req interface{}) (interface{}, error) { 112 return fn(ctx, req.(*UpdateUserRequest)) 113 } 114 var UpdateUser_info transport.ServiceInfo 115 if h.Interceptor != nil { 116 UpdateUser_info = transport.NewServiceInfo("erda.infra.example.UserService", "UpdateUser", srv) 117 handler = h.Interceptor(handler) 118 } 119 compiler, _ := httprule.Parse(path) 120 temp := compiler.Compile() 121 pattern, _ := runtime.NewPattern(httprule.SupportPackageIsVersion1, temp.OpCodes, temp.Pool, temp.Verb) 122 r.Add(method, path, encodeFunc( 123 func(w http1.ResponseWriter, r *http1.Request) (interface{}, error) { 124 var in UpdateUserRequest 125 if err := h.Decode(r, &in); err != nil { 126 return nil, err 127 } 128 var input interface{} = &in 129 if u, ok := (input).(urlenc.URLValuesUnmarshaler); ok { 130 if err := u.UnmarshalURLValues("", r.URL.Query()); err != nil { 131 return nil, err 132 } 133 } 134 path := r.URL.Path 135 if len(path) > 0 { 136 components := strings.Split(path[1:], "/") 137 last := len(components) - 1 138 var verb string 139 if idx := strings.LastIndex(components[last], ":"); idx >= 0 { 140 c := components[last] 141 components[last], verb = c[:idx], c[idx+1:] 142 } 143 vars, err := pattern.Match(components, verb) 144 if err != nil { 145 return nil, err 146 } 147 for k, val := range vars { 148 switch k { 149 case "user.id": 150 if in.User == nil { 151 in.User = &User{} 152 } 153 val, err := strconv.ParseInt(val, 10, 64) 154 if err != nil { 155 return nil, err 156 } 157 in.User.Id = val 158 } 159 } 160 } 161 ctx := http.WithRequest(r.Context(), r) 162 ctx = transport.WithHTTPHeaderForServer(ctx, r.Header) 163 if h.Interceptor != nil { 164 ctx = context.WithValue(ctx, transport.ServiceInfoContextKey, UpdateUser_info) 165 } 166 out, err := handler(ctx, &in) 167 if err != nil { 168 return out, err 169 } 170 return out, nil 171 }), 172 ) 173 } 174 175 add_GetUser("GET", "/api/user/{id}", srv.GetUser) 176 add_UpdateUser("PUT", "/api/user/{user.id}", srv.UpdateUser) 177 }