rest.go 30 KB

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