File
Blob: spec/protocol/config.twirp.go
| 1 | // Code generated by protoc-gen-twirp v8.1.3, DO NOT EDIT. |
| 2 | // source: spec/proto/config.proto |
| 3 | |
| 4 | package protocol |
| 5 | |
| 6 | import context "context" |
| 7 | import fmt "fmt" |
| 8 | import http "net/http" |
| 9 | import io "io" |
| 10 | import json "encoding/json" |
| 11 | import strconv "strconv" |
| 12 | import strings "strings" |
| 13 | |
| 14 | import protojson "google.golang.org/protobuf/encoding/protojson" |
| 15 | import twirp "github.com/twitchtv/twirp" |
| 16 | import ctxsetters "github.com/twitchtv/twirp/ctxsetters" |
| 17 | |
| 18 | // Version compatibility assertion. |
| 19 | // If the constant is not defined in the package, that likely means |
| 20 | // the package needs to be updated to work with this generated code. |
| 21 | // See https://twitchtv.github.io/twirp/docs/version_matrix.html |
| 22 | const _ = twirp.TwirpPackageMinVersion_8_1_0 |
| 23 | |
| 24 | // ======================= |
| 25 | // ConfigService Interface |
| 26 | // ======================= |
| 27 | |
| 28 | // Each physical server provides keyed reads of its local configuration replica. |
| 29 | // Snapshot exchange is exposed only on authenticated Chord CONFIG streams. |
| 30 | type ConfigService interface { |
| 31 | Exchange(context.Context, *ConfigSnapshot) (*ConfigSnapshot, error) |
| 32 | |
| 33 | Get(context.Context, *ConfigGetRequest) (*ConfigRecord, error) |
| 34 | } |
| 35 | |
| 36 | // ============================= |
| 37 | // ConfigService Protobuf Client |
| 38 | // ============================= |
| 39 | |
| 40 | type configServiceProtobufClient struct { |
| 41 | client HTTPClient |
| 42 | urls [2]string |
| 43 | interceptor twirp.Interceptor |
| 44 | opts twirp.ClientOptions |
| 45 | } |
| 46 | |
| 47 | // NewConfigServiceProtobufClient creates a Protobuf client that implements the ConfigService interface. |
| 48 | // It communicates using Protobuf and can be configured with a custom HTTPClient. |
| 49 | func NewConfigServiceProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) ConfigService { |
| 50 | if c, ok := client.(*http.Client); ok { |
| 51 | client = withoutRedirects(c) |
| 52 | } |
| 53 | |
| 54 | clientOpts := twirp.ClientOptions{} |
| 55 | for _, o := range opts { |
| 56 | o(&clientOpts) |
| 57 | } |
| 58 | |
| 59 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 60 | literalURLs := false |
| 61 | _ = clientOpts.ReadOpt("literalURLs", &literalURLs) |
| 62 | var pathPrefix string |
| 63 | if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 64 | pathPrefix = "/twirp" // default prefix |
| 65 | } |
| 66 | |
| 67 | // Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method> |
| 68 | serviceURL := sanitizeBaseURL(baseURL) |
| 69 | serviceURL += baseServicePath(pathPrefix, "protocol", "ConfigService") |
| 70 | urls := [2]string{ |
| 71 | serviceURL + "Exchange", |
| 72 | serviceURL + "Get", |
| 73 | } |
| 74 | |
| 75 | return &configServiceProtobufClient{ |
| 76 | client: client, |
| 77 | urls: urls, |
| 78 | interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), |
| 79 | opts: clientOpts, |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (c *configServiceProtobufClient) Exchange(ctx context.Context, in *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 84 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 85 | ctx = ctxsetters.WithServiceName(ctx, "ConfigService") |
| 86 | ctx = ctxsetters.WithMethodName(ctx, "Exchange") |
| 87 | caller := c.callExchange |
| 88 | if c.interceptor != nil { |
| 89 | caller = func(ctx context.Context, req *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 90 | resp, err := c.interceptor( |
| 91 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 92 | typedReq, ok := req.(*ConfigSnapshot) |
| 93 | if !ok { |
| 94 | return nil, twirp.InternalError("failed type assertion req.(*ConfigSnapshot) when calling interceptor") |
| 95 | } |
| 96 | return c.callExchange(ctx, typedReq) |
| 97 | }, |
| 98 | )(ctx, req) |
| 99 | if resp != nil { |
| 100 | typedResp, ok := resp.(*ConfigSnapshot) |
| 101 | if !ok { |
| 102 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigSnapshot) when calling interceptor") |
| 103 | } |
| 104 | return typedResp, err |
| 105 | } |
| 106 | return nil, err |
| 107 | } |
| 108 | } |
| 109 | return caller(ctx, in) |
| 110 | } |
| 111 | |
| 112 | func (c *configServiceProtobufClient) callExchange(ctx context.Context, in *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 113 | out := new(ConfigSnapshot) |
| 114 | ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) |
| 115 | if err != nil { |
| 116 | twerr, ok := err.(twirp.Error) |
| 117 | if !ok { |
| 118 | twerr = twirp.InternalErrorWith(err) |
| 119 | } |
| 120 | callClientError(ctx, c.opts.Hooks, twerr) |
| 121 | return nil, err |
| 122 | } |
| 123 | |
| 124 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 125 | |
| 126 | return out, nil |
| 127 | } |
| 128 | |
| 129 | func (c *configServiceProtobufClient) Get(ctx context.Context, in *ConfigGetRequest) (*ConfigRecord, error) { |
| 130 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 131 | ctx = ctxsetters.WithServiceName(ctx, "ConfigService") |
| 132 | ctx = ctxsetters.WithMethodName(ctx, "Get") |
| 133 | caller := c.callGet |
| 134 | if c.interceptor != nil { |
| 135 | caller = func(ctx context.Context, req *ConfigGetRequest) (*ConfigRecord, error) { |
| 136 | resp, err := c.interceptor( |
| 137 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 138 | typedReq, ok := req.(*ConfigGetRequest) |
| 139 | if !ok { |
| 140 | return nil, twirp.InternalError("failed type assertion req.(*ConfigGetRequest) when calling interceptor") |
| 141 | } |
| 142 | return c.callGet(ctx, typedReq) |
| 143 | }, |
| 144 | )(ctx, req) |
| 145 | if resp != nil { |
| 146 | typedResp, ok := resp.(*ConfigRecord) |
| 147 | if !ok { |
| 148 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigRecord) when calling interceptor") |
| 149 | } |
| 150 | return typedResp, err |
| 151 | } |
| 152 | return nil, err |
| 153 | } |
| 154 | } |
| 155 | return caller(ctx, in) |
| 156 | } |
| 157 | |
| 158 | func (c *configServiceProtobufClient) callGet(ctx context.Context, in *ConfigGetRequest) (*ConfigRecord, error) { |
| 159 | out := new(ConfigRecord) |
| 160 | ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[1], in, out) |
| 161 | if err != nil { |
| 162 | twerr, ok := err.(twirp.Error) |
| 163 | if !ok { |
| 164 | twerr = twirp.InternalErrorWith(err) |
| 165 | } |
| 166 | callClientError(ctx, c.opts.Hooks, twerr) |
| 167 | return nil, err |
| 168 | } |
| 169 | |
| 170 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 171 | |
| 172 | return out, nil |
| 173 | } |
| 174 | |
| 175 | // ========================= |
| 176 | // ConfigService JSON Client |
| 177 | // ========================= |
| 178 | |
| 179 | type configServiceJSONClient struct { |
| 180 | client HTTPClient |
| 181 | urls [2]string |
| 182 | interceptor twirp.Interceptor |
| 183 | opts twirp.ClientOptions |
| 184 | } |
| 185 | |
| 186 | // NewConfigServiceJSONClient creates a JSON client that implements the ConfigService interface. |
| 187 | // It communicates using JSON and can be configured with a custom HTTPClient. |
| 188 | func NewConfigServiceJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) ConfigService { |
| 189 | if c, ok := client.(*http.Client); ok { |
| 190 | client = withoutRedirects(c) |
| 191 | } |
| 192 | |
| 193 | clientOpts := twirp.ClientOptions{} |
| 194 | for _, o := range opts { |
| 195 | o(&clientOpts) |
| 196 | } |
| 197 | |
| 198 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 199 | literalURLs := false |
| 200 | _ = clientOpts.ReadOpt("literalURLs", &literalURLs) |
| 201 | var pathPrefix string |
| 202 | if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 203 | pathPrefix = "/twirp" // default prefix |
| 204 | } |
| 205 | |
| 206 | // Build method URLs: <baseURL>[<prefix>]/<package>.<Service>/<Method> |
| 207 | serviceURL := sanitizeBaseURL(baseURL) |
| 208 | serviceURL += baseServicePath(pathPrefix, "protocol", "ConfigService") |
| 209 | urls := [2]string{ |
| 210 | serviceURL + "Exchange", |
| 211 | serviceURL + "Get", |
| 212 | } |
| 213 | |
| 214 | return &configServiceJSONClient{ |
| 215 | client: client, |
| 216 | urls: urls, |
| 217 | interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), |
| 218 | opts: clientOpts, |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func (c *configServiceJSONClient) Exchange(ctx context.Context, in *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 223 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 224 | ctx = ctxsetters.WithServiceName(ctx, "ConfigService") |
| 225 | ctx = ctxsetters.WithMethodName(ctx, "Exchange") |
| 226 | caller := c.callExchange |
| 227 | if c.interceptor != nil { |
| 228 | caller = func(ctx context.Context, req *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 229 | resp, err := c.interceptor( |
| 230 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 231 | typedReq, ok := req.(*ConfigSnapshot) |
| 232 | if !ok { |
| 233 | return nil, twirp.InternalError("failed type assertion req.(*ConfigSnapshot) when calling interceptor") |
| 234 | } |
| 235 | return c.callExchange(ctx, typedReq) |
| 236 | }, |
| 237 | )(ctx, req) |
| 238 | if resp != nil { |
| 239 | typedResp, ok := resp.(*ConfigSnapshot) |
| 240 | if !ok { |
| 241 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigSnapshot) when calling interceptor") |
| 242 | } |
| 243 | return typedResp, err |
| 244 | } |
| 245 | return nil, err |
| 246 | } |
| 247 | } |
| 248 | return caller(ctx, in) |
| 249 | } |
| 250 | |
| 251 | func (c *configServiceJSONClient) callExchange(ctx context.Context, in *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 252 | out := new(ConfigSnapshot) |
| 253 | ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) |
| 254 | if err != nil { |
| 255 | twerr, ok := err.(twirp.Error) |
| 256 | if !ok { |
| 257 | twerr = twirp.InternalErrorWith(err) |
| 258 | } |
| 259 | callClientError(ctx, c.opts.Hooks, twerr) |
| 260 | return nil, err |
| 261 | } |
| 262 | |
| 263 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 264 | |
| 265 | return out, nil |
| 266 | } |
| 267 | |
| 268 | func (c *configServiceJSONClient) Get(ctx context.Context, in *ConfigGetRequest) (*ConfigRecord, error) { |
| 269 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 270 | ctx = ctxsetters.WithServiceName(ctx, "ConfigService") |
| 271 | ctx = ctxsetters.WithMethodName(ctx, "Get") |
| 272 | caller := c.callGet |
| 273 | if c.interceptor != nil { |
| 274 | caller = func(ctx context.Context, req *ConfigGetRequest) (*ConfigRecord, error) { |
| 275 | resp, err := c.interceptor( |
| 276 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 277 | typedReq, ok := req.(*ConfigGetRequest) |
| 278 | if !ok { |
| 279 | return nil, twirp.InternalError("failed type assertion req.(*ConfigGetRequest) when calling interceptor") |
| 280 | } |
| 281 | return c.callGet(ctx, typedReq) |
| 282 | }, |
| 283 | )(ctx, req) |
| 284 | if resp != nil { |
| 285 | typedResp, ok := resp.(*ConfigRecord) |
| 286 | if !ok { |
| 287 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigRecord) when calling interceptor") |
| 288 | } |
| 289 | return typedResp, err |
| 290 | } |
| 291 | return nil, err |
| 292 | } |
| 293 | } |
| 294 | return caller(ctx, in) |
| 295 | } |
| 296 | |
| 297 | func (c *configServiceJSONClient) callGet(ctx context.Context, in *ConfigGetRequest) (*ConfigRecord, error) { |
| 298 | out := new(ConfigRecord) |
| 299 | ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[1], in, out) |
| 300 | if err != nil { |
| 301 | twerr, ok := err.(twirp.Error) |
| 302 | if !ok { |
| 303 | twerr = twirp.InternalErrorWith(err) |
| 304 | } |
| 305 | callClientError(ctx, c.opts.Hooks, twerr) |
| 306 | return nil, err |
| 307 | } |
| 308 | |
| 309 | callClientResponseReceived(ctx, c.opts.Hooks) |
| 310 | |
| 311 | return out, nil |
| 312 | } |
| 313 | |
| 314 | // ============================ |
| 315 | // ConfigService Server Handler |
| 316 | // ============================ |
| 317 | |
| 318 | type configServiceServer struct { |
| 319 | ConfigService |
| 320 | interceptor twirp.Interceptor |
| 321 | hooks *twirp.ServerHooks |
| 322 | pathPrefix string // prefix for routing |
| 323 | jsonSkipDefaults bool // do not include unpopulated fields (default values) in the response |
| 324 | jsonCamelCase bool // JSON fields are serialized as lowerCamelCase rather than keeping the original proto names |
| 325 | } |
| 326 | |
| 327 | // NewConfigServiceServer builds a TwirpServer that can be used as an http.Handler to handle |
| 328 | // HTTP requests that are routed to the right method in the provided svc implementation. |
| 329 | // The opts are twirp.ServerOption modifiers, for example twirp.WithServerHooks(hooks). |
| 330 | func NewConfigServiceServer(svc ConfigService, opts ...interface{}) TwirpServer { |
| 331 | serverOpts := newServerOpts(opts) |
| 332 | |
| 333 | // Using ReadOpt allows backwards and forwards compatibility with new options in the future |
| 334 | jsonSkipDefaults := false |
| 335 | _ = serverOpts.ReadOpt("jsonSkipDefaults", &jsonSkipDefaults) |
| 336 | jsonCamelCase := false |
| 337 | _ = serverOpts.ReadOpt("jsonCamelCase", &jsonCamelCase) |
| 338 | var pathPrefix string |
| 339 | if ok := serverOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { |
| 340 | pathPrefix = "/twirp" // default prefix |
| 341 | } |
| 342 | |
| 343 | return &configServiceServer{ |
| 344 | ConfigService: svc, |
| 345 | hooks: serverOpts.Hooks, |
| 346 | interceptor: twirp.ChainInterceptors(serverOpts.Interceptors...), |
| 347 | pathPrefix: pathPrefix, |
| 348 | jsonSkipDefaults: jsonSkipDefaults, |
| 349 | jsonCamelCase: jsonCamelCase, |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // writeError writes an HTTP response with a valid Twirp error format, and triggers hooks. |
| 354 | // If err is not a twirp.Error, it will get wrapped with twirp.InternalErrorWith(err) |
| 355 | func (s *configServiceServer) writeError(ctx context.Context, resp http.ResponseWriter, err error) { |
| 356 | writeError(ctx, resp, err, s.hooks) |
| 357 | } |
| 358 | |
| 359 | // handleRequestBodyError is used to handle error when the twirp server cannot read request |
| 360 | func (s *configServiceServer) handleRequestBodyError(ctx context.Context, resp http.ResponseWriter, msg string, err error) { |
| 361 | if context.Canceled == ctx.Err() { |
| 362 | s.writeError(ctx, resp, twirp.NewError(twirp.Canceled, "failed to read request: context canceled")) |
| 363 | return |
| 364 | } |
| 365 | if context.DeadlineExceeded == ctx.Err() { |
| 366 | s.writeError(ctx, resp, twirp.NewError(twirp.DeadlineExceeded, "failed to read request: deadline exceeded")) |
| 367 | return |
| 368 | } |
| 369 | s.writeError(ctx, resp, twirp.WrapError(malformedRequestError(msg), err)) |
| 370 | } |
| 371 | |
| 372 | // ConfigServicePathPrefix is a convenience constant that may identify URL paths. |
| 373 | // Should be used with caution, it only matches routes generated by Twirp Go clients, |
| 374 | // with the default "/twirp" prefix and default CamelCase service and method names. |
| 375 | // More info: https://twitchtv.github.io/twirp/docs/routing.html |
| 376 | const ConfigServicePathPrefix = "/twirp/protocol.ConfigService/" |
| 377 | |
| 378 | func (s *configServiceServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { |
| 379 | ctx := req.Context() |
| 380 | ctx = ctxsetters.WithPackageName(ctx, "protocol") |
| 381 | ctx = ctxsetters.WithServiceName(ctx, "ConfigService") |
| 382 | ctx = ctxsetters.WithResponseWriter(ctx, resp) |
| 383 | |
| 384 | var err error |
| 385 | ctx, err = callRequestReceived(ctx, s.hooks) |
| 386 | if err != nil { |
| 387 | s.writeError(ctx, resp, err) |
| 388 | return |
| 389 | } |
| 390 | |
| 391 | if req.Method != "POST" { |
| 392 | msg := fmt.Sprintf("unsupported method %q (only POST is allowed)", req.Method) |
| 393 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 394 | return |
| 395 | } |
| 396 | |
| 397 | // Verify path format: [<prefix>]/<package>.<Service>/<Method> |
| 398 | prefix, pkgService, method := parseTwirpPath(req.URL.Path) |
| 399 | if pkgService != "protocol.ConfigService" { |
| 400 | msg := fmt.Sprintf("no handler for path %q", req.URL.Path) |
| 401 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 402 | return |
| 403 | } |
| 404 | if prefix != s.pathPrefix { |
| 405 | msg := fmt.Sprintf("invalid path prefix %q, expected %q, on path %q", prefix, s.pathPrefix, req.URL.Path) |
| 406 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 407 | return |
| 408 | } |
| 409 | |
| 410 | switch method { |
| 411 | case "Exchange": |
| 412 | s.serveExchange(ctx, resp, req) |
| 413 | return |
| 414 | case "Get": |
| 415 | s.serveGet(ctx, resp, req) |
| 416 | return |
| 417 | default: |
| 418 | msg := fmt.Sprintf("no handler for path %q", req.URL.Path) |
| 419 | s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) |
| 420 | return |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | func (s *configServiceServer) serveExchange(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 425 | header := req.Header.Get("Content-Type") |
| 426 | i := strings.Index(header, ";") |
| 427 | if i == -1 { |
| 428 | i = len(header) |
| 429 | } |
| 430 | switch strings.TrimSpace(strings.ToLower(header[:i])) { |
| 431 | case "application/json": |
| 432 | s.serveExchangeJSON(ctx, resp, req) |
| 433 | case "application/protobuf": |
| 434 | s.serveExchangeProtobuf(ctx, resp, req) |
| 435 | default: |
| 436 | msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) |
| 437 | twerr := badRouteError(msg, req.Method, req.URL.Path) |
| 438 | s.writeError(ctx, resp, twerr) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | func (s *configServiceServer) serveExchangeJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 443 | var err error |
| 444 | ctx = ctxsetters.WithMethodName(ctx, "Exchange") |
| 445 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 446 | if err != nil { |
| 447 | s.writeError(ctx, resp, err) |
| 448 | return |
| 449 | } |
| 450 | |
| 451 | d := json.NewDecoder(req.Body) |
| 452 | rawReqBody := json.RawMessage{} |
| 453 | if err := d.Decode(&rawReqBody); err != nil { |
| 454 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 455 | return |
| 456 | } |
| 457 | reqContent := new(ConfigSnapshot) |
| 458 | unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} |
| 459 | if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { |
| 460 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 461 | return |
| 462 | } |
| 463 | |
| 464 | handler := s.ConfigService.Exchange |
| 465 | if s.interceptor != nil { |
| 466 | handler = func(ctx context.Context, req *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 467 | resp, err := s.interceptor( |
| 468 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 469 | typedReq, ok := req.(*ConfigSnapshot) |
| 470 | if !ok { |
| 471 | return nil, twirp.InternalError("failed type assertion req.(*ConfigSnapshot) when calling interceptor") |
| 472 | } |
| 473 | return s.ConfigService.Exchange(ctx, typedReq) |
| 474 | }, |
| 475 | )(ctx, req) |
| 476 | if resp != nil { |
| 477 | typedResp, ok := resp.(*ConfigSnapshot) |
| 478 | if !ok { |
| 479 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigSnapshot) when calling interceptor") |
| 480 | } |
| 481 | return typedResp, err |
| 482 | } |
| 483 | return nil, err |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Call service method |
| 488 | var respContent *ConfigSnapshot |
| 489 | func() { |
| 490 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 491 | respContent, err = handler(ctx, reqContent) |
| 492 | }() |
| 493 | |
| 494 | if err != nil { |
| 495 | s.writeError(ctx, resp, err) |
| 496 | return |
| 497 | } |
| 498 | if respContent == nil { |
| 499 | s.writeError(ctx, resp, twirp.InternalError("received a nil *ConfigSnapshot and nil error while calling Exchange. nil responses are not supported")) |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | ctx = callResponsePrepared(ctx, s.hooks) |
| 504 | |
| 505 | marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} |
| 506 | respBytes, err := marshaler.Marshal(respContent) |
| 507 | if err != nil { |
| 508 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) |
| 509 | return |
| 510 | } |
| 511 | |
| 512 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 513 | resp.Header().Set("Content-Type", "application/json") |
| 514 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 515 | resp.WriteHeader(http.StatusOK) |
| 516 | |
| 517 | if n, err := resp.Write(respBytes); err != nil { |
| 518 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 519 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 520 | ctx = callError(ctx, s.hooks, twerr) |
| 521 | } |
| 522 | callResponseSent(ctx, s.hooks) |
| 523 | } |
| 524 | |
| 525 | func (s *configServiceServer) serveExchangeProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 526 | var err error |
| 527 | ctx = ctxsetters.WithMethodName(ctx, "Exchange") |
| 528 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 529 | if err != nil { |
| 530 | s.writeError(ctx, resp, err) |
| 531 | return |
| 532 | } |
| 533 | |
| 534 | buf, err := io.ReadAll(req.Body) |
| 535 | if err != nil { |
| 536 | s.handleRequestBodyError(ctx, resp, "failed to read request body", err) |
| 537 | return |
| 538 | } |
| 539 | reqContent := new(ConfigSnapshot) |
| 540 | if err = reqContent.UnmarshalVT(buf); err != nil { |
| 541 | s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) |
| 542 | return |
| 543 | } |
| 544 | |
| 545 | handler := s.ConfigService.Exchange |
| 546 | if s.interceptor != nil { |
| 547 | handler = func(ctx context.Context, req *ConfigSnapshot) (*ConfigSnapshot, error) { |
| 548 | resp, err := s.interceptor( |
| 549 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 550 | typedReq, ok := req.(*ConfigSnapshot) |
| 551 | if !ok { |
| 552 | return nil, twirp.InternalError("failed type assertion req.(*ConfigSnapshot) when calling interceptor") |
| 553 | } |
| 554 | return s.ConfigService.Exchange(ctx, typedReq) |
| 555 | }, |
| 556 | )(ctx, req) |
| 557 | if resp != nil { |
| 558 | typedResp, ok := resp.(*ConfigSnapshot) |
| 559 | if !ok { |
| 560 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigSnapshot) when calling interceptor") |
| 561 | } |
| 562 | return typedResp, err |
| 563 | } |
| 564 | return nil, err |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | // Call service method |
| 569 | var respContent *ConfigSnapshot |
| 570 | func() { |
| 571 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 572 | respContent, err = handler(ctx, reqContent) |
| 573 | }() |
| 574 | |
| 575 | if err != nil { |
| 576 | s.writeError(ctx, resp, err) |
| 577 | return |
| 578 | } |
| 579 | if respContent == nil { |
| 580 | s.writeError(ctx, resp, twirp.InternalError("received a nil *ConfigSnapshot and nil error while calling Exchange. nil responses are not supported")) |
| 581 | return |
| 582 | } |
| 583 | |
| 584 | ctx = callResponsePrepared(ctx, s.hooks) |
| 585 | |
| 586 | respBytes, err := respContent.MarshalVT() |
| 587 | if err != nil { |
| 588 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) |
| 589 | return |
| 590 | } |
| 591 | |
| 592 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 593 | resp.Header().Set("Content-Type", "application/protobuf") |
| 594 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 595 | resp.WriteHeader(http.StatusOK) |
| 596 | if n, err := resp.Write(respBytes); err != nil { |
| 597 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 598 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 599 | ctx = callError(ctx, s.hooks, twerr) |
| 600 | } |
| 601 | callResponseSent(ctx, s.hooks) |
| 602 | } |
| 603 | |
| 604 | func (s *configServiceServer) serveGet(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 605 | header := req.Header.Get("Content-Type") |
| 606 | i := strings.Index(header, ";") |
| 607 | if i == -1 { |
| 608 | i = len(header) |
| 609 | } |
| 610 | switch strings.TrimSpace(strings.ToLower(header[:i])) { |
| 611 | case "application/json": |
| 612 | s.serveGetJSON(ctx, resp, req) |
| 613 | case "application/protobuf": |
| 614 | s.serveGetProtobuf(ctx, resp, req) |
| 615 | default: |
| 616 | msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) |
| 617 | twerr := badRouteError(msg, req.Method, req.URL.Path) |
| 618 | s.writeError(ctx, resp, twerr) |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | func (s *configServiceServer) serveGetJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 623 | var err error |
| 624 | ctx = ctxsetters.WithMethodName(ctx, "Get") |
| 625 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 626 | if err != nil { |
| 627 | s.writeError(ctx, resp, err) |
| 628 | return |
| 629 | } |
| 630 | |
| 631 | d := json.NewDecoder(req.Body) |
| 632 | rawReqBody := json.RawMessage{} |
| 633 | if err := d.Decode(&rawReqBody); err != nil { |
| 634 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 635 | return |
| 636 | } |
| 637 | reqContent := new(ConfigGetRequest) |
| 638 | unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} |
| 639 | if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { |
| 640 | s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) |
| 641 | return |
| 642 | } |
| 643 | |
| 644 | handler := s.ConfigService.Get |
| 645 | if s.interceptor != nil { |
| 646 | handler = func(ctx context.Context, req *ConfigGetRequest) (*ConfigRecord, error) { |
| 647 | resp, err := s.interceptor( |
| 648 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 649 | typedReq, ok := req.(*ConfigGetRequest) |
| 650 | if !ok { |
| 651 | return nil, twirp.InternalError("failed type assertion req.(*ConfigGetRequest) when calling interceptor") |
| 652 | } |
| 653 | return s.ConfigService.Get(ctx, typedReq) |
| 654 | }, |
| 655 | )(ctx, req) |
| 656 | if resp != nil { |
| 657 | typedResp, ok := resp.(*ConfigRecord) |
| 658 | if !ok { |
| 659 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigRecord) when calling interceptor") |
| 660 | } |
| 661 | return typedResp, err |
| 662 | } |
| 663 | return nil, err |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Call service method |
| 668 | var respContent *ConfigRecord |
| 669 | func() { |
| 670 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 671 | respContent, err = handler(ctx, reqContent) |
| 672 | }() |
| 673 | |
| 674 | if err != nil { |
| 675 | s.writeError(ctx, resp, err) |
| 676 | return |
| 677 | } |
| 678 | if respContent == nil { |
| 679 | s.writeError(ctx, resp, twirp.InternalError("received a nil *ConfigRecord and nil error while calling Get. nil responses are not supported")) |
| 680 | return |
| 681 | } |
| 682 | |
| 683 | ctx = callResponsePrepared(ctx, s.hooks) |
| 684 | |
| 685 | marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} |
| 686 | respBytes, err := marshaler.Marshal(respContent) |
| 687 | if err != nil { |
| 688 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) |
| 689 | return |
| 690 | } |
| 691 | |
| 692 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 693 | resp.Header().Set("Content-Type", "application/json") |
| 694 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 695 | resp.WriteHeader(http.StatusOK) |
| 696 | |
| 697 | if n, err := resp.Write(respBytes); err != nil { |
| 698 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 699 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 700 | ctx = callError(ctx, s.hooks, twerr) |
| 701 | } |
| 702 | callResponseSent(ctx, s.hooks) |
| 703 | } |
| 704 | |
| 705 | func (s *configServiceServer) serveGetProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { |
| 706 | var err error |
| 707 | ctx = ctxsetters.WithMethodName(ctx, "Get") |
| 708 | ctx, err = callRequestRouted(ctx, s.hooks) |
| 709 | if err != nil { |
| 710 | s.writeError(ctx, resp, err) |
| 711 | return |
| 712 | } |
| 713 | |
| 714 | buf, err := io.ReadAll(req.Body) |
| 715 | if err != nil { |
| 716 | s.handleRequestBodyError(ctx, resp, "failed to read request body", err) |
| 717 | return |
| 718 | } |
| 719 | reqContent := new(ConfigGetRequest) |
| 720 | if err = reqContent.UnmarshalVT(buf); err != nil { |
| 721 | s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) |
| 722 | return |
| 723 | } |
| 724 | |
| 725 | handler := s.ConfigService.Get |
| 726 | if s.interceptor != nil { |
| 727 | handler = func(ctx context.Context, req *ConfigGetRequest) (*ConfigRecord, error) { |
| 728 | resp, err := s.interceptor( |
| 729 | func(ctx context.Context, req interface{}) (interface{}, error) { |
| 730 | typedReq, ok := req.(*ConfigGetRequest) |
| 731 | if !ok { |
| 732 | return nil, twirp.InternalError("failed type assertion req.(*ConfigGetRequest) when calling interceptor") |
| 733 | } |
| 734 | return s.ConfigService.Get(ctx, typedReq) |
| 735 | }, |
| 736 | )(ctx, req) |
| 737 | if resp != nil { |
| 738 | typedResp, ok := resp.(*ConfigRecord) |
| 739 | if !ok { |
| 740 | return nil, twirp.InternalError("failed type assertion resp.(*ConfigRecord) when calling interceptor") |
| 741 | } |
| 742 | return typedResp, err |
| 743 | } |
| 744 | return nil, err |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Call service method |
| 749 | var respContent *ConfigRecord |
| 750 | func() { |
| 751 | defer ensurePanicResponses(ctx, resp, s.hooks) |
| 752 | respContent, err = handler(ctx, reqContent) |
| 753 | }() |
| 754 | |
| 755 | if err != nil { |
| 756 | s.writeError(ctx, resp, err) |
| 757 | return |
| 758 | } |
| 759 | if respContent == nil { |
| 760 | s.writeError(ctx, resp, twirp.InternalError("received a nil *ConfigRecord and nil error while calling Get. nil responses are not supported")) |
| 761 | return |
| 762 | } |
| 763 | |
| 764 | ctx = callResponsePrepared(ctx, s.hooks) |
| 765 | |
| 766 | respBytes, err := respContent.MarshalVT() |
| 767 | if err != nil { |
| 768 | s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) |
| 769 | return |
| 770 | } |
| 771 | |
| 772 | ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) |
| 773 | resp.Header().Set("Content-Type", "application/protobuf") |
| 774 | resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) |
| 775 | resp.WriteHeader(http.StatusOK) |
| 776 | if n, err := resp.Write(respBytes); err != nil { |
| 777 | msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) |
| 778 | twerr := twirp.NewError(twirp.Unknown, msg) |
| 779 | ctx = callError(ctx, s.hooks, twerr) |
| 780 | } |
| 781 | callResponseSent(ctx, s.hooks) |
| 782 | } |
| 783 | |
| 784 | func (s *configServiceServer) ServiceDescriptor() ([]byte, int) { |
| 785 | return twirpFileDescriptor2, 0 |
| 786 | } |
| 787 | |
| 788 | func (s *configServiceServer) ProtocGenTwirpVersion() string { |
| 789 | return "v8.1.3" |
| 790 | } |
| 791 | |
| 792 | // PathPrefix returns the base service path, in the form: "/<prefix>/<package>.<Service>/" |
| 793 | // that is everything in a Twirp route except for the <Method>. This can be used for routing, |
| 794 | // for example to identify the requests that are targeted to this service in a mux. |
| 795 | func (s *configServiceServer) PathPrefix() string { |
| 796 | return baseServicePath(s.pathPrefix, "protocol", "ConfigService") |
| 797 | } |
| 798 | |
| 799 | var twirpFileDescriptor2 = []byte{ |
| 800 | // 244 bytes of a gzipped FileDescriptorProto |
| 801 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x90, 0x4f, 0x4b, 0x03, 0x31, |
| 802 | 0x10, 0xc5, 0x59, 0xeb, 0x9f, 0x3a, 0x54, 0x91, 0x20, 0x1a, 0xf6, 0x24, 0x8b, 0xa8, 0x20, 0x64, |
| 803 | 0x41, 0x4f, 0x5e, 0x3c, 0x28, 0x52, 0xcf, 0xf1, 0xe6, 0x2d, 0x4e, 0xc7, 0xed, 0xc2, 0x36, 0xb3, |
| 804 | 0x26, 0xe9, 0xe2, 0x67, 0xf0, 0x53, 0x8b, 0xb3, 0x96, 0xe2, 0x62, 0x4f, 0x79, 0x2f, 0xef, 0x07, |
| 805 | 0xc9, 0x7b, 0x70, 0x1a, 0x5b, 0xc2, 0xb2, 0x0d, 0x9c, 0xb8, 0x44, 0xf6, 0xef, 0x75, 0x65, 0xc4, |
| 806 | 0xa8, 0xb1, 0x1c, 0xc8, 0x4d, 0x71, 0x0e, 0x87, 0x8f, 0x92, 0xbc, 0x78, 0xd7, 0xc6, 0x39, 0x27, |
| 807 | 0xa5, 0x60, 0x7b, 0xe6, 0x92, 0xd3, 0xd9, 0x59, 0x76, 0x35, 0xb1, 0xa2, 0x8b, 0x0b, 0x38, 0xea, |
| 808 | 0xa9, 0x29, 0x25, 0x4b, 0x1f, 0x4b, 0x8a, 0xc2, 0x79, 0xb7, 0x20, 0xe1, 0xf6, 0xad, 0xe8, 0xc2, |
| 809 | 0xc2, 0xa4, 0xe7, 0x2c, 0x21, 0x87, 0xd9, 0x7f, 0x8c, 0xd2, 0xb0, 0xd7, 0x51, 0x88, 0x35, 0x7b, |
| 810 | 0xbd, 0x25, 0x4f, 0xac, 0xac, 0x3a, 0x86, 0x9d, 0xce, 0x35, 0x4b, 0xd2, 0x23, 0xb9, 0xef, 0xcd, |
| 811 | 0xcd, 0x57, 0x06, 0x07, 0xbf, 0x5f, 0xa4, 0xd0, 0xd5, 0x48, 0xea, 0x1e, 0xc6, 0x4f, 0x9f, 0x38, |
| 812 | 0x77, 0xbe, 0x22, 0xa5, 0xcd, 0xaa, 0x8a, 0xf9, 0xdb, 0x23, 0xdf, 0x98, 0xa8, 0x3b, 0x18, 0x4d, |
| 813 | 0x29, 0xa9, 0x7c, 0x08, 0xac, 0xcb, 0xe5, 0x27, 0xc3, 0xac, 0x2f, 0xf4, 0x70, 0xfd, 0x9c, 0xbd, |
| 814 | 0x5e, 0x56, 0x6c, 0x16, 0x75, 0x70, 0x15, 0xc5, 0xd6, 0x21, 0x19, 0xe4, 0xf2, 0x67, 0xe7, 0x44, |
| 815 | 0xa1, 0x5c, 0xef, 0x8d, 0xdc, 0xbc, 0xed, 0x8a, 0xba, 0xfd, 0x1e, 0x00, 0xa2, 0xa1, 0xf6, 0x93, |
| 816 | 0x87, 0x01, 0x00, 0x00, |
| 817 | } |