plugin_init.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build plugin || !core
  15. // +build plugin !core
  16. package server
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "github.com/gorilla/mux"
  21. "github.com/lf-edge/ekuiper/internal/binder"
  22. "github.com/lf-edge/ekuiper/internal/plugin"
  23. "github.com/lf-edge/ekuiper/internal/plugin/native"
  24. "github.com/lf-edge/ekuiper/pkg/errorx"
  25. "net/http"
  26. )
  27. var nativeManager *native.Manager
  28. func init() {
  29. components["plugin"] = pluginComp{}
  30. }
  31. type pluginComp struct{}
  32. func (p pluginComp) register() {
  33. var err error
  34. nativeManager, err = native.InitManager()
  35. if err != nil {
  36. panic(err)
  37. }
  38. entries = append(entries, binder.FactoryEntry{Name: "native plugin", Factory: nativeManager, Weight: 9})
  39. }
  40. func (p pluginComp) rest(r *mux.Router) {
  41. r.HandleFunc("/plugins/sources", sourcesHandler).Methods(http.MethodGet, http.MethodPost)
  42. r.HandleFunc("/plugins/sources/{name}", sourceHandler).Methods(http.MethodDelete, http.MethodGet)
  43. r.HandleFunc("/plugins/sinks", sinksHandler).Methods(http.MethodGet, http.MethodPost)
  44. r.HandleFunc("/plugins/sinks/{name}", sinkHandler).Methods(http.MethodDelete, http.MethodGet)
  45. r.HandleFunc("/plugins/functions", functionsHandler).Methods(http.MethodGet, http.MethodPost)
  46. r.HandleFunc("/plugins/functions/{name}", functionHandler).Methods(http.MethodDelete, http.MethodGet)
  47. r.HandleFunc("/plugins/functions/{name}/register", functionRegisterHandler).Methods(http.MethodPost)
  48. r.HandleFunc("/plugins/udfs", functionsListHandler).Methods(http.MethodGet)
  49. r.HandleFunc("/plugins/udfs/{name}", functionsGetHandler).Methods(http.MethodGet)
  50. }
  51. func pluginsHandler(w http.ResponseWriter, r *http.Request, t plugin.PluginType) {
  52. defer r.Body.Close()
  53. switch r.Method {
  54. case http.MethodGet:
  55. content := nativeManager.List(t)
  56. jsonResponse(content, w, logger)
  57. case http.MethodPost:
  58. sd := plugin.NewPluginByType(t)
  59. err := json.NewDecoder(r.Body).Decode(sd)
  60. // Problems decoding
  61. if err != nil {
  62. handleError(w, err, fmt.Sprintf("Invalid body: Error decoding the %s plugin json", plugin.PluginTypes[t]), logger)
  63. return
  64. }
  65. err = nativeManager.Register(t, sd)
  66. if err != nil {
  67. handleError(w, err, fmt.Sprintf("%s plugins create command error", plugin.PluginTypes[t]), logger)
  68. return
  69. }
  70. w.WriteHeader(http.StatusCreated)
  71. w.Write([]byte(fmt.Sprintf("%s plugin %s is created", plugin.PluginTypes[t], sd.GetName())))
  72. }
  73. }
  74. func pluginHandler(w http.ResponseWriter, r *http.Request, t plugin.PluginType) {
  75. defer r.Body.Close()
  76. vars := mux.Vars(r)
  77. name := vars["name"]
  78. cb := r.URL.Query().Get("stop")
  79. switch r.Method {
  80. case http.MethodDelete:
  81. r := cb == "1"
  82. err := nativeManager.Delete(t, name, r)
  83. if err != nil {
  84. handleError(w, err, fmt.Sprintf("delete %s plugin %s error", plugin.PluginTypes[t], name), logger)
  85. return
  86. }
  87. w.WriteHeader(http.StatusOK)
  88. result := fmt.Sprintf("%s plugin %s is deleted", plugin.PluginTypes[t], name)
  89. if r {
  90. result = fmt.Sprintf("%s and Kuiper will be stopped", result)
  91. } else {
  92. result = fmt.Sprintf("%s and Kuiper must restart for the change to take effect.", result)
  93. }
  94. w.Write([]byte(result))
  95. case http.MethodGet:
  96. j, ok := nativeManager.GetPluginInfo(t, name)
  97. if !ok {
  98. handleError(w, errorx.NewWithCode(errorx.NOT_FOUND, "not found"), fmt.Sprintf("describe %s plugin %s error", plugin.PluginTypes[t], name), logger)
  99. return
  100. }
  101. jsonResponse(j, w, logger)
  102. }
  103. }
  104. //list or create source plugin
  105. func sourcesHandler(w http.ResponseWriter, r *http.Request) {
  106. pluginsHandler(w, r, plugin.SOURCE)
  107. }
  108. //delete a source plugin
  109. func sourceHandler(w http.ResponseWriter, r *http.Request) {
  110. pluginHandler(w, r, plugin.SOURCE)
  111. }
  112. //list or create sink plugin
  113. func sinksHandler(w http.ResponseWriter, r *http.Request) {
  114. pluginsHandler(w, r, plugin.SINK)
  115. }
  116. //delete a sink plugin
  117. func sinkHandler(w http.ResponseWriter, r *http.Request) {
  118. pluginHandler(w, r, plugin.SINK)
  119. }
  120. //list or create function plugin
  121. func functionsHandler(w http.ResponseWriter, r *http.Request) {
  122. pluginsHandler(w, r, plugin.FUNCTION)
  123. }
  124. //list all user defined functions in all function plugins
  125. func functionsListHandler(w http.ResponseWriter, _ *http.Request) {
  126. content := nativeManager.ListSymbols()
  127. jsonResponse(content, w, logger)
  128. }
  129. func functionsGetHandler(w http.ResponseWriter, r *http.Request) {
  130. vars := mux.Vars(r)
  131. name := vars["name"]
  132. j, ok := nativeManager.GetPluginBySymbol(plugin.FUNCTION, name)
  133. if !ok {
  134. handleError(w, errorx.NewWithCode(errorx.NOT_FOUND, "not found"), fmt.Sprintf("describe function %s error", name), logger)
  135. return
  136. }
  137. jsonResponse(map[string]string{"name": name, "plugin": j}, w, logger)
  138. }
  139. //delete a function plugin
  140. func functionHandler(w http.ResponseWriter, r *http.Request) {
  141. pluginHandler(w, r, plugin.FUNCTION)
  142. }
  143. type functionList struct {
  144. Functions []string `json:"functions,omitempty"`
  145. }
  146. // register function list for function plugin. If a plugin exports multiple functions, the function list must be registered
  147. // either by create or register. If the function plugin has been loaded because of auto load through so file, the function
  148. // list MUST be registered by this API or only the function with the same name as the plugin can be used.
  149. func functionRegisterHandler(w http.ResponseWriter, r *http.Request) {
  150. defer r.Body.Close()
  151. vars := mux.Vars(r)
  152. name := vars["name"]
  153. _, ok := nativeManager.GetPluginInfo(plugin.FUNCTION, name)
  154. if !ok {
  155. handleError(w, errorx.NewWithCode(errorx.NOT_FOUND, "not found"), fmt.Sprintf("register %s plugin %s error", plugin.PluginTypes[plugin.FUNCTION], name), logger)
  156. return
  157. }
  158. sd := functionList{}
  159. err := json.NewDecoder(r.Body).Decode(&sd)
  160. // Problems decoding
  161. if err != nil {
  162. handleError(w, err, fmt.Sprintf("Invalid body: Error decoding the function list json %s", r.Body), logger)
  163. return
  164. }
  165. err = nativeManager.RegisterFuncs(name, sd.Functions)
  166. if err != nil {
  167. handleError(w, err, fmt.Sprintf("function plugins %s regiser functions error", name), logger)
  168. return
  169. }
  170. w.WriteHeader(http.StatusOK)
  171. w.Write([]byte(fmt.Sprintf("function plugin %s function list is registered", name)))
  172. }