rest.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. // Copyright 2021-2023 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. package server
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "net/http"
  22. "os"
  23. "path/filepath"
  24. "reflect"
  25. "runtime"
  26. "strconv"
  27. "time"
  28. "github.com/gorilla/handlers"
  29. "github.com/gorilla/mux"
  30. "golang.org/x/text/cases"
  31. "golang.org/x/text/language"
  32. "github.com/lf-edge/ekuiper/internal/conf"
  33. "github.com/lf-edge/ekuiper/internal/meta"
  34. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  35. "github.com/lf-edge/ekuiper/internal/processor"
  36. "github.com/lf-edge/ekuiper/internal/server/middleware"
  37. "github.com/lf-edge/ekuiper/pkg/api"
  38. "github.com/lf-edge/ekuiper/pkg/ast"
  39. "github.com/lf-edge/ekuiper/pkg/cast"
  40. "github.com/lf-edge/ekuiper/pkg/errorx"
  41. "github.com/lf-edge/ekuiper/pkg/infra"
  42. )
  43. const (
  44. ContentType = "Content-Type"
  45. ContentTypeJSON = "application/json"
  46. )
  47. var uploadDir string
  48. type statementDescriptor struct {
  49. Sql string `json:"sql,omitempty"`
  50. }
  51. func decodeStatementDescriptor(reader io.ReadCloser) (statementDescriptor, error) {
  52. sd := statementDescriptor{}
  53. err := json.NewDecoder(reader).Decode(&sd)
  54. // Problems decoding
  55. if err != nil {
  56. return sd, fmt.Errorf("Error decoding the statement descriptor: %v", err)
  57. }
  58. return sd, nil
  59. }
  60. // Handle applies the specified error and error concept to the HTTP response writer
  61. func handleError(w http.ResponseWriter, err error, prefix string, logger api.Logger) {
  62. message := prefix
  63. if message != "" {
  64. message += ": "
  65. }
  66. message += err.Error()
  67. logger.Error(message)
  68. var ec int
  69. switch e := err.(type) {
  70. case *errorx.Error:
  71. switch e.Code() {
  72. case errorx.NOT_FOUND:
  73. ec = http.StatusNotFound
  74. default:
  75. ec = http.StatusBadRequest
  76. }
  77. default:
  78. ec = http.StatusBadRequest
  79. }
  80. http.Error(w, message, ec)
  81. }
  82. func jsonResponse(i interface{}, w http.ResponseWriter, logger api.Logger) {
  83. w.Header().Add(ContentType, ContentTypeJSON)
  84. jsonByte, err := json.Marshal(i)
  85. if err != nil {
  86. handleError(w, err, "", logger)
  87. }
  88. w.Header().Add("Content-Length", strconv.Itoa(len(jsonByte)))
  89. _, err = w.Write(jsonByte)
  90. // Problems encoding
  91. if err != nil {
  92. handleError(w, err, "", logger)
  93. }
  94. }
  95. func jsonByteResponse(buffer bytes.Buffer, w http.ResponseWriter, logger api.Logger) {
  96. w.Header().Add(ContentType, ContentTypeJSON)
  97. w.Header().Add("Content-Length", strconv.Itoa(buffer.Len()))
  98. _, err := w.Write(buffer.Bytes())
  99. // Problems encoding
  100. if err != nil {
  101. handleError(w, err, "", logger)
  102. }
  103. }
  104. func createRestServer(ip string, port int, needToken bool) *http.Server {
  105. dataDir, err := conf.GetDataLoc()
  106. if err != nil {
  107. panic(err)
  108. }
  109. uploadDir = filepath.Join(dataDir, "uploads")
  110. r := mux.NewRouter()
  111. r.HandleFunc("/", rootHandler).Methods(http.MethodGet, http.MethodPost)
  112. r.HandleFunc("/ping", pingHandler).Methods(http.MethodGet)
  113. r.HandleFunc("/streams", streamsHandler).Methods(http.MethodGet, http.MethodPost)
  114. r.HandleFunc("/streams/{name}", streamHandler).Methods(http.MethodGet, http.MethodDelete, http.MethodPut)
  115. r.HandleFunc("/streams/{name}/schema", streamSchemaHandler).Methods(http.MethodGet)
  116. r.HandleFunc("/tables", tablesHandler).Methods(http.MethodGet, http.MethodPost)
  117. r.HandleFunc("/tables/{name}", tableHandler).Methods(http.MethodGet, http.MethodDelete, http.MethodPut)
  118. r.HandleFunc("/tables/{name}/schema", tableSchemaHandler).Methods(http.MethodGet)
  119. r.HandleFunc("/rules", rulesHandler).Methods(http.MethodGet, http.MethodPost)
  120. r.HandleFunc("/rules/{name}", ruleHandler).Methods(http.MethodDelete, http.MethodGet, http.MethodPut)
  121. r.HandleFunc("/rules/{name}/status", getStatusRuleHandler).Methods(http.MethodGet)
  122. r.HandleFunc("/rules/{name}/start", startRuleHandler).Methods(http.MethodPost)
  123. r.HandleFunc("/rules/{name}/stop", stopRuleHandler).Methods(http.MethodPost)
  124. r.HandleFunc("/rules/{name}/restart", restartRuleHandler).Methods(http.MethodPost)
  125. r.HandleFunc("/rules/{name}/topo", getTopoRuleHandler).Methods(http.MethodGet)
  126. r.HandleFunc("/ruleset/export", exportHandler).Methods(http.MethodPost)
  127. r.HandleFunc("/ruleset/import", importHandler).Methods(http.MethodPost)
  128. r.HandleFunc("/config/uploads", fileUploadHandler).Methods(http.MethodPost, http.MethodGet)
  129. r.HandleFunc("/config/uploads/{name}", fileDeleteHandler).Methods(http.MethodDelete)
  130. r.HandleFunc("/data/export", configurationExportHandler).Methods(http.MethodGet, http.MethodPost)
  131. r.HandleFunc("/data/import", configurationImportHandler).Methods(http.MethodPost)
  132. r.HandleFunc("/data/import/status", configurationStatusHandler).Methods(http.MethodGet)
  133. // Register extended routes
  134. for k, v := range components {
  135. logger.Infof("register rest endpoint for component %s", k)
  136. v.rest(r)
  137. }
  138. if needToken {
  139. r.Use(middleware.Auth)
  140. }
  141. server := &http.Server{
  142. Addr: cast.JoinHostPortInt(ip, port),
  143. // Good practice to set timeouts to avoid Slowloris attacks.
  144. WriteTimeout: time.Second * 60 * 5,
  145. ReadTimeout: time.Second * 60 * 5,
  146. IdleTimeout: time.Second * 60,
  147. Handler: handlers.CORS(handlers.AllowedHeaders([]string{"Accept", "Accept-Language", "Content-Type", "Content-Language", "Origin", "Authorization"}), handlers.AllowedMethods([]string{"POST", "GET", "PUT", "DELETE", "HEAD"}))(r),
  148. }
  149. server.SetKeepAlivesEnabled(false)
  150. return server
  151. }
  152. type fileContent struct {
  153. Name string `json:"name"`
  154. Content string `json:"content"`
  155. FilePath string `json:"file"`
  156. }
  157. func (f *fileContent) Validate() error {
  158. if f.Content == "" && f.FilePath == "" {
  159. return fmt.Errorf("invalid body: content or FilePath is required")
  160. }
  161. if f.Name == "" {
  162. return fmt.Errorf("invalid body: name is required")
  163. }
  164. return nil
  165. }
  166. func upload(file *fileContent) error {
  167. err := getFile(file)
  168. if err != nil {
  169. return err
  170. }
  171. return nil
  172. }
  173. func getFile(file *fileContent) error {
  174. filePath := filepath.Join(uploadDir, file.Name)
  175. dst, err := os.Create(filePath)
  176. if err != nil {
  177. return err
  178. }
  179. defer dst.Close()
  180. if file.FilePath != "" {
  181. err := httpx.DownloadFile(filePath, file.FilePath)
  182. if err != nil {
  183. return err
  184. }
  185. } else {
  186. _, err := dst.Write([]byte(file.Content))
  187. if err != nil {
  188. return err
  189. }
  190. }
  191. return nil
  192. }
  193. func fileUploadHandler(w http.ResponseWriter, r *http.Request) {
  194. switch r.Method {
  195. // Upload or overwrite a file
  196. case http.MethodPost:
  197. switch r.Header.Get("Content-Type") {
  198. case "application/json":
  199. fc := &fileContent{}
  200. defer r.Body.Close()
  201. err := json.NewDecoder(r.Body).Decode(fc)
  202. if err != nil {
  203. handleError(w, err, "Invalid body: Error decoding file json", logger)
  204. return
  205. }
  206. err = fc.Validate()
  207. if err != nil {
  208. handleError(w, err, "Invalid body: missing necessary field", logger)
  209. return
  210. }
  211. filePath := filepath.Join(uploadDir, fc.Name)
  212. err = upload(fc)
  213. if err != nil {
  214. handleError(w, err, "Upload error: getFile has error", logger)
  215. return
  216. }
  217. w.WriteHeader(http.StatusCreated)
  218. w.Write([]byte(filePath))
  219. default:
  220. // Maximum upload of 1 GB files
  221. err := r.ParseMultipartForm(1024 << 20)
  222. if err != nil {
  223. handleError(w, err, "Error parse the multi part form", logger)
  224. return
  225. }
  226. // Get handler for filename, size and headers
  227. file, handler, err := r.FormFile("uploadFile")
  228. if err != nil {
  229. handleError(w, err, "Error Retrieving the File", logger)
  230. return
  231. }
  232. defer file.Close()
  233. // Create file
  234. filePath := filepath.Join(uploadDir, handler.Filename)
  235. dst, err := os.Create(filePath)
  236. defer dst.Close()
  237. if err != nil {
  238. handleError(w, err, "Error creating the file", logger)
  239. return
  240. }
  241. // Copy the uploaded file to the created file on the filesystem
  242. if _, err := io.Copy(dst, file); err != nil {
  243. handleError(w, err, "Error writing the file", logger)
  244. return
  245. }
  246. w.WriteHeader(http.StatusCreated)
  247. w.Write([]byte(filePath))
  248. }
  249. case http.MethodGet:
  250. // Get the list of files in the upload directory
  251. files, err := os.ReadDir(uploadDir)
  252. if err != nil {
  253. handleError(w, err, "Error reading the file upload dir", logger)
  254. return
  255. }
  256. fileNames := make([]string, len(files))
  257. for i, f := range files {
  258. fileNames[i] = filepath.Join(uploadDir, f.Name())
  259. }
  260. jsonResponse(fileNames, w, logger)
  261. }
  262. }
  263. func fileDeleteHandler(w http.ResponseWriter, r *http.Request) {
  264. vars := mux.Vars(r)
  265. name := vars["name"]
  266. filePath := filepath.Join(uploadDir, name)
  267. e := os.Remove(filePath)
  268. if e != nil {
  269. handleError(w, e, "Error deleting the file", logger)
  270. return
  271. }
  272. w.WriteHeader(http.StatusOK)
  273. w.Write([]byte("ok"))
  274. }
  275. type information struct {
  276. Version string `json:"version"`
  277. Os string `json:"os"`
  278. Arch string `json:"arch"`
  279. UpTimeSeconds int64 `json:"upTimeSeconds"`
  280. }
  281. // The handler for root
  282. func rootHandler(w http.ResponseWriter, r *http.Request) {
  283. defer r.Body.Close()
  284. switch r.Method {
  285. case http.MethodGet, http.MethodPost:
  286. w.WriteHeader(http.StatusOK)
  287. info := new(information)
  288. info.Version = version
  289. info.UpTimeSeconds = time.Now().Unix() - startTimeStamp
  290. info.Os = runtime.GOOS
  291. info.Arch = runtime.GOARCH
  292. byteInfo, _ := json.Marshal(info)
  293. w.Write(byteInfo)
  294. }
  295. }
  296. func pingHandler(w http.ResponseWriter, _ *http.Request) {
  297. w.WriteHeader(http.StatusOK)
  298. }
  299. func sourcesManageHandler(w http.ResponseWriter, r *http.Request, st ast.StreamType) {
  300. defer r.Body.Close()
  301. switch r.Method {
  302. case http.MethodGet:
  303. var (
  304. content []string
  305. err error
  306. kind string
  307. )
  308. if st == ast.TypeTable {
  309. kind = r.URL.Query().Get("kind")
  310. if kind == "scan" {
  311. kind = ast.StreamKindScan
  312. } else if kind == "lookup" {
  313. kind = ast.StreamKindLookup
  314. } else {
  315. kind = ""
  316. }
  317. }
  318. if kind != "" {
  319. content, err = streamProcessor.ShowTable(kind)
  320. } else {
  321. content, err = streamProcessor.ShowStream(st)
  322. }
  323. if err != nil {
  324. handleError(w, err, fmt.Sprintf("%s command error", cases.Title(language.Und).String(ast.StreamTypeMap[st])), logger)
  325. return
  326. }
  327. jsonResponse(content, w, logger)
  328. case http.MethodPost:
  329. v, err := decodeStatementDescriptor(r.Body)
  330. if err != nil {
  331. handleError(w, err, "Invalid body", logger)
  332. return
  333. }
  334. content, err := streamProcessor.ExecStreamSql(v.Sql)
  335. if err != nil {
  336. handleError(w, err, fmt.Sprintf("%s command error", cases.Title(language.Und).String(ast.StreamTypeMap[st])), logger)
  337. return
  338. }
  339. w.WriteHeader(http.StatusCreated)
  340. w.Write([]byte(content))
  341. }
  342. }
  343. func sourceManageHandler(w http.ResponseWriter, r *http.Request, st ast.StreamType) {
  344. defer r.Body.Close()
  345. vars := mux.Vars(r)
  346. name := vars["name"]
  347. switch r.Method {
  348. case http.MethodGet:
  349. content, err := streamProcessor.DescStream(name, st)
  350. if err != nil {
  351. handleError(w, err, fmt.Sprintf("describe %s error", ast.StreamTypeMap[st]), logger)
  352. return
  353. }
  354. jsonResponse(content, w, logger)
  355. case http.MethodDelete:
  356. content, err := streamProcessor.DropStream(name, st)
  357. if err != nil {
  358. handleError(w, err, fmt.Sprintf("delete %s error", ast.StreamTypeMap[st]), logger)
  359. return
  360. }
  361. w.WriteHeader(http.StatusOK)
  362. w.Write([]byte(content))
  363. case http.MethodPut:
  364. v, err := decodeStatementDescriptor(r.Body)
  365. if err != nil {
  366. handleError(w, err, "Invalid body", logger)
  367. return
  368. }
  369. content, err := streamProcessor.ExecReplaceStream(name, v.Sql, st)
  370. if err != nil {
  371. handleError(w, err, fmt.Sprintf("%s command error", cases.Title(language.Und).String(ast.StreamTypeMap[st])), logger)
  372. return
  373. }
  374. w.WriteHeader(http.StatusOK)
  375. w.Write([]byte(content))
  376. }
  377. }
  378. // list or create streams
  379. func streamsHandler(w http.ResponseWriter, r *http.Request) {
  380. sourcesManageHandler(w, r, ast.TypeStream)
  381. }
  382. // describe or delete a stream
  383. func streamHandler(w http.ResponseWriter, r *http.Request) {
  384. sourceManageHandler(w, r, ast.TypeStream)
  385. }
  386. // list or create tables
  387. func tablesHandler(w http.ResponseWriter, r *http.Request) {
  388. sourcesManageHandler(w, r, ast.TypeTable)
  389. }
  390. func tableHandler(w http.ResponseWriter, r *http.Request) {
  391. sourceManageHandler(w, r, ast.TypeTable)
  392. }
  393. func streamSchemaHandler(w http.ResponseWriter, r *http.Request) {
  394. sourceSchemaHandler(w, r, ast.TypeStream)
  395. }
  396. func tableSchemaHandler(w http.ResponseWriter, r *http.Request) {
  397. sourceSchemaHandler(w, r, ast.TypeTable)
  398. }
  399. func sourceSchemaHandler(w http.ResponseWriter, r *http.Request, st ast.StreamType) {
  400. vars := mux.Vars(r)
  401. name := vars["name"]
  402. content, err := streamProcessor.GetInferredJsonSchema(name, st)
  403. if err != nil {
  404. handleError(w, err, fmt.Sprintf("get schema of %s error", ast.StreamTypeMap[st]), logger)
  405. return
  406. }
  407. jsonResponse(content, w, logger)
  408. }
  409. // list or create rules
  410. func rulesHandler(w http.ResponseWriter, r *http.Request) {
  411. defer r.Body.Close()
  412. switch r.Method {
  413. case http.MethodPost:
  414. body, err := io.ReadAll(r.Body)
  415. if err != nil {
  416. handleError(w, err, "Invalid body", logger)
  417. return
  418. }
  419. id, err := createRule("", string(body))
  420. if err != nil {
  421. handleError(w, err, "", logger)
  422. return
  423. }
  424. result := fmt.Sprintf("Rule %s was created successfully.", id)
  425. w.WriteHeader(http.StatusCreated)
  426. w.Write([]byte(result))
  427. case http.MethodGet:
  428. content, err := getAllRulesWithStatus()
  429. if err != nil {
  430. handleError(w, err, "Show rules error", logger)
  431. return
  432. }
  433. jsonResponse(content, w, logger)
  434. }
  435. }
  436. // describe or delete a rule
  437. func ruleHandler(w http.ResponseWriter, r *http.Request) {
  438. defer r.Body.Close()
  439. vars := mux.Vars(r)
  440. name := vars["name"]
  441. switch r.Method {
  442. case http.MethodGet:
  443. rule, err := ruleProcessor.GetRuleJson(name)
  444. if err != nil {
  445. handleError(w, err, "Describe rule error", logger)
  446. return
  447. }
  448. w.Header().Add(ContentType, ContentTypeJSON)
  449. w.Write([]byte(rule))
  450. case http.MethodDelete:
  451. deleteRule(name)
  452. content, err := ruleProcessor.ExecDrop(name)
  453. if err != nil {
  454. handleError(w, err, "Delete rule error", logger)
  455. return
  456. }
  457. w.WriteHeader(http.StatusOK)
  458. w.Write([]byte(content))
  459. case http.MethodPut:
  460. _, err := ruleProcessor.GetRuleById(name)
  461. if err != nil {
  462. handleError(w, err, "Rule not found", logger)
  463. return
  464. }
  465. body, err := io.ReadAll(r.Body)
  466. if err != nil {
  467. handleError(w, err, "Invalid body", logger)
  468. return
  469. }
  470. err = updateRule(name, string(body))
  471. if err != nil {
  472. handleError(w, err, "Update rule error", logger)
  473. return
  474. }
  475. // Update to db after validation
  476. _, err = ruleProcessor.ExecUpdate(name, string(body))
  477. var result string
  478. if err != nil {
  479. handleError(w, err, "Update rule error, suggest to delete it and recreate", logger)
  480. return
  481. } else {
  482. result = fmt.Sprintf("Rule %s was updated successfully.", name)
  483. }
  484. w.WriteHeader(http.StatusOK)
  485. w.Write([]byte(result))
  486. }
  487. }
  488. // get status of a rule
  489. func getStatusRuleHandler(w http.ResponseWriter, r *http.Request) {
  490. defer r.Body.Close()
  491. vars := mux.Vars(r)
  492. name := vars["name"]
  493. content, err := getRuleStatus(name)
  494. if err != nil {
  495. handleError(w, err, "get rule status error", logger)
  496. return
  497. }
  498. w.Header().Set(ContentType, ContentTypeJSON)
  499. w.WriteHeader(http.StatusOK)
  500. w.Write([]byte(content))
  501. }
  502. // start a rule
  503. func startRuleHandler(w http.ResponseWriter, r *http.Request) {
  504. defer r.Body.Close()
  505. vars := mux.Vars(r)
  506. name := vars["name"]
  507. err := startRule(name)
  508. if err != nil {
  509. handleError(w, err, "start rule error", logger)
  510. return
  511. }
  512. w.WriteHeader(http.StatusOK)
  513. w.Write([]byte(fmt.Sprintf("Rule %s was started", name)))
  514. }
  515. // stop a rule
  516. func stopRuleHandler(w http.ResponseWriter, r *http.Request) {
  517. defer r.Body.Close()
  518. vars := mux.Vars(r)
  519. name := vars["name"]
  520. result := stopRule(name)
  521. w.WriteHeader(http.StatusOK)
  522. w.Write([]byte(result))
  523. }
  524. // restart a rule
  525. func restartRuleHandler(w http.ResponseWriter, r *http.Request) {
  526. defer r.Body.Close()
  527. vars := mux.Vars(r)
  528. name := vars["name"]
  529. err := restartRule(name)
  530. if err != nil {
  531. handleError(w, err, "restart rule error", logger)
  532. return
  533. }
  534. w.WriteHeader(http.StatusOK)
  535. w.Write([]byte(fmt.Sprintf("Rule %s was restarted", name)))
  536. }
  537. // get topo of a rule
  538. func getTopoRuleHandler(w http.ResponseWriter, r *http.Request) {
  539. defer r.Body.Close()
  540. vars := mux.Vars(r)
  541. name := vars["name"]
  542. content, err := getRuleTopo(name)
  543. if err != nil {
  544. handleError(w, err, "get rule topo error", logger)
  545. return
  546. }
  547. w.Header().Set(ContentType, ContentTypeJSON)
  548. w.Write([]byte(content))
  549. }
  550. type rulesetInfo struct {
  551. Content string `json:"content"`
  552. FilePath string `json:"file"`
  553. }
  554. func importHandler(w http.ResponseWriter, r *http.Request) {
  555. rsi := &rulesetInfo{}
  556. err := json.NewDecoder(r.Body).Decode(rsi)
  557. if err != nil {
  558. handleError(w, err, "Invalid body: Error decoding json", logger)
  559. return
  560. }
  561. if rsi.Content != "" && rsi.FilePath != "" {
  562. handleError(w, errors.New("bad request"), "Invalid body: Cannot specify both content and file", logger)
  563. return
  564. } else if rsi.Content == "" && rsi.FilePath == "" {
  565. handleError(w, errors.New("bad request"), "Invalid body: must specify content or file", logger)
  566. return
  567. }
  568. content := []byte(rsi.Content)
  569. if rsi.FilePath != "" {
  570. reader, err := httpx.ReadFile(rsi.FilePath)
  571. if err != nil {
  572. handleError(w, err, "Fail to read file", logger)
  573. return
  574. }
  575. defer reader.Close()
  576. buf := new(bytes.Buffer)
  577. _, err = io.Copy(buf, reader)
  578. if err != nil {
  579. handleError(w, err, "fail to convert file", logger)
  580. return
  581. }
  582. content = buf.Bytes()
  583. }
  584. rules, counts, err := rulesetProcessor.Import(content)
  585. if err != nil {
  586. handleError(w, nil, "Import ruleset error", logger)
  587. return
  588. }
  589. infra.SafeRun(func() error {
  590. for _, name := range rules {
  591. rul, ee := ruleProcessor.GetRuleById(name)
  592. if ee != nil {
  593. logger.Error(ee)
  594. continue
  595. }
  596. reply := recoverRule(rul)
  597. if reply != "" {
  598. logger.Error(reply)
  599. }
  600. }
  601. return nil
  602. })
  603. w.Write([]byte(fmt.Sprintf("imported %d streams, %d tables and %d rules", counts[0], counts[1], counts[2])))
  604. }
  605. func exportHandler(w http.ResponseWriter, r *http.Request) {
  606. const name = "ekuiper_export.json"
  607. exported, _, err := rulesetProcessor.Export()
  608. if err != nil {
  609. handleError(w, err, "export error", logger)
  610. return
  611. }
  612. w.Header().Set("Content-Type", "application/octet-stream")
  613. w.Header().Add("Content-Disposition", "Attachment")
  614. http.ServeContent(w, r, name, time.Now(), exported)
  615. }
  616. type Configuration struct {
  617. Streams map[string]string `json:"streams"`
  618. Tables map[string]string `json:"tables"`
  619. Rules map[string]string `json:"rules"`
  620. NativePlugins map[string]string `json:"nativePlugins"`
  621. PortablePlugins map[string]string `json:"portablePlugins"`
  622. SourceConfig map[string]string `json:"sourceConfig"`
  623. SinkConfig map[string]string `json:"sinkConfig"`
  624. ConnectionConfig map[string]string `json:"connectionConfig"`
  625. Service map[string]string `json:"Service"`
  626. Schema map[string]string `json:"Schema"`
  627. }
  628. func configurationExport() ([]byte, error) {
  629. conf := &Configuration{
  630. Streams: make(map[string]string),
  631. Tables: make(map[string]string),
  632. Rules: make(map[string]string),
  633. NativePlugins: make(map[string]string),
  634. PortablePlugins: make(map[string]string),
  635. SourceConfig: make(map[string]string),
  636. SinkConfig: make(map[string]string),
  637. ConnectionConfig: make(map[string]string),
  638. Service: make(map[string]string),
  639. Schema: make(map[string]string),
  640. }
  641. ruleSet := rulesetProcessor.ExportRuleSet()
  642. if ruleSet != nil {
  643. conf.Streams = ruleSet.Streams
  644. conf.Tables = ruleSet.Tables
  645. conf.Rules = ruleSet.Rules
  646. }
  647. conf.NativePlugins = pluginExport()
  648. conf.PortablePlugins = portablePluginExport()
  649. conf.Service = serviceExport()
  650. conf.Schema = schemaExport()
  651. yamlCfg := meta.GetConfigurations()
  652. conf.SourceConfig = yamlCfg.Sources
  653. conf.SinkConfig = yamlCfg.Sinks
  654. conf.ConnectionConfig = yamlCfg.Connections
  655. return json.Marshal(conf)
  656. }
  657. func configurationExportHandler(w http.ResponseWriter, r *http.Request) {
  658. var jsonBytes []byte
  659. const name = "ekuiper_export.json"
  660. switch r.Method {
  661. case http.MethodGet:
  662. jsonBytes, _ = configurationExport()
  663. case http.MethodPost:
  664. var rules []string
  665. _ = json.NewDecoder(r.Body).Decode(&rules)
  666. jsonBytes, _ = ruleMigrationProcessor.ConfigurationPartialExport(rules)
  667. }
  668. w.Header().Set("Content-Type", "application/octet-stream")
  669. w.Header().Add("Content-Disposition", "Attachment")
  670. http.ServeContent(w, r, name, time.Now(), bytes.NewReader(jsonBytes))
  671. }
  672. func configurationReset() {
  673. _ = resetAllRules()
  674. _ = resetAllStreams()
  675. pluginReset()
  676. portablePluginsReset()
  677. serviceReset()
  678. schemaReset()
  679. meta.ResetConfigs()
  680. }
  681. type ImportConfigurationStatus struct {
  682. ErrorMsg string
  683. ConfigResponse Configuration
  684. }
  685. func configurationImport(data []byte, reboot bool) ImportConfigurationStatus {
  686. conf := &Configuration{
  687. Streams: make(map[string]string),
  688. Tables: make(map[string]string),
  689. Rules: make(map[string]string),
  690. NativePlugins: make(map[string]string),
  691. PortablePlugins: make(map[string]string),
  692. SourceConfig: make(map[string]string),
  693. SinkConfig: make(map[string]string),
  694. ConnectionConfig: make(map[string]string),
  695. Service: make(map[string]string),
  696. Schema: make(map[string]string),
  697. }
  698. importStatus := ImportConfigurationStatus{}
  699. configResponse := Configuration{
  700. Streams: make(map[string]string),
  701. Tables: make(map[string]string),
  702. Rules: make(map[string]string),
  703. NativePlugins: make(map[string]string),
  704. PortablePlugins: make(map[string]string),
  705. SourceConfig: make(map[string]string),
  706. SinkConfig: make(map[string]string),
  707. ConnectionConfig: make(map[string]string),
  708. Service: make(map[string]string),
  709. Schema: make(map[string]string),
  710. }
  711. ResponseNil := Configuration{
  712. Streams: make(map[string]string),
  713. Tables: make(map[string]string),
  714. Rules: make(map[string]string),
  715. NativePlugins: make(map[string]string),
  716. PortablePlugins: make(map[string]string),
  717. SourceConfig: make(map[string]string),
  718. SinkConfig: make(map[string]string),
  719. ConnectionConfig: make(map[string]string),
  720. Service: make(map[string]string),
  721. Schema: make(map[string]string),
  722. }
  723. err := json.Unmarshal(data, conf)
  724. if err != nil {
  725. importStatus.ErrorMsg = fmt.Errorf("configuration unmarshal with error %v", err).Error()
  726. return importStatus
  727. }
  728. if reboot {
  729. err = pluginImport(conf.NativePlugins)
  730. if err != nil {
  731. importStatus.ErrorMsg = fmt.Errorf("pluginImport NativePlugins import error %v", err).Error()
  732. return importStatus
  733. }
  734. err = schemaImport(conf.Schema)
  735. if err != nil {
  736. importStatus.ErrorMsg = fmt.Errorf("schemaImport Schema import error %v", err).Error()
  737. return importStatus
  738. }
  739. }
  740. configResponse.PortablePlugins = portablePluginImport(conf.PortablePlugins)
  741. configResponse.Service = serviceImport(conf.Service)
  742. yamlCfgSet := meta.YamlConfigurationSet{
  743. Sources: conf.SourceConfig,
  744. Sinks: conf.SinkConfig,
  745. Connections: conf.ConnectionConfig,
  746. }
  747. confRsp := meta.LoadConfigurations(yamlCfgSet)
  748. configResponse.SourceConfig = confRsp.Sources
  749. configResponse.SinkConfig = confRsp.Sinks
  750. configResponse.ConnectionConfig = confRsp.Connections
  751. ruleSet := processor.Ruleset{
  752. Streams: conf.Streams,
  753. Tables: conf.Tables,
  754. Rules: conf.Rules,
  755. }
  756. result := rulesetProcessor.ImportRuleSet(ruleSet)
  757. configResponse.Streams = result.Streams
  758. configResponse.Tables = result.Tables
  759. configResponse.Rules = result.Rules
  760. if !reboot {
  761. infra.SafeRun(func() error {
  762. for name := range ruleSet.Rules {
  763. rul, ee := ruleProcessor.GetRuleById(name)
  764. if ee != nil {
  765. logger.Error(ee)
  766. continue
  767. }
  768. reply := recoverRule(rul)
  769. if reply != "" {
  770. logger.Error(reply)
  771. }
  772. }
  773. return nil
  774. })
  775. }
  776. if reflect.DeepEqual(ResponseNil, configResponse) {
  777. importStatus.ConfigResponse = ResponseNil
  778. } else {
  779. importStatus.ErrorMsg = "process error"
  780. importStatus.ConfigResponse = configResponse
  781. }
  782. return importStatus
  783. }
  784. func configurationPartialImport(data []byte) ImportConfigurationStatus {
  785. conf := &Configuration{
  786. Streams: make(map[string]string),
  787. Tables: make(map[string]string),
  788. Rules: make(map[string]string),
  789. NativePlugins: make(map[string]string),
  790. PortablePlugins: make(map[string]string),
  791. SourceConfig: make(map[string]string),
  792. SinkConfig: make(map[string]string),
  793. ConnectionConfig: make(map[string]string),
  794. Service: make(map[string]string),
  795. Schema: make(map[string]string),
  796. }
  797. importStatus := ImportConfigurationStatus{}
  798. configResponse := Configuration{
  799. Streams: make(map[string]string),
  800. Tables: make(map[string]string),
  801. Rules: make(map[string]string),
  802. NativePlugins: make(map[string]string),
  803. PortablePlugins: make(map[string]string),
  804. SourceConfig: make(map[string]string),
  805. SinkConfig: make(map[string]string),
  806. ConnectionConfig: make(map[string]string),
  807. Service: make(map[string]string),
  808. Schema: make(map[string]string),
  809. }
  810. ResponseNil := Configuration{
  811. Streams: make(map[string]string),
  812. Tables: make(map[string]string),
  813. Rules: make(map[string]string),
  814. NativePlugins: make(map[string]string),
  815. PortablePlugins: make(map[string]string),
  816. SourceConfig: make(map[string]string),
  817. SinkConfig: make(map[string]string),
  818. ConnectionConfig: make(map[string]string),
  819. Service: make(map[string]string),
  820. Schema: make(map[string]string),
  821. }
  822. err := json.Unmarshal(data, conf)
  823. if err != nil {
  824. importStatus.ErrorMsg = fmt.Errorf("configuration unmarshal with error %v", err).Error()
  825. return importStatus
  826. }
  827. yamlCfgSet := meta.YamlConfigurationSet{
  828. Sources: conf.SourceConfig,
  829. Sinks: conf.SinkConfig,
  830. Connections: conf.ConnectionConfig,
  831. }
  832. confRsp := meta.LoadConfigurationsPartial(yamlCfgSet)
  833. configResponse.NativePlugins = pluginPartialImport(conf.NativePlugins)
  834. configResponse.Schema = schemaPartialImport(conf.Schema)
  835. configResponse.PortablePlugins = portablePluginPartialImport(conf.PortablePlugins)
  836. configResponse.Service = servicePartialImport(conf.Service)
  837. configResponse.SourceConfig = confRsp.Sources
  838. configResponse.SinkConfig = confRsp.Sinks
  839. configResponse.ConnectionConfig = confRsp.Connections
  840. ruleSet := processor.Ruleset{
  841. Streams: conf.Streams,
  842. Tables: conf.Tables,
  843. Rules: conf.Rules,
  844. }
  845. result := importRuleSetPartial(ruleSet)
  846. configResponse.Streams = result.Streams
  847. configResponse.Tables = result.Tables
  848. configResponse.Rules = result.Rules
  849. if reflect.DeepEqual(ResponseNil, configResponse) {
  850. importStatus.ConfigResponse = ResponseNil
  851. } else {
  852. importStatus.ErrorMsg = "process error"
  853. importStatus.ConfigResponse = configResponse
  854. }
  855. return importStatus
  856. }
  857. type configurationInfo struct {
  858. Content string `json:"content"`
  859. FilePath string `json:"file"`
  860. }
  861. func configurationImportHandler(w http.ResponseWriter, r *http.Request) {
  862. cb := r.URL.Query().Get("stop")
  863. stop := cb == "1"
  864. par := r.URL.Query().Get("partial")
  865. partial := par == "1"
  866. rsi := &configurationInfo{}
  867. err := json.NewDecoder(r.Body).Decode(rsi)
  868. if err != nil {
  869. handleError(w, err, "Invalid body: Error decoding json", logger)
  870. return
  871. }
  872. if rsi.Content != "" && rsi.FilePath != "" {
  873. handleError(w, errors.New("bad request"), "Invalid body: Cannot specify both content and file", logger)
  874. return
  875. } else if rsi.Content == "" && rsi.FilePath == "" {
  876. handleError(w, errors.New("bad request"), "Invalid body: must specify content or file", logger)
  877. return
  878. }
  879. content := []byte(rsi.Content)
  880. if rsi.FilePath != "" {
  881. reader, err := httpx.ReadFile(rsi.FilePath)
  882. if err != nil {
  883. handleError(w, err, "Fail to read file", logger)
  884. return
  885. }
  886. defer reader.Close()
  887. buf := new(bytes.Buffer)
  888. _, err = io.Copy(buf, reader)
  889. if err != nil {
  890. handleError(w, err, "fail to convert file", logger)
  891. return
  892. }
  893. content = buf.Bytes()
  894. }
  895. if !partial {
  896. configurationReset()
  897. result := configurationImport(content, stop)
  898. if result.ErrorMsg != "" {
  899. w.WriteHeader(http.StatusBadRequest)
  900. jsonResponse(result, w, logger)
  901. } else {
  902. w.WriteHeader(http.StatusOK)
  903. jsonResponse(result, w, logger)
  904. }
  905. if stop {
  906. go func() {
  907. time.Sleep(1 * time.Second)
  908. os.Exit(100)
  909. }()
  910. }
  911. } else {
  912. result := configurationPartialImport(content)
  913. if result.ErrorMsg != "" {
  914. w.WriteHeader(http.StatusBadRequest)
  915. jsonResponse(result, w, logger)
  916. } else {
  917. w.WriteHeader(http.StatusOK)
  918. jsonResponse(result, w, logger)
  919. }
  920. }
  921. }
  922. func configurationStatusExport() Configuration {
  923. conf := Configuration{
  924. Streams: make(map[string]string),
  925. Tables: make(map[string]string),
  926. Rules: make(map[string]string),
  927. NativePlugins: make(map[string]string),
  928. PortablePlugins: make(map[string]string),
  929. SourceConfig: make(map[string]string),
  930. SinkConfig: make(map[string]string),
  931. ConnectionConfig: make(map[string]string),
  932. Service: make(map[string]string),
  933. Schema: make(map[string]string),
  934. }
  935. ruleSet := rulesetProcessor.ExportRuleSetStatus()
  936. if ruleSet != nil {
  937. conf.Streams = ruleSet.Streams
  938. conf.Tables = ruleSet.Tables
  939. conf.Rules = ruleSet.Rules
  940. }
  941. conf.NativePlugins = pluginStatusExport()
  942. conf.PortablePlugins = portablePluginStatusExport()
  943. conf.Service = serviceStatusExport()
  944. conf.Schema = schemaStatusExport()
  945. yamlCfgStatus := meta.GetConfigurationStatus()
  946. conf.SourceConfig = yamlCfgStatus.Sources
  947. conf.SinkConfig = yamlCfgStatus.Sinks
  948. conf.ConnectionConfig = yamlCfgStatus.Connections
  949. return conf
  950. }
  951. func configurationStatusHandler(w http.ResponseWriter, r *http.Request) {
  952. defer r.Body.Close()
  953. content := configurationStatusExport()
  954. jsonResponse(content, w, logger)
  955. }
  956. func importRuleSetPartial(all processor.Ruleset) processor.Ruleset {
  957. ruleSetRsp := processor.Ruleset{
  958. Rules: map[string]string{},
  959. Streams: map[string]string{},
  960. Tables: map[string]string{},
  961. }
  962. // replace streams
  963. for k, v := range all.Streams {
  964. _, e := streamProcessor.ExecReplaceStream(k, v, ast.TypeStream)
  965. if e != nil {
  966. ruleSetRsp.Streams[k] = e.Error()
  967. continue
  968. }
  969. }
  970. // replace tables
  971. for k, v := range all.Tables {
  972. _, e := streamProcessor.ExecReplaceStream(k, v, ast.TypeTable)
  973. if e != nil {
  974. ruleSetRsp.Tables[k] = e.Error()
  975. continue
  976. }
  977. }
  978. for k, v := range all.Rules {
  979. _, err := ruleProcessor.GetRuleJson(k)
  980. if err == nil {
  981. // the rule already exist, update
  982. err = updateRule(k, v)
  983. if err != nil {
  984. ruleSetRsp.Rules[k] = err.Error()
  985. continue
  986. }
  987. // Update to db after validation
  988. _, err = ruleProcessor.ExecUpdate(k, v)
  989. if err != nil {
  990. ruleSetRsp.Rules[k] = err.Error()
  991. continue
  992. }
  993. } else {
  994. // not found, create
  995. _, err2 := createRule(k, v)
  996. if err2 != nil {
  997. ruleSetRsp.Rules[k] = err2.Error()
  998. continue
  999. }
  1000. }
  1001. }
  1002. return ruleSetRsp
  1003. }