wasm_init.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright erfenjiao, 630166475@qq.com.
  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 wasm
  15. // +build wasm
  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/wasm"
  24. "github.com/lf-edge/ekuiper/pkg/errorx"
  25. "net/http"
  26. )
  27. var wasmManager *wasm.Manager
  28. func init() {
  29. components["wasm"] = wasmComp{}
  30. }
  31. type wasmComp struct{}
  32. func (p wasmComp) register() {
  33. var err error
  34. wasmManager, err = wasm.InitManager()
  35. if err != nil {
  36. panic(err)
  37. }
  38. entries = append(entries, binder.FactoryEntry{Name: "wasm plugin", Factory: wasmManager, Weight: 8})
  39. }
  40. func (p wasmComp) rest(r *mux.Router) {
  41. r.HandleFunc("/plugins/wasm", wasmsHandler).Methods(http.MethodGet, http.MethodPost)
  42. r.HandleFunc("/plugins/wasm/{name}", wasmHandler).Methods(http.MethodGet, http.MethodDelete)
  43. }
  44. func wasmsHandler(w http.ResponseWriter, r *http.Request) {
  45. defer r.Body.Close()
  46. switch r.Method {
  47. case http.MethodGet:
  48. content := wasmManager.List()
  49. jsonResponse(content, w, logger)
  50. case http.MethodPost:
  51. sd := plugin.NewPluginByType(plugin.WASM)
  52. err := json.NewDecoder(r.Body).Decode(sd)
  53. // Problems decoding
  54. if err != nil {
  55. handleError(w, err, "Invalid body: Error decoding the wasm plugin json", logger)
  56. return
  57. }
  58. err = wasmManager.Register(sd)
  59. if err != nil {
  60. handleError(w, err, "wasm plugin create command error", logger)
  61. return
  62. }
  63. w.WriteHeader(http.StatusCreated)
  64. w.Write([]byte(fmt.Sprintf("wasm plugin %s is created", sd.GetName())))
  65. }
  66. }
  67. func wasmHandler(w http.ResponseWriter, r *http.Request) {
  68. defer r.Body.Close()
  69. vars := mux.Vars(r)
  70. name := vars["name"]
  71. switch r.Method {
  72. case http.MethodDelete:
  73. err := wasmManager.Delete(name)
  74. if err != nil {
  75. handleError(w, err, fmt.Sprintf("delete wasm plugin %s error", name), logger)
  76. return
  77. }
  78. w.WriteHeader(http.StatusOK)
  79. result := fmt.Sprintf("wasm plugin %s is deleted", name)
  80. w.Write([]byte(result))
  81. case http.MethodGet:
  82. j, ok := wasmManager.GetPluginInfo(name)
  83. if !ok {
  84. handleError(w, errorx.NewWithCode(errorx.NOT_FOUND, "not found"), fmt.Sprintf("describe wasm plugin %s error", name), logger)
  85. return
  86. }
  87. jsonResponse(j, w, logger)
  88. }
  89. }