server.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. package server
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/binder/function"
  19. "github.com/lf-edge/ekuiper/internal/binder/io"
  20. "github.com/lf-edge/ekuiper/internal/binder/meta"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/pkg/store"
  23. "github.com/lf-edge/ekuiper/internal/processor"
  24. "github.com/lf-edge/ekuiper/internal/topo/connection/factory"
  25. "net/http"
  26. "os"
  27. "os/signal"
  28. "path/filepath"
  29. "sort"
  30. "syscall"
  31. "time"
  32. )
  33. var (
  34. logger = conf.Log
  35. startTimeStamp int64
  36. version = ""
  37. ruleProcessor *processor.RuleProcessor
  38. streamProcessor *processor.StreamProcessor
  39. )
  40. func createPaths() {
  41. dataDir, err := conf.GetDataLoc()
  42. if err != nil {
  43. panic(err)
  44. }
  45. dirs := []string{"uploads", "sources", "sinks", "functions", "services", "services/schemas", "connections"}
  46. for _, v := range dirs {
  47. // Create dir if not exist
  48. realDir := filepath.Join(dataDir, v)
  49. if _, err := os.Stat(realDir); os.IsNotExist(err) {
  50. if err := os.MkdirAll(realDir, os.ModePerm); err != nil {
  51. panic(err)
  52. }
  53. }
  54. }
  55. files := []string{"connections/connection.yaml"}
  56. for _, v := range files {
  57. // Create dir if not exist
  58. realFile := filepath.Join(dataDir, v)
  59. if _, err := os.Stat(realFile); os.IsNotExist(err) {
  60. if _, err := os.Create(realFile); err != nil {
  61. panic(err)
  62. }
  63. }
  64. }
  65. }
  66. func StartUp(Version, LoadFileType string) {
  67. version = Version
  68. conf.LoadFileType = LoadFileType
  69. startTimeStamp = time.Now().Unix()
  70. createPaths()
  71. conf.InitConf()
  72. factory.InitClientsFactory()
  73. err := store.SetupWithKuiperConfig(conf.Config)
  74. if err != nil {
  75. panic(err)
  76. }
  77. ruleProcessor = processor.NewRuleProcessor()
  78. streamProcessor = processor.NewStreamProcessor()
  79. // register all extensions
  80. for k, v := range components {
  81. logger.Infof("register component %s", k)
  82. v.register()
  83. }
  84. // Bind the source, function, sink
  85. sort.Sort(entries)
  86. err = function.Initialize(entries)
  87. if err != nil {
  88. panic(err)
  89. }
  90. err = io.Initialize(entries)
  91. if err != nil {
  92. panic(err)
  93. }
  94. meta.Bind()
  95. registry = &RuleRegistry{internal: make(map[string]*RuleState)}
  96. //Start lookup tables
  97. streamProcessor.RecoverLookupTable()
  98. //Start rules
  99. if rules, err := ruleProcessor.GetAllRules(); err != nil {
  100. logger.Infof("Start rules error: %s", err)
  101. } else {
  102. logger.Info("Starting rules")
  103. var reply string
  104. for _, rule := range rules {
  105. //err = server.StartRule(rule, &reply)
  106. reply = recoverRule(rule)
  107. if 0 != len(reply) {
  108. logger.Info(reply)
  109. }
  110. }
  111. }
  112. //Start rest service
  113. srvRest := createRestServer(conf.Config.Basic.RestIp, conf.Config.Basic.RestPort, conf.Config.Basic.Authentication)
  114. go func() {
  115. var err error
  116. if conf.Config.Basic.RestTls == nil {
  117. err = srvRest.ListenAndServe()
  118. } else {
  119. err = srvRest.ListenAndServeTLS(conf.Config.Basic.RestTls.Certfile, conf.Config.Basic.RestTls.Keyfile)
  120. }
  121. if err != nil && err != http.ErrServerClosed {
  122. logger.Errorf("Error serving rest service: %s", err)
  123. }
  124. }()
  125. // Start extend services
  126. for k, v := range servers {
  127. logger.Infof("start service %s", k)
  128. v.serve()
  129. }
  130. //Startup message
  131. restHttpType := "http"
  132. if conf.Config.Basic.RestTls != nil {
  133. restHttpType = "https"
  134. }
  135. msg := fmt.Sprintf("Serving kuiper (version - %s) on port %d, and restful api on %s://%s:%d. \n", Version, conf.Config.Basic.Port, restHttpType, conf.Config.Basic.RestIp, conf.Config.Basic.RestPort)
  136. logger.Info(msg)
  137. fmt.Printf(msg)
  138. //Stop the services
  139. sigint := make(chan os.Signal, 1)
  140. signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
  141. <-sigint
  142. if err = srvRest.Shutdown(context.TODO()); err != nil {
  143. logger.Errorf("rest server shutdown error: %v", err)
  144. }
  145. logger.Info("rest server successfully shutdown.")
  146. // close extend services
  147. for k, v := range servers {
  148. logger.Infof("close service %s", k)
  149. v.close()
  150. }
  151. os.Exit(0)
  152. }