|
@@ -3,6 +3,7 @@ package server
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "github.com/emqx/kuiper/plugins"
|
|
|
"github.com/emqx/kuiper/xstream/api"
|
|
|
"github.com/gorilla/mux"
|
|
|
"io"
|
|
@@ -60,6 +61,13 @@ func createRestServer(port int) *http.Server {
|
|
|
r.HandleFunc("/rules/{name}/stop", stopRuleHandler).Methods(http.MethodPost)
|
|
|
r.HandleFunc("/rules/{name}/restart", restartRuleHandler).Methods(http.MethodPost)
|
|
|
|
|
|
+ r.HandleFunc("/plugins/sources", sourcesHandler).Methods(http.MethodGet, http.MethodPost)
|
|
|
+ r.HandleFunc("/plugins/sources/{name}", sourceHandler).Methods(http.MethodDelete)
|
|
|
+ r.HandleFunc("/plugins/sinks", sinksHandler).Methods(http.MethodGet, http.MethodPost)
|
|
|
+ r.HandleFunc("/plugins/sinks/{name}", sinkHandler).Methods(http.MethodDelete)
|
|
|
+ r.HandleFunc("/plugins/functions", functionsHandler).Methods(http.MethodGet, http.MethodPost)
|
|
|
+ r.HandleFunc("/plugins/functions/{name}", functionHandler).Methods(http.MethodDelete)
|
|
|
+
|
|
|
server := &http.Server{
|
|
|
Addr: fmt.Sprintf("0.0.0.0:%d", port),
|
|
|
// Good practice to set timeouts to avoid Slowloris attacks.
|
|
@@ -248,3 +256,78 @@ func restartRuleHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
w.Write([]byte(fmt.Sprintf("Rule %s was restarted", name)))
|
|
|
}
|
|
|
+
|
|
|
+func pluginsHandler(w http.ResponseWriter, r *http.Request, t plugins.PluginType) {
|
|
|
+ defer r.Body.Close()
|
|
|
+ switch r.Method {
|
|
|
+ case http.MethodGet:
|
|
|
+ content, err := pluginManager.List(t)
|
|
|
+ if err != nil {
|
|
|
+ handleError(w, fmt.Errorf("%s plugins list command error: %s", plugins.PluginTypes[t], err), http.StatusBadRequest, logger)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ jsonResponse(content, w, logger)
|
|
|
+ case http.MethodPost:
|
|
|
+ sd := plugins.Plugin{}
|
|
|
+ err := json.NewDecoder(r.Body).Decode(&sd)
|
|
|
+ // Problems decoding
|
|
|
+ if err != nil {
|
|
|
+ handleError(w, fmt.Errorf("Invalid body: Error decoding the %s plugin json: %v", plugins.PluginTypes[t], err), http.StatusBadRequest, logger)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = pluginManager.Register(t, &sd)
|
|
|
+ if err != nil {
|
|
|
+ handleError(w, fmt.Errorf("%s plugins create command error: %s", plugins.PluginTypes[t], err), http.StatusBadRequest, logger)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ w.WriteHeader(http.StatusCreated)
|
|
|
+ w.Write([]byte(fmt.Sprintf("%s plugin %s is created", plugins.PluginTypes[t], sd.Name)))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func pluginHandler(w http.ResponseWriter, r *http.Request, t plugins.PluginType) {
|
|
|
+ defer r.Body.Close()
|
|
|
+ vars := mux.Vars(r)
|
|
|
+ name := vars["name"]
|
|
|
+
|
|
|
+ switch r.Method {
|
|
|
+ case http.MethodDelete:
|
|
|
+ err := pluginManager.Delete(t, name)
|
|
|
+ if err != nil {
|
|
|
+ handleError(w, fmt.Errorf("delete %s plugin %s error: %s", plugins.PluginTypes[t], name, err), http.StatusBadRequest, logger)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
+ w.Write([]byte(fmt.Sprintf("%s plugin %s is deleted", plugins.PluginTypes[t], name)))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//list or create source plugin
|
|
|
+func sourcesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginsHandler(w, r, plugins.SOURCE)
|
|
|
+}
|
|
|
+
|
|
|
+//delete a source plugin
|
|
|
+func sourceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginHandler(w, r, plugins.SOURCE)
|
|
|
+}
|
|
|
+
|
|
|
+//list or create sink plugin
|
|
|
+func sinksHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginsHandler(w, r, plugins.SINK)
|
|
|
+}
|
|
|
+
|
|
|
+//delete a sink plugin
|
|
|
+func sinkHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginHandler(w, r, plugins.SINK)
|
|
|
+}
|
|
|
+
|
|
|
+//list or create function plugin
|
|
|
+func functionsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginsHandler(w, r, plugins.FUNCTION)
|
|
|
+}
|
|
|
+
|
|
|
+//delete a function plugin
|
|
|
+func functionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ pluginHandler(w, r, plugins.FUNCTION)
|
|
|
+}
|