From 547402bebe0b17837cfdfd2ccb9cef1e9236516c Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 24 May 2024 19:07:14 -0400 Subject: fix events API Signed-off-by: Xe Iaso --- cmd/xesite/main.go | 7 + internal/adminpb/internal.pb.go | 2 +- pb/external/mi.proto | 7 + pb/external/mi/mi.pb.go | 25 +- pb/external/mimi/announce/mimi-announce.pb.go | 2 +- pb/external/protofeed/protofeed.pb.go | 2 +- pb/openapi.json | 2 +- pb/xesite.pb.go | 27 +- pb/xesite.proto | 6 - pb/xesite.twirp.go | 539 +------------------------- 10 files changed, 64 insertions(+), 555 deletions(-) diff --git a/cmd/xesite/main.go b/cmd/xesite/main.go index b4b238c..4793f38 100644 --- a/cmd/xesite/main.go +++ b/cmd/xesite/main.go @@ -17,6 +17,7 @@ import ( "xeiaso.net/v4/internal" "xeiaso.net/v4/internal/lume" "xeiaso.net/v4/pb" + "xeiaso.net/v4/pb/external/mi" ) var ( @@ -88,6 +89,12 @@ func main() { fsrv := pb.NewFeedServer(&FeedServer{fs}, twirp.WithServerPathPrefix("/api")) mux.Handle(fsrv.PathPrefix(), fsrv) + es := mi.NewEventsServer( + mi.NewEventsProtobufClient(*miURL, http.DefaultClient), + twirp.WithServerPathPrefix("/api"), + ) + mux.Handle(es.PathPrefix(), es) + mux.HandleFunc("/blog.atom", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/blog.rss", http.StatusMovedPermanently) }) diff --git a/internal/adminpb/internal.pb.go b/internal/adminpb/internal.pb.go index 569a904..921d7dd 100644 --- a/internal/adminpb/internal.pb.go +++ b/internal/adminpb/internal.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.32.0 +// protoc-gen-go v1.34.1 // protoc v5.26.1 // source: internal.proto diff --git a/pb/external/mi.proto b/pb/external/mi.proto index 937a15c..b4bd844 100644 --- a/pb/external/mi.proto +++ b/pb/external/mi.proto @@ -4,6 +4,7 @@ syntax = "proto3"; package within.website.x.mi; option go_package = "xeiaso.net/v4/pb/external/mi"; +import "google/protobuf/empty.proto"; import "google/protobuf/timestamp.proto"; // Event is a single event that Xe will be attending. @@ -28,4 +29,10 @@ message Event { message EventFeed { // The events in the feed repeated Event events = 1; +} + +// Events lets users fetch the current feed of events that Xe will be attending. +service Events { + // Get fetches the current feed of upcoming events. + rpc Get(google.protobuf.Empty) returns (EventFeed); } \ No newline at end of file diff --git a/pb/external/mi/mi.pb.go b/pb/external/mi/mi.pb.go index 8d425d3..15e0b44 100644 --- a/pb/external/mi/mi.pb.go +++ b/pb/external/mi/mi.pb.go @@ -2,7 +2,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.32.0 +// protoc-gen-go v1.34.1 // protoc v5.26.1 // source: mi.proto @@ -11,7 +11,7 @@ package mi import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - _ "google.golang.org/protobuf/types/known/emptypb" + emptypb "google.golang.org/protobuf/types/known/emptypb" timestamppb "google.golang.org/protobuf/types/known/timestamppb" reflect "reflect" sync "sync" @@ -203,10 +203,14 @@ var file_mi_proto_rawDesc = []byte{ 0x09, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x46, 0x65, 0x65, 0x64, 0x12, 0x32, 0x0a, 0x06, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x77, 0x69, 0x74, 0x68, 0x69, 0x6e, 0x2e, 0x77, 0x65, 0x62, 0x73, 0x69, 0x74, 0x65, 0x2e, 0x78, 0x2e, 0x6d, 0x69, - 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x06, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x42, 0x1e, - 0x5a, 0x1c, 0x78, 0x65, 0x69, 0x61, 0x73, 0x6f, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x76, 0x34, 0x2f, - 0x70, 0x62, 0x2f, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x6d, 0x69, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x06, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x32, 0x47, + 0x0a, 0x06, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x12, 0x3d, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, + 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1e, 0x2e, 0x77, 0x69, 0x74, 0x68, 0x69, 0x6e, + 0x2e, 0x77, 0x65, 0x62, 0x73, 0x69, 0x74, 0x65, 0x2e, 0x78, 0x2e, 0x6d, 0x69, 0x2e, 0x45, 0x76, + 0x65, 0x6e, 0x74, 0x46, 0x65, 0x65, 0x64, 0x42, 0x1e, 0x5a, 0x1c, 0x78, 0x65, 0x69, 0x61, 0x73, + 0x6f, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x76, 0x34, 0x2f, 0x70, 0x62, 0x2f, 0x65, 0x78, 0x74, 0x65, + 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x6d, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -226,13 +230,16 @@ var file_mi_proto_goTypes = []interface{}{ (*Event)(nil), // 0: within.website.x.mi.Event (*EventFeed)(nil), // 1: within.website.x.mi.EventFeed (*timestamppb.Timestamp)(nil), // 2: google.protobuf.Timestamp + (*emptypb.Empty)(nil), // 3: google.protobuf.Empty } var file_mi_proto_depIdxs = []int32{ 2, // 0: within.website.x.mi.Event.start_date:type_name -> google.protobuf.Timestamp 2, // 1: within.website.x.mi.Event.end_date:type_name -> google.protobuf.Timestamp 0, // 2: within.website.x.mi.EventFeed.events:type_name -> within.website.x.mi.Event - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type + 3, // 3: within.website.x.mi.Events.Get:input_type -> google.protobuf.Empty + 1, // 4: within.website.x.mi.Events.Get:output_type -> within.website.x.mi.EventFeed + 4, // [4:5] is the sub-list for method output_type + 3, // [3:4] is the sub-list for method input_type 3, // [3:3] is the sub-list for extension type_name 3, // [3:3] is the sub-list for extension extendee 0, // [0:3] is the sub-list for field type_name @@ -277,7 +284,7 @@ func file_mi_proto_init() { NumEnums: 0, NumMessages: 2, NumExtensions: 0, - NumServices: 0, + NumServices: 1, }, GoTypes: file_mi_proto_goTypes, DependencyIndexes: file_mi_proto_depIdxs, diff --git a/pb/external/mimi/announce/mimi-announce.pb.go b/pb/external/mimi/announce/mimi-announce.pb.go index 5faa967..2eb35e2 100644 --- a/pb/external/mimi/announce/mimi-announce.pb.go +++ b/pb/external/mimi/announce/mimi-announce.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.32.0 +// protoc-gen-go v1.34.1 // protoc v5.26.1 // source: mimi-announce.proto diff --git a/pb/external/protofeed/protofeed.pb.go b/pb/external/protofeed/protofeed.pb.go index 11c27cc..8ac14ec 100644 --- a/pb/external/protofeed/protofeed.pb.go +++ b/pb/external/protofeed/protofeed.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.32.0 +// protoc-gen-go v1.34.1 // protoc v5.26.1 // source: protofeed.proto diff --git a/pb/openapi.json b/pb/openapi.json index 0adc9a8..b89d6fa 100644 --- a/pb/openapi.json +++ b/pb/openapi.json @@ -270,7 +270,7 @@ }, "openapi": "3.0.0", "paths": { - "/api/xeiaso.net.Events/Get": { + "/api/within.website.x.mi.Events/Get": { "post": { "description": "\nGet fetches the current feed of upcoming events.", "requestBody": { diff --git a/pb/xesite.pb.go b/pb/xesite.pb.go index 5cb9461..2ef012b 100644 --- a/pb/xesite.pb.go +++ b/pb/xesite.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.32.0 +// protoc-gen-go v1.34.1 // protoc v5.26.1 // source: xesite.proto @@ -13,7 +13,7 @@ import ( timestamppb "google.golang.org/protobuf/types/known/timestamppb" reflect "reflect" sync "sync" - mi "xeiaso.net/v4/pb/external/mi" + _ "xeiaso.net/v4/pb/external/mi" protofeed "xeiaso.net/v4/pb/external/protofeed" ) @@ -140,13 +140,9 @@ var file_xesite_proto_rawDesc = []byte{ 0x66, 0x6f, 0x32, 0x36, 0x0a, 0x04, 0x46, 0x65, 0x65, 0x64, 0x12, 0x2e, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x0f, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x46, 0x65, 0x65, 0x64, 0x32, 0x47, 0x0a, 0x06, 0x45, 0x76, - 0x65, 0x6e, 0x74, 0x73, 0x12, 0x3d, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x16, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x1a, 0x1e, 0x2e, 0x77, 0x69, 0x74, 0x68, 0x69, 0x6e, 0x2e, 0x77, 0x65, 0x62, - 0x73, 0x69, 0x74, 0x65, 0x2e, 0x78, 0x2e, 0x6d, 0x69, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x46, - 0x65, 0x65, 0x64, 0x42, 0x12, 0x5a, 0x10, 0x78, 0x65, 0x69, 0x61, 0x73, 0x6f, 0x2e, 0x6e, 0x65, - 0x74, 0x2f, 0x76, 0x34, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x46, 0x65, 0x65, 0x64, 0x42, 0x12, 0x5a, 0x10, 0x78, 0x65, + 0x69, 0x61, 0x73, 0x6f, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x76, 0x34, 0x2f, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -167,18 +163,15 @@ var file_xesite_proto_goTypes = []interface{}{ (*timestamppb.Timestamp)(nil), // 1: google.protobuf.Timestamp (*emptypb.Empty)(nil), // 2: google.protobuf.Empty (*protofeed.Feed)(nil), // 3: protofeed.Feed - (*mi.EventFeed)(nil), // 4: within.website.x.mi.EventFeed } var file_xesite_proto_depIdxs = []int32{ 1, // 0: xeiaso.net.BuildInfo.build_time:type_name -> google.protobuf.Timestamp 2, // 1: xeiaso.net.Meta.Metadata:input_type -> google.protobuf.Empty 2, // 2: xeiaso.net.Feed.Get:input_type -> google.protobuf.Empty - 2, // 3: xeiaso.net.Events.Get:input_type -> google.protobuf.Empty - 0, // 4: xeiaso.net.Meta.Metadata:output_type -> xeiaso.net.BuildInfo - 3, // 5: xeiaso.net.Feed.Get:output_type -> protofeed.Feed - 4, // 6: xeiaso.net.Events.Get:output_type -> within.website.x.mi.EventFeed - 4, // [4:7] is the sub-list for method output_type - 1, // [1:4] is the sub-list for method input_type + 0, // 3: xeiaso.net.Meta.Metadata:output_type -> xeiaso.net.BuildInfo + 3, // 4: xeiaso.net.Feed.Get:output_type -> protofeed.Feed + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type 1, // [1:1] is the sub-list for extension type_name 1, // [1:1] is the sub-list for extension extendee 0, // [0:1] is the sub-list for field type_name @@ -211,7 +204,7 @@ func file_xesite_proto_init() { NumEnums: 0, NumMessages: 1, NumExtensions: 0, - NumServices: 3, + NumServices: 2, }, GoTypes: file_xesite_proto_goTypes, DependencyIndexes: file_xesite_proto_depIdxs, diff --git a/pb/xesite.proto b/pb/xesite.proto index e7f426a..aea867c 100644 --- a/pb/xesite.proto +++ b/pb/xesite.proto @@ -33,10 +33,4 @@ message BuildInfo { service Feed { // Get fetches the current feed of posts. rpc Get(google.protobuf.Empty) returns (protofeed.Feed); -} - -// Events lets users fetch the current feed of events that Xe will be attending. -service Events { - // Get fetches the current feed of upcoming events. - rpc Get(google.protobuf.Empty) returns (within.website.x.mi.EventFeed); } \ No newline at end of file diff --git a/pb/xesite.twirp.go b/pb/xesite.twirp.go index 248bb15..ac67c62 100644 --- a/pb/xesite.twirp.go +++ b/pb/xesite.twirp.go @@ -18,7 +18,6 @@ import ctxsetters "github.com/twitchtv/twirp/ctxsetters" import google_protobuf "google.golang.org/protobuf/types/known/emptypb" import protofeed "xeiaso.net/v4/pb/external/protofeed" -import within_website_x_mi "xeiaso.net/v4/pb/external/mi" import bytes "bytes" import errors "errors" @@ -1024,502 +1023,6 @@ func (s *feedServer) PathPrefix() string { return baseServicePath(s.pathPrefix, "xeiaso.net", "Feed") } -// ================ -// Events Interface -// ================ - -// Events lets users fetch the current feed of events that Xe will be attending. -type Events interface { - // Get fetches the current feed of upcoming events. - Get(context.Context, *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) -} - -// ====================== -// Events Protobuf Client -// ====================== - -type eventsProtobufClient struct { - client HTTPClient - urls [1]string - interceptor twirp.Interceptor - opts twirp.ClientOptions -} - -// NewEventsProtobufClient creates a Protobuf client that implements the Events interface. -// It communicates using Protobuf and can be configured with a custom HTTPClient. -func NewEventsProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) Events { - if c, ok := client.(*http.Client); ok { - client = withoutRedirects(c) - } - - clientOpts := twirp.ClientOptions{} - for _, o := range opts { - o(&clientOpts) - } - - // Using ReadOpt allows backwards and forwards compatibility with new options in the future - literalURLs := false - _ = clientOpts.ReadOpt("literalURLs", &literalURLs) - var pathPrefix string - if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { - pathPrefix = "/twirp" // default prefix - } - - // Build method URLs: []/./ - serviceURL := sanitizeBaseURL(baseURL) - serviceURL += baseServicePath(pathPrefix, "xeiaso.net", "Events") - urls := [1]string{ - serviceURL + "Get", - } - - return &eventsProtobufClient{ - client: client, - urls: urls, - interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), - opts: clientOpts, - } -} - -func (c *eventsProtobufClient) Get(ctx context.Context, in *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - ctx = ctxsetters.WithPackageName(ctx, "xeiaso.net") - ctx = ctxsetters.WithServiceName(ctx, "Events") - ctx = ctxsetters.WithMethodName(ctx, "Get") - caller := c.callGet - if c.interceptor != nil { - caller = func(ctx context.Context, req *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - resp, err := c.interceptor( - func(ctx context.Context, req interface{}) (interface{}, error) { - typedReq, ok := req.(*google_protobuf.Empty) - if !ok { - return nil, twirp.InternalError("failed type assertion req.(*google_protobuf.Empty) when calling interceptor") - } - return c.callGet(ctx, typedReq) - }, - )(ctx, req) - if resp != nil { - typedResp, ok := resp.(*within_website_x_mi.EventFeed) - if !ok { - return nil, twirp.InternalError("failed type assertion resp.(*within_website_x_mi.EventFeed) when calling interceptor") - } - return typedResp, err - } - return nil, err - } - } - return caller(ctx, in) -} - -func (c *eventsProtobufClient) callGet(ctx context.Context, in *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - out := new(within_website_x_mi.EventFeed) - ctx, err := doProtobufRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) - if err != nil { - twerr, ok := err.(twirp.Error) - if !ok { - twerr = twirp.InternalErrorWith(err) - } - callClientError(ctx, c.opts.Hooks, twerr) - return nil, err - } - - callClientResponseReceived(ctx, c.opts.Hooks) - - return out, nil -} - -// ================== -// Events JSON Client -// ================== - -type eventsJSONClient struct { - client HTTPClient - urls [1]string - interceptor twirp.Interceptor - opts twirp.ClientOptions -} - -// NewEventsJSONClient creates a JSON client that implements the Events interface. -// It communicates using JSON and can be configured with a custom HTTPClient. -func NewEventsJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) Events { - if c, ok := client.(*http.Client); ok { - client = withoutRedirects(c) - } - - clientOpts := twirp.ClientOptions{} - for _, o := range opts { - o(&clientOpts) - } - - // Using ReadOpt allows backwards and forwards compatibility with new options in the future - literalURLs := false - _ = clientOpts.ReadOpt("literalURLs", &literalURLs) - var pathPrefix string - if ok := clientOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { - pathPrefix = "/twirp" // default prefix - } - - // Build method URLs: []/./ - serviceURL := sanitizeBaseURL(baseURL) - serviceURL += baseServicePath(pathPrefix, "xeiaso.net", "Events") - urls := [1]string{ - serviceURL + "Get", - } - - return &eventsJSONClient{ - client: client, - urls: urls, - interceptor: twirp.ChainInterceptors(clientOpts.Interceptors...), - opts: clientOpts, - } -} - -func (c *eventsJSONClient) Get(ctx context.Context, in *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - ctx = ctxsetters.WithPackageName(ctx, "xeiaso.net") - ctx = ctxsetters.WithServiceName(ctx, "Events") - ctx = ctxsetters.WithMethodName(ctx, "Get") - caller := c.callGet - if c.interceptor != nil { - caller = func(ctx context.Context, req *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - resp, err := c.interceptor( - func(ctx context.Context, req interface{}) (interface{}, error) { - typedReq, ok := req.(*google_protobuf.Empty) - if !ok { - return nil, twirp.InternalError("failed type assertion req.(*google_protobuf.Empty) when calling interceptor") - } - return c.callGet(ctx, typedReq) - }, - )(ctx, req) - if resp != nil { - typedResp, ok := resp.(*within_website_x_mi.EventFeed) - if !ok { - return nil, twirp.InternalError("failed type assertion resp.(*within_website_x_mi.EventFeed) when calling interceptor") - } - return typedResp, err - } - return nil, err - } - } - return caller(ctx, in) -} - -func (c *eventsJSONClient) callGet(ctx context.Context, in *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - out := new(within_website_x_mi.EventFeed) - ctx, err := doJSONRequest(ctx, c.client, c.opts.Hooks, c.urls[0], in, out) - if err != nil { - twerr, ok := err.(twirp.Error) - if !ok { - twerr = twirp.InternalErrorWith(err) - } - callClientError(ctx, c.opts.Hooks, twerr) - return nil, err - } - - callClientResponseReceived(ctx, c.opts.Hooks) - - return out, nil -} - -// ===================== -// Events Server Handler -// ===================== - -type eventsServer struct { - Events - interceptor twirp.Interceptor - hooks *twirp.ServerHooks - pathPrefix string // prefix for routing - jsonSkipDefaults bool // do not include unpopulated fields (default values) in the response - jsonCamelCase bool // JSON fields are serialized as lowerCamelCase rather than keeping the original proto names -} - -// NewEventsServer builds a TwirpServer that can be used as an http.Handler to handle -// HTTP requests that are routed to the right method in the provided svc implementation. -// The opts are twirp.ServerOption modifiers, for example twirp.WithServerHooks(hooks). -func NewEventsServer(svc Events, opts ...interface{}) TwirpServer { - serverOpts := newServerOpts(opts) - - // Using ReadOpt allows backwards and forwards compatibility with new options in the future - jsonSkipDefaults := false - _ = serverOpts.ReadOpt("jsonSkipDefaults", &jsonSkipDefaults) - jsonCamelCase := false - _ = serverOpts.ReadOpt("jsonCamelCase", &jsonCamelCase) - var pathPrefix string - if ok := serverOpts.ReadOpt("pathPrefix", &pathPrefix); !ok { - pathPrefix = "/twirp" // default prefix - } - - return &eventsServer{ - Events: svc, - hooks: serverOpts.Hooks, - interceptor: twirp.ChainInterceptors(serverOpts.Interceptors...), - pathPrefix: pathPrefix, - jsonSkipDefaults: jsonSkipDefaults, - jsonCamelCase: jsonCamelCase, - } -} - -// writeError writes an HTTP response with a valid Twirp error format, and triggers hooks. -// If err is not a twirp.Error, it will get wrapped with twirp.InternalErrorWith(err) -func (s *eventsServer) writeError(ctx context.Context, resp http.ResponseWriter, err error) { - writeError(ctx, resp, err, s.hooks) -} - -// handleRequestBodyError is used to handle error when the twirp server cannot read request -func (s *eventsServer) handleRequestBodyError(ctx context.Context, resp http.ResponseWriter, msg string, err error) { - if context.Canceled == ctx.Err() { - s.writeError(ctx, resp, twirp.NewError(twirp.Canceled, "failed to read request: context canceled")) - return - } - if context.DeadlineExceeded == ctx.Err() { - s.writeError(ctx, resp, twirp.NewError(twirp.DeadlineExceeded, "failed to read request: deadline exceeded")) - return - } - s.writeError(ctx, resp, twirp.WrapError(malformedRequestError(msg), err)) -} - -// EventsPathPrefix is a convenience constant that may identify URL paths. -// Should be used with caution, it only matches routes generated by Twirp Go clients, -// with the default "/twirp" prefix and default CamelCase service and method names. -// More info: https://twitchtv.github.io/twirp/docs/routing.html -const EventsPathPrefix = "/twirp/xeiaso.net.Events/" - -func (s *eventsServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - ctx := req.Context() - ctx = ctxsetters.WithPackageName(ctx, "xeiaso.net") - ctx = ctxsetters.WithServiceName(ctx, "Events") - ctx = ctxsetters.WithResponseWriter(ctx, resp) - - var err error - ctx, err = callRequestReceived(ctx, s.hooks) - if err != nil { - s.writeError(ctx, resp, err) - return - } - - if req.Method != "POST" { - msg := fmt.Sprintf("unsupported method %q (only POST is allowed)", req.Method) - s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) - return - } - - // Verify path format: []/./ - prefix, pkgService, method := parseTwirpPath(req.URL.Path) - if pkgService != "xeiaso.net.Events" { - msg := fmt.Sprintf("no handler for path %q", req.URL.Path) - s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) - return - } - if prefix != s.pathPrefix { - msg := fmt.Sprintf("invalid path prefix %q, expected %q, on path %q", prefix, s.pathPrefix, req.URL.Path) - s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) - return - } - - switch method { - case "Get": - s.serveGet(ctx, resp, req) - return - default: - msg := fmt.Sprintf("no handler for path %q", req.URL.Path) - s.writeError(ctx, resp, badRouteError(msg, req.Method, req.URL.Path)) - return - } -} - -func (s *eventsServer) serveGet(ctx context.Context, resp http.ResponseWriter, req *http.Request) { - header := req.Header.Get("Content-Type") - i := strings.Index(header, ";") - if i == -1 { - i = len(header) - } - switch strings.TrimSpace(strings.ToLower(header[:i])) { - case "application/json": - s.serveGetJSON(ctx, resp, req) - case "application/protobuf": - s.serveGetProtobuf(ctx, resp, req) - default: - msg := fmt.Sprintf("unexpected Content-Type: %q", req.Header.Get("Content-Type")) - twerr := badRouteError(msg, req.Method, req.URL.Path) - s.writeError(ctx, resp, twerr) - } -} - -func (s *eventsServer) serveGetJSON(ctx context.Context, resp http.ResponseWriter, req *http.Request) { - var err error - ctx = ctxsetters.WithMethodName(ctx, "Get") - ctx, err = callRequestRouted(ctx, s.hooks) - if err != nil { - s.writeError(ctx, resp, err) - return - } - - d := json.NewDecoder(req.Body) - rawReqBody := json.RawMessage{} - if err := d.Decode(&rawReqBody); err != nil { - s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) - return - } - reqContent := new(google_protobuf.Empty) - unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true} - if err = unmarshaler.Unmarshal(rawReqBody, reqContent); err != nil { - s.handleRequestBodyError(ctx, resp, "the json request could not be decoded", err) - return - } - - handler := s.Events.Get - if s.interceptor != nil { - handler = func(ctx context.Context, req *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - resp, err := s.interceptor( - func(ctx context.Context, req interface{}) (interface{}, error) { - typedReq, ok := req.(*google_protobuf.Empty) - if !ok { - return nil, twirp.InternalError("failed type assertion req.(*google_protobuf.Empty) when calling interceptor") - } - return s.Events.Get(ctx, typedReq) - }, - )(ctx, req) - if resp != nil { - typedResp, ok := resp.(*within_website_x_mi.EventFeed) - if !ok { - return nil, twirp.InternalError("failed type assertion resp.(*within_website_x_mi.EventFeed) when calling interceptor") - } - return typedResp, err - } - return nil, err - } - } - - // Call service method - var respContent *within_website_x_mi.EventFeed - func() { - defer ensurePanicResponses(ctx, resp, s.hooks) - respContent, err = handler(ctx, reqContent) - }() - - if err != nil { - s.writeError(ctx, resp, err) - return - } - if respContent == nil { - s.writeError(ctx, resp, twirp.InternalError("received a nil *within_website_x_mi.EventFeed and nil error while calling Get. nil responses are not supported")) - return - } - - ctx = callResponsePrepared(ctx, s.hooks) - - marshaler := &protojson.MarshalOptions{UseProtoNames: !s.jsonCamelCase, EmitUnpopulated: !s.jsonSkipDefaults} - respBytes, err := marshaler.Marshal(respContent) - if err != nil { - s.writeError(ctx, resp, wrapInternal(err, "failed to marshal json response")) - return - } - - ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) - resp.Header().Set("Content-Type", "application/json") - resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) - resp.WriteHeader(http.StatusOK) - - if n, err := resp.Write(respBytes); err != nil { - msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) - twerr := twirp.NewError(twirp.Unknown, msg) - ctx = callError(ctx, s.hooks, twerr) - } - callResponseSent(ctx, s.hooks) -} - -func (s *eventsServer) serveGetProtobuf(ctx context.Context, resp http.ResponseWriter, req *http.Request) { - var err error - ctx = ctxsetters.WithMethodName(ctx, "Get") - ctx, err = callRequestRouted(ctx, s.hooks) - if err != nil { - s.writeError(ctx, resp, err) - return - } - - buf, err := io.ReadAll(req.Body) - if err != nil { - s.handleRequestBodyError(ctx, resp, "failed to read request body", err) - return - } - reqContent := new(google_protobuf.Empty) - if err = proto.Unmarshal(buf, reqContent); err != nil { - s.writeError(ctx, resp, malformedRequestError("the protobuf request could not be decoded")) - return - } - - handler := s.Events.Get - if s.interceptor != nil { - handler = func(ctx context.Context, req *google_protobuf.Empty) (*within_website_x_mi.EventFeed, error) { - resp, err := s.interceptor( - func(ctx context.Context, req interface{}) (interface{}, error) { - typedReq, ok := req.(*google_protobuf.Empty) - if !ok { - return nil, twirp.InternalError("failed type assertion req.(*google_protobuf.Empty) when calling interceptor") - } - return s.Events.Get(ctx, typedReq) - }, - )(ctx, req) - if resp != nil { - typedResp, ok := resp.(*within_website_x_mi.EventFeed) - if !ok { - return nil, twirp.InternalError("failed type assertion resp.(*within_website_x_mi.EventFeed) when calling interceptor") - } - return typedResp, err - } - return nil, err - } - } - - // Call service method - var respContent *within_website_x_mi.EventFeed - func() { - defer ensurePanicResponses(ctx, resp, s.hooks) - respContent, err = handler(ctx, reqContent) - }() - - if err != nil { - s.writeError(ctx, resp, err) - return - } - if respContent == nil { - s.writeError(ctx, resp, twirp.InternalError("received a nil *within_website_x_mi.EventFeed and nil error while calling Get. nil responses are not supported")) - return - } - - ctx = callResponsePrepared(ctx, s.hooks) - - respBytes, err := proto.Marshal(respContent) - if err != nil { - s.writeError(ctx, resp, wrapInternal(err, "failed to marshal proto response")) - return - } - - ctx = ctxsetters.WithStatusCode(ctx, http.StatusOK) - resp.Header().Set("Content-Type", "application/protobuf") - resp.Header().Set("Content-Length", strconv.Itoa(len(respBytes))) - resp.WriteHeader(http.StatusOK) - if n, err := resp.Write(respBytes); err != nil { - msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error()) - twerr := twirp.NewError(twirp.Unknown, msg) - ctx = callError(ctx, s.hooks, twerr) - } - callResponseSent(ctx, s.hooks) -} - -func (s *eventsServer) ServiceDescriptor() ([]byte, int) { - return twirpFileDescriptor0, 2 -} - -func (s *eventsServer) ProtocGenTwirpVersion() string { - return "v8.1.3" -} - -// PathPrefix returns the base service path, in the form: "//./" -// that is everything in a Twirp route except for the . This can be used for routing, -// for example to identify the requests that are targeted to this service in a mux. -func (s *eventsServer) PathPrefix() string { - return baseServicePath(s.pathPrefix, "xeiaso.net", "Events") -} - // ===== // Utils // ===== @@ -2086,26 +1589,24 @@ func callClientError(ctx context.Context, h *twirp.ClientHooks, err twirp.Error) } var twirpFileDescriptor0 = []byte{ - // 329 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0x4f, 0x4b, 0xc3, 0x40, - 0x10, 0xc5, 0x89, 0xad, 0xc5, 0x4c, 0xeb, 0xbf, 0x05, 0x4b, 0x88, 0xa8, 0x55, 0x10, 0x7a, 0xda, - 0x40, 0x14, 0xa1, 0x07, 0x0f, 0x16, 0x6a, 0xf1, 0xe0, 0xa5, 0x88, 0x07, 0x2f, 0x25, 0x31, 0xd3, - 0xb8, 0xd0, 0xdd, 0x0d, 0xcd, 0xb4, 0x8d, 0x9f, 0xd0, 0xaf, 0x25, 0xbb, 0x49, 0x1b, 0x50, 0xf4, - 0xb4, 0xcc, 0x9b, 0xdf, 0xcc, 0xbe, 0x7d, 0x0b, 0x9d, 0x02, 0x73, 0x41, 0xc8, 0xb3, 0x85, 0x26, - 0xcd, 0xa0, 0x40, 0x11, 0xe5, 0x9a, 0x2b, 0x24, 0xff, 0x34, 0xd5, 0x3a, 0x9d, 0x63, 0x60, 0x3b, - 0xf1, 0x72, 0x16, 0xa0, 0xcc, 0xe8, 0xb3, 0x04, 0xfd, 0x8b, 0x9f, 0x4d, 0x12, 0x12, 0x73, 0x8a, - 0x64, 0x56, 0x01, 0xc7, 0x58, 0x10, 0x2e, 0x54, 0x34, 0x0f, 0xa4, 0xa8, 0x24, 0x6f, 0x2b, 0xd9, - 0x7a, 0x86, 0x98, 0x94, 0x9d, 0xab, 0x2f, 0x07, 0xdc, 0xe1, 0x52, 0xcc, 0x93, 0x27, 0x35, 0xd3, - 0xac, 0x0b, 0xad, 0x77, 0x2d, 0xa5, 0x20, 0xcf, 0xe9, 0x39, 0x7d, 0x77, 0x52, 0x55, 0x6c, 0x00, - 0x10, 0x1b, 0x68, 0x6a, 0xee, 0xf2, 0x76, 0x7a, 0x4e, 0xbf, 0x1d, 0xfa, 0xbc, 0x34, 0xc2, 0x37, - 0x46, 0xf8, 0xcb, 0xc6, 0xc8, 0xc4, 0xb5, 0xb4, 0xa9, 0xd9, 0x19, 0x40, 0xaa, 0xa7, 0x2b, 0x5c, - 0xe4, 0x42, 0x2b, 0xaf, 0x61, 0xd7, 0xba, 0xa9, 0x7e, 0x2d, 0x05, 0x76, 0x09, 0x9d, 0x04, 0x55, - 0x0d, 0x34, 0x2d, 0xd0, 0x36, 0xda, 0x06, 0xb9, 0x86, 0x83, 0x32, 0xa9, 0x2d, 0xb4, 0x6b, 0xa1, - 0xfd, 0x52, 0xad, 0xb0, 0xf0, 0x01, 0x9a, 0xcf, 0x48, 0x11, 0x1b, 0xc0, 0x9e, 0x39, 0x93, 0x88, - 0x22, 0xd6, 0xfd, 0xe5, 0x71, 0x64, 0x92, 0xf4, 0x4f, 0x78, 0x9d, 0x36, 0xdf, 0x3e, 0x3f, 0xbc, - 0x83, 0xe6, 0x23, 0x62, 0xc2, 0x38, 0x34, 0xc6, 0x48, 0x7f, 0x4e, 0x1f, 0xf2, 0x3a, 0x45, 0xc3, - 0x87, 0x63, 0x68, 0x8d, 0x56, 0xa8, 0x28, 0x67, 0xf7, 0xff, 0x4f, 0x9e, 0xf3, 0xb5, 0xa0, 0x0f, - 0xa1, 0xf8, 0x1a, 0x63, 0xfb, 0xf7, 0x05, 0x97, 0x82, 0xdb, 0x59, 0xb3, 0x68, 0xc8, 0xde, 0x8e, - 0x6a, 0x63, 0xc1, 0xea, 0x36, 0xc8, 0xe2, 0xb8, 0x65, 0x77, 0xdc, 0x7c, 0x07, 0x00, 0x00, 0xff, - 0xff, 0xba, 0x70, 0xbe, 0x6e, 0x2f, 0x02, 0x00, 0x00, + // 296 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x90, 0x5f, 0x4b, 0xc3, 0x30, + 0x14, 0xc5, 0xa9, 0xab, 0xc3, 0xde, 0xcd, 0x7f, 0x01, 0x47, 0xa9, 0x88, 0x53, 0x10, 0xf6, 0x94, + 0x42, 0x15, 0x61, 0x8f, 0x0e, 0x54, 0x7c, 0xf0, 0x65, 0x88, 0x0f, 0xbe, 0x8c, 0xd4, 0xde, 0x96, + 0x40, 0xd3, 0x94, 0x36, 0x1b, 0xf5, 0x13, 0xfa, 0xb5, 0x24, 0x49, 0xff, 0x80, 0xe2, 0x53, 0xb8, + 0xe7, 0xfc, 0x72, 0x73, 0x72, 0x60, 0xda, 0x60, 0xcd, 0x15, 0xd2, 0xb2, 0x92, 0x4a, 0x12, 0x68, + 0x90, 0xb3, 0x5a, 0xd2, 0x02, 0x55, 0x70, 0x9e, 0x49, 0x99, 0xe5, 0x18, 0x1a, 0x27, 0xde, 0xa6, + 0x21, 0x8a, 0x52, 0x7d, 0x59, 0x30, 0xb8, 0xfc, 0x6d, 0x2a, 0x2e, 0xb0, 0x56, 0x4c, 0x94, 0x2d, + 0x70, 0x8a, 0x8d, 0xc2, 0xaa, 0x60, 0x79, 0x28, 0x78, 0x2b, 0xf9, 0xbd, 0x64, 0xe6, 0x14, 0x31, + 0xb1, 0xce, 0xf5, 0xb7, 0x03, 0xde, 0x6a, 0xcb, 0xf3, 0xe4, 0xa5, 0x48, 0x25, 0x99, 0xc1, 0xf8, + 0x53, 0x0a, 0xc1, 0x95, 0xef, 0xcc, 0x9d, 0x85, 0xb7, 0x6e, 0x27, 0xb2, 0x04, 0x88, 0x35, 0xb4, + 0xd1, 0x6f, 0xf9, 0x7b, 0x73, 0x67, 0x31, 0x89, 0x02, 0x6a, 0x83, 0xd0, 0x2e, 0x08, 0x7d, 0xeb, + 0x82, 0xac, 0x3d, 0x43, 0xeb, 0x99, 0x5c, 0x00, 0x64, 0x72, 0xb3, 0xc3, 0xaa, 0xe6, 0xb2, 0xf0, + 0x47, 0x66, 0xad, 0x97, 0xc9, 0x77, 0x2b, 0x90, 0x2b, 0x98, 0x26, 0x58, 0x0c, 0x80, 0x6b, 0x80, + 0x89, 0xd6, 0x3a, 0xe4, 0x06, 0x8e, 0x6c, 0x53, 0x3d, 0xb4, 0x6f, 0xa0, 0x43, 0xab, 0xb6, 0x58, + 0xf4, 0x00, 0xee, 0x2b, 0x2a, 0x46, 0x96, 0x70, 0xa0, 0xcf, 0x84, 0x29, 0x46, 0x66, 0x7f, 0x32, + 0x3e, 0xea, 0x26, 0x83, 0x33, 0x3a, 0xb4, 0x4d, 0xfb, 0xef, 0x47, 0xf7, 0xe0, 0x3e, 0x21, 0x26, + 0x84, 0xc2, 0xe8, 0x19, 0xd5, 0xbf, 0xb7, 0x8f, 0xe9, 0xd0, 0xa2, 0xe6, 0x57, 0xe4, 0xe3, 0x64, + 0xd8, 0x17, 0xee, 0xee, 0xc2, 0x32, 0x8e, 0xc7, 0x86, 0xb9, 0xfd, 0x09, 0x00, 0x00, 0xff, 0xff, + 0x23, 0x62, 0xd8, 0x88, 0xe6, 0x01, 0x00, 0x00, } -- cgit v1.2.3