setup.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2021 INTECH Process Automation 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 kv
  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(conf *conf.KuiperConf) error {
  37. c := db.Config{
  38. Type: conf.Store.Type,
  39. Redis: redis.Config{
  40. Host: conf.Store.Redis.Host,
  41. Port: conf.Store.Redis.Port,
  42. Password: conf.Store.Redis.Password,
  43. Timeout: conf.Store.Redis.Timeout,
  44. },
  45. Sqlite: sqlite.Config{
  46. Path: conf.Store.Sqlite.Path,
  47. Name: conf.Store.Sqlite.Name,
  48. },
  49. }
  50. return Setup(c)
  51. }
  52. func Setup(config db.Config) error {
  53. err, database := db.CreateDatabase(config)
  54. if err != nil {
  55. return err
  56. }
  57. err = InitGlobalStores(database)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }