From 2f98ecd0c6bdfcfd97c67b64bc057bf29c0ad3b4 Mon Sep 17 00:00:00 2001 From: Anthony Rubick <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Sun, 7 Sep 2025 17:10:02 -0500 Subject: [PATCH] fix(event-subscriber-example): remove event.go the file was removed from zoraxy_plugin, but since `build_all.sh` works by copying the module to the plugins this change wasn't propagated. This fix removed the leftover file and updates `build_all.sh` to delete the existing `zoraxy_plugin` module of the plugins before copying over the updated code. --- example/plugins/build_all.sh | 5 + .../plugins/event-subscriber-example/main.go | 6 +- .../mod/zoraxy_plugin/event.go | 120 ------------------ 3 files changed, 8 insertions(+), 123 deletions(-) delete mode 100644 example/plugins/event-subscriber-example/mod/zoraxy_plugin/event.go diff --git a/example/plugins/build_all.sh b/example/plugins/build_all.sh index 92dde57..c309639 100644 --- a/example/plugins/build_all.sh +++ b/example/plugins/build_all.sh @@ -4,6 +4,11 @@ echo "Copying zoraxy_plugin to all mods" for dir in ./*; do if [ -d "$dir" ]; then + # remove existing zoraxy_plugin module, if it exists + if [ -d "${dir}/mod/zoraxy_plugin" ]; then + rm -r $dir/mod/zoraxy_plugin + fi + # copy over updated module cp -r ../../src/mod/plugins/zoraxy_plugin "$dir/mod" fi done diff --git a/example/plugins/event-subscriber-example/main.go b/example/plugins/event-subscriber-example/main.go index 736eeef..5867c02 100644 --- a/example/plugins/event-subscriber-example/main.go +++ b/example/plugins/event-subscriber-example/main.go @@ -17,7 +17,7 @@ const ( ) var ( - EventLog = make([]plugin.Event, 0) // A slice to store events + EventLog = make([]events.Event, 0) // A slice to store events EventLogMutex = &sync.Mutex{} // Mutex to protect access to the event log ) @@ -58,7 +58,7 @@ func main() { }) http.HandleFunc(EVENT_PATH, func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { - var event plugin.Event + var event events.Event // read the request body if r.Body == nil || r.ContentLength == 0 { @@ -74,7 +74,7 @@ func main() { } // parse the event from the request body - if err := plugin.ParseEvent(buffer.Bytes(), &event); err != nil { + if err := events.ParseEvent(buffer.Bytes(), &event); err != nil { http.Error(w, fmt.Sprintf("Failed to parse event: %v", err), http.StatusBadRequest) return } diff --git a/example/plugins/event-subscriber-example/mod/zoraxy_plugin/event.go b/example/plugins/event-subscriber-example/mod/zoraxy_plugin/event.go deleted file mode 100644 index 0e0924e..0000000 --- a/example/plugins/event-subscriber-example/mod/zoraxy_plugin/event.go +++ /dev/null @@ -1,120 +0,0 @@ -package zoraxy_plugin - -import ( - "encoding/json" - "fmt" -) - -// EventName represents the type of event -type EventName string - -// EventPayload interface for all event payloads -type EventPayload interface { - // GetName returns the event type - GetName() EventName -} - -// Event represents a system event -type Event struct { - Name EventName `json:"name"` - Timestamp int64 `json:"timestamp"` // Unix timestamp - Data EventPayload `json:"data"` -} - -const ( - // EventBlacklistedIPBlocked is emitted when a blacklisted IP is blocked - EventBlacklistedIPBlocked EventName = "blacklistedIpBlocked" - // EventBlacklistToggled is emitted when the blacklist is toggled for an access rule - EventBlacklistToggled EventName = "blacklistToggled" - // EventAccessRuleCreated is emitted when a new access ruleset is created - EventAccessRuleCreated EventName = "accessRuleCreated" - - // Add more event types as needed -) - -// BlacklistedIPBlockedEvent represents an event when a blacklisted IP is blocked -type BlacklistedIPBlockedEvent struct { - IP string `json:"ip"` - Comment string `json:"comment"` - RequestedURL string `json:"requested_url"` - Hostname string `json:"hostname"` - UserAgent string `json:"user_agent"` - Method string `json:"method"` -} - -func (e *BlacklistedIPBlockedEvent) GetName() EventName { - return EventBlacklistedIPBlocked -} - -// BlacklistToggledEvent represents an event when the blacklist is disabled for an access rule -type BlacklistToggledEvent struct { - RuleID string `json:"rule_id"` - Enabled bool `json:"enabled"` // Whether the blacklist is enabled or disabled -} - -func (e *BlacklistToggledEvent) GetName() EventName { - return EventBlacklistToggled -} - -// AccessRuleCreatedEvent represents an event when a new access ruleset is created -type AccessRuleCreatedEvent struct { - ID string `json:"id"` - Name string `json:"name"` - Desc string `json:"desc"` - BlacklistEnabled bool `json:"blacklist_enabled"` - WhitelistEnabled bool `json:"whitelist_enabled"` -} - -func (e *AccessRuleCreatedEvent) GetName() EventName { - return EventAccessRuleCreated -} - -// ParseEvent parses a JSON byte slice into an Event struct -func ParseEvent(jsonData []byte, event *Event) error { - // First, determine the event type, and parse shared fields, from the JSON data - var temp struct { - Name EventName `json:"name"` - Timestamp int64 `json:"timestamp"` - } - if err := json.Unmarshal(jsonData, &temp); err != nil { - return err - } - - // Set the event name and timestamp - event.Name = temp.Name - event.Timestamp = temp.Timestamp - - // Now, based on the event type, unmarshal the specific payload - switch temp.Name { - case EventBlacklistedIPBlocked: - type tempData struct { - Data BlacklistedIPBlockedEvent `json:"data"` - } - var payload tempData - if err := json.Unmarshal(jsonData, &payload); err != nil { - return err - } - event.Data = &payload.Data - case EventBlacklistToggled: - type tempData struct { - Data BlacklistToggledEvent `json:"data"` - } - var payload tempData - if err := json.Unmarshal(jsonData, &payload); err != nil { - return err - } - event.Data = &payload.Data - case EventAccessRuleCreated: - type tempData struct { - Data AccessRuleCreatedEvent `json:"data"` - } - var payload tempData - if err := json.Unmarshal(jsonData, &payload); err != nil { - return err - } - event.Data = &payload.Data - default: - return fmt.Errorf("unknown event: %s, %v", temp.Name, jsonData) - } - return nil -}