Skip to content

Commit

Permalink
fest: defer webhook event loads to prevent panics
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 6, 2023
1 parent 1c7acf9 commit c0acbdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 2 additions & 10 deletions routes/staff/endpoints/get_full_app_list/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

Expand Down
21 changes: 21 additions & 0 deletions webhooks/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions webhooks/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit c0acbdf

Please sign in to comment.