server.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "sort"
  29. "syscall"
  30. "time"
  31. )
  32. var (
  33. logger = conf.Log
  34. startTimeStamp int64
  35. version = ""
  36. ruleProcessor *processor.RuleProcessor
  37. streamProcessor *processor.StreamProcessor
  38. )
  39. func StartUp(Version, LoadFileType string) {
  40. version = Version
  41. conf.LoadFileType = LoadFileType
  42. startTimeStamp = time.Now().Unix()
  43. conf.InitConf()
  44. factory.InitClientsFactory()
  45. err := store.SetupWithKuiperConfig(conf.Config)
  46. if err != nil {
  47. panic(err)
  48. }
  49. ruleProcessor = processor.NewRuleProcessor()
  50. streamProcessor = processor.NewStreamProcessor()
  51. // register all extensions
  52. for k, v := range components {
  53. logger.Infof("register component %s", k)
  54. v.register()
  55. }
  56. // Bind the source, function, sink
  57. sort.Sort(entries)
  58. err = function.Initialize(entries)
  59. if err != nil {
  60. panic(err)
  61. }
  62. err = io.Initialize(entries)
  63. if err != nil {
  64. panic(err)
  65. }
  66. meta.Bind()
  67. registry = &RuleRegistry{internal: make(map[string]*RuleState)}
  68. //Start rules
  69. if rules, err := ruleProcessor.GetAllRules(); err != nil {
  70. logger.Infof("Start rules error: %s", err)
  71. } else {
  72. logger.Info("Starting rules")
  73. var reply string
  74. for _, rule := range rules {
  75. //err = server.StartRule(rule, &reply)
  76. reply = recoverRule(rule)
  77. if 0 != len(reply) {
  78. logger.Info(reply)
  79. }
  80. }
  81. }
  82. //Start rest service
  83. srvRest := createRestServer(conf.Config.Basic.RestIp, conf.Config.Basic.RestPort, conf.Config.Basic.Authentication)
  84. go func() {
  85. var err error
  86. if conf.Config.Basic.RestTls == nil {
  87. err = srvRest.ListenAndServe()
  88. } else {
  89. err = srvRest.ListenAndServeTLS(conf.Config.Basic.RestTls.Certfile, conf.Config.Basic.RestTls.Keyfile)
  90. }
  91. if err != nil && err != http.ErrServerClosed {
  92. logger.Errorf("Error serving rest service: %s", err)
  93. }
  94. }()
  95. // Start extend services
  96. for k, v := range servers {
  97. logger.Infof("start service %s", k)
  98. v.serve()
  99. }
  100. //Startup message
  101. restHttpType := "http"
  102. if conf.Config.Basic.RestTls != nil {
  103. restHttpType = "https"
  104. }
  105. 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)
  106. logger.Info(msg)
  107. fmt.Printf(msg)
  108. //Stop the services
  109. sigint := make(chan os.Signal, 1)
  110. signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
  111. <-sigint
  112. if err = srvRest.Shutdown(context.TODO()); err != nil {
  113. logger.Errorf("rest server shutdown error: %v", err)
  114. }
  115. logger.Info("rest server successfully shutdown.")
  116. // close extend services
  117. for k, v := range servers {
  118. logger.Infof("close service %s", k)
  119. v.close()
  120. }
  121. os.Exit(0)
  122. }