mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-11-07 17:34:12 +01:00
feat(plugin api): add endpoint to facilitate plugin<->plugin comms via event system.
This commit is contained in:
@@ -12,7 +12,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"imuslab.com/zoraxy/mod/eventsystem"
|
||||||
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin"
|
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin"
|
||||||
|
"imuslab.com/zoraxy/mod/plugins/zoraxy_plugin/events"
|
||||||
"imuslab.com/zoraxy/mod/utils"
|
"imuslab.com/zoraxy/mod/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -354,3 +356,39 @@ func (m *Manager) HandleUninstallPlugin(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
utils.SendOK(w)
|
utils.SendOK(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleEmitCustomEvent is the handler for emitting a custom event from a plugin to other plugins
|
||||||
|
func (m *Manager) HandleEmitCustomEvent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
utils.SendErrorResponse(w, "Method not allowed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if contentType == "" || !strings.HasPrefix(strings.ToLower(contentType), "application/json") {
|
||||||
|
utils.SendErrorResponse(w, "Invalid or missing Content-Type, expected application/json")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse the event payload from the request body
|
||||||
|
var payload events.CustomEvent
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||||
|
utils.SendErrorResponse(w, "Failed to parse event: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// collect the recipients
|
||||||
|
if len(payload.Recipients) > 0 {
|
||||||
|
recipients := make([]eventsystem.ListenerID, 0, len(payload.Recipients))
|
||||||
|
for _, rid := range payload.Recipients {
|
||||||
|
recipients = append(recipients, eventsystem.ListenerID(rid))
|
||||||
|
}
|
||||||
|
// Emit the event to subscribers and specified recipients
|
||||||
|
eventsystem.Publisher.EmitToSubscribersAnd(recipients, &payload)
|
||||||
|
} else {
|
||||||
|
// Emit the event to all subscribers
|
||||||
|
eventsystem.Publisher.Emit(&payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.SendOK(w)
|
||||||
|
}
|
||||||
|
|||||||
@@ -108,6 +108,10 @@ func RegisterPluginRestAPI(authRouter *auth.PluginAuthMiddleware) {
|
|||||||
authRouter.HandleFunc("/api/plugins/groups/list", pluginManager.HandleListPluginGroups)
|
authRouter.HandleFunc("/api/plugins/groups/list", pluginManager.HandleListPluginGroups)
|
||||||
|
|
||||||
authRouter.HandleFunc("/api/plugins/store/list", pluginManager.HandleListDownloadablePlugins)
|
authRouter.HandleFunc("/api/plugins/store/list", pluginManager.HandleListDownloadablePlugins)
|
||||||
|
|
||||||
|
// recall that these plugin APIs are under the /plugin path, so
|
||||||
|
// the full path to this endpoint is /plugin/event/emit
|
||||||
|
authRouter.HandleFunc("/event/emit", pluginManager.HandleEmitCustomEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Register all the APIs */
|
/* Register all the APIs */
|
||||||
|
|||||||
Reference in New Issue
Block a user