setup.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2021 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 store
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/pkg/db"
  18. "github.com/lf-edge/ekuiper/internal/pkg/db/redis"
  19. "github.com/lf-edge/ekuiper/internal/pkg/db/sql/sqlite"
  20. )
  21. func SetupDefault() error {
  22. dir, err := conf.GetDataLoc()
  23. if err != nil {
  24. return err
  25. }
  26. c := db.Config{
  27. Type: "sqlite",
  28. Redis: redis.Config{},
  29. Sqlite: sqlite.Config{
  30. Path: dir,
  31. Name: "",
  32. },
  33. }
  34. return Setup(c)
  35. }
  36. func SetupWithKuiperConfig(kconf *conf.KuiperConf) error {
  37. dir, err := conf.GetDataLoc()
  38. if err != nil {
  39. return err
  40. }
  41. c := db.Config{
  42. Type: kconf.Store.Type,
  43. Redis: redis.Config{
  44. Host: kconf.Store.Redis.Host,
  45. Port: kconf.Store.Redis.Port,
  46. Password: kconf.Store.Redis.Password,
  47. Timeout: kconf.Store.Redis.Timeout,
  48. },
  49. Sqlite: sqlite.Config{
  50. Path: dir,
  51. Name: kconf.Store.Sqlite.Name,
  52. },
  53. }
  54. return Setup(c)
  55. }
  56. func Setup(config db.Config) error {
  57. database, err := db.CreateDatabase(config)
  58. if err != nil {
  59. return err
  60. }
  61. err = InitGlobalStores(database)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }