mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-09-14 19:45:54 -05:00
Add event logging via database and webhook
This commit is contained in:
34
database/events.go
Normal file
34
database/events.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"wwfc/common"
|
||||
"wwfc/logging"
|
||||
)
|
||||
|
||||
const (
|
||||
insertEventQuery = `
|
||||
INSERT INTO events (event_type, event_data)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
)
|
||||
|
||||
func (c *Connection) InsertEvent(eventType string, eventData map[string]any) (int, error) {
|
||||
var eventId int
|
||||
err := c.pool.QueryRow(c.ctx, insertEventQuery, eventType, eventData).Scan(&eventId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return eventId, nil
|
||||
}
|
||||
|
||||
func (c *Connection) RegisterEventLogging(config common.Config, eventTypes []string) {
|
||||
if !config.EventReporting.LogToDatabase {
|
||||
return
|
||||
}
|
||||
logging.RegisterEventCallback(eventTypes, func(eventType string, eventData map[string]any) {
|
||||
_, err := c.InsertEvent(eventType, eventData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user