From c0acbdfaa0bf54e5c6d7462f7aad390860725f51 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 6 Nov 2023 15:27:34 +0000 Subject: [PATCH] fest: defer webhook event loads to prevent panics --- .../endpoints/get_full_app_list/route.go | 12 ++--------- webhooks/events/events.go | 21 +++++++++++++++++++ webhooks/setup.go | 3 +++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/routes/staff/endpoints/get_full_app_list/route.go b/routes/staff/endpoints/get_full_app_list/route.go index b9d9c8ab..f0134c27 100644 --- a/routes/staff/endpoints/get_full_app_list/route.go +++ b/routes/staff/endpoints/get_full_app_list/route.go @@ -24,16 +24,8 @@ func Docs() *docs.Doc { return &docs.Doc{ Summary: "Staff: Get Full Application List", Description: "Gets all applications of a user returning a list of apps.", - Params: []docs.Parameter{ - { - Name: "user_id", - Description: "The ID of the user to use.", - Required: true, - In: "path", - Schema: docs.IdSchema, - }, - }, - Resp: types.AppListResponse{}, + Params: []docs.Parameter{}, + Resp: types.AppListResponse{}, } } diff --git a/webhooks/events/events.go b/webhooks/events/events.go index 19d6d99c..e01fc2c7 100644 --- a/webhooks/events/events.go +++ b/webhooks/events/events.go @@ -3,12 +3,14 @@ package events import ( "fmt" "os" + "popplio/state" "popplio/types" "reflect" "github.com/bwmarrin/discordgo" docs "github.com/infinitybotlist/eureka/doclib" "github.com/infinitybotlist/eureka/dovewing/dovetypes" + "go.uber.org/zap" ) type EventRegistry struct { @@ -27,7 +29,26 @@ type WebhookEvent interface { Description() string } +var eventList = []WebhookEvent{} + +// Adds an event to be registered. This should be called in the init() function of the event +// +// Note that this function does not register the event, it just adds it to the list of events to be registered. +// This is because `doclib` and `state` are not initialized until after state setyp func RegisterEvent(a WebhookEvent) { + eventList = append(eventList, a) +} + +// Register all events +func RegisterAllEvents() { + for _, a := range eventList { + state.Logger.Error("Webhook event register", zap.String("event", a.Event())) + registerEventImpl(a) + } +} + +// Internal implementation to register an event +func registerEventImpl(a WebhookEvent) { docs.AddWebhook(&docs.WebhookDoc{ Name: a.Event(), Summary: a.Summary(), diff --git a/webhooks/setup.go b/webhooks/setup.go index 02ad60c6..3be6fb14 100644 --- a/webhooks/setup.go +++ b/webhooks/setup.go @@ -3,6 +3,7 @@ package webhooks import ( "popplio/webhooks/bothooks" "popplio/webhooks/bothooks_legacy" + "popplio/webhooks/events" "popplio/webhooks/sender" "popplio/webhooks/serverhooks" "popplio/webhooks/teamhooks" @@ -33,6 +34,8 @@ func Setup() { "Webhooks are a way to receive events from Infinity Bot List in real time. You can use webhooks to receive events such as new votes, new reviews, and more.", ) + events.RegisterAllEvents() + for _, driver := range RegisteredDrivers { driver.Register()