config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package config
  11. import (
  12. "fmt"
  13. "path"
  14. "strconv"
  15. "devt.de/krotik/common/errorutil"
  16. "devt.de/krotik/common/fileutil"
  17. )
  18. // Global variables
  19. // ================
  20. /*
  21. ProductVersion is the current version of EliasDB
  22. */
  23. const ProductVersion = "1.0.0"
  24. /*
  25. DefaultConfigFile is the default config file which will be used to configure EliasDB
  26. */
  27. var DefaultConfigFile = "eliasdb.config.json"
  28. /*
  29. Known configuration options for EliasDB
  30. */
  31. const (
  32. MemoryOnlyStorage = "MemoryOnlyStorage"
  33. LocationDatastore = "LocationDatastore"
  34. LocationHTTPS = "LocationHTTPS"
  35. LocationWebFolder = "LocationWebFolder"
  36. LocationUserDB = "LocationUserDB"
  37. LocationAccessDB = "LocationAccessDB"
  38. HTTPSCertificate = "HTTPSCertificate"
  39. HTTPSKey = "HTTPSKey"
  40. LockFile = "LockFile"
  41. HTTPSHost = "HTTPSHost"
  42. HTTPSPort = "HTTPSPort"
  43. CookieMaxAgeSeconds = "CookieMaxAgeSeconds"
  44. EnableReadOnly = "EnableReadOnly"
  45. EnableWebFolder = "EnableWebFolder"
  46. EnableAccessControl = "EnableAccessControl"
  47. EnableWebTerminal = "EnableWebTerminal"
  48. EnableCluster = "EnableCluster"
  49. EnableClusterTerminal = "EnableClusterTerminal"
  50. ResultCacheMaxSize = "ResultCacheMaxSize"
  51. ResultCacheMaxAgeSeconds = "ResultCacheMaxAgeSeconds"
  52. ClusterStateInfoFile = "ClusterStateInfoFile"
  53. ClusterConfigFile = "ClusterConfigFile"
  54. ClusterLogHistory = "ClusterLogHistory"
  55. )
  56. /*
  57. DefaultConfig is the defaut configuration
  58. */
  59. var DefaultConfig = map[string]interface{}{
  60. MemoryOnlyStorage: false,
  61. EnableReadOnly: false,
  62. EnableWebFolder: true,
  63. EnableAccessControl: false,
  64. EnableWebTerminal: true,
  65. EnableCluster: false,
  66. EnableClusterTerminal: false,
  67. LocationDatastore: "db",
  68. LocationHTTPS: "ssl",
  69. LocationWebFolder: "web",
  70. LocationUserDB: "users.db",
  71. LocationAccessDB: "access.db",
  72. HTTPSHost: "127.0.0.1",
  73. HTTPSPort: "9090",
  74. CookieMaxAgeSeconds: "86400",
  75. HTTPSCertificate: "cert.pem",
  76. HTTPSKey: "key.pem",
  77. LockFile: "eliasdb.lck",
  78. ResultCacheMaxSize: 0,
  79. ResultCacheMaxAgeSeconds: 0,
  80. ClusterStateInfoFile: "cluster.stateinfo",
  81. ClusterConfigFile: "cluster.config.json",
  82. ClusterLogHistory: 100.0,
  83. }
  84. /*
  85. Config is the actual config which is used
  86. */
  87. var Config map[string]interface{}
  88. /*
  89. LoadConfigFile loads a given config file. If the config file does not exist it is
  90. created with the default options.
  91. */
  92. func LoadConfigFile(configfile string) error {
  93. var err error
  94. Config, err = fileutil.LoadConfig(configfile, DefaultConfig)
  95. return err
  96. }
  97. /*
  98. LoadDefaultConfig loads the default configuration.
  99. */
  100. func LoadDefaultConfig() {
  101. data := make(map[string]interface{})
  102. for k, v := range DefaultConfig {
  103. data[k] = v
  104. }
  105. Config = data
  106. }
  107. // Helper functions
  108. // ================
  109. /*
  110. Str reads a config value as a string value.
  111. */
  112. func Str(key string) string {
  113. return fmt.Sprint(Config[key])
  114. }
  115. /*
  116. Int reads a config value as an int value.
  117. */
  118. func Int(key string) int64 {
  119. ret, err := strconv.ParseInt(fmt.Sprint(Config[key]), 10, 64)
  120. errorutil.AssertTrue(err == nil,
  121. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  122. return ret
  123. }
  124. /*
  125. Bool reads a config value as a boolean value.
  126. */
  127. func Bool(key string) bool {
  128. ret, err := strconv.ParseBool(fmt.Sprint(Config[key]))
  129. errorutil.AssertTrue(err == nil,
  130. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  131. return ret
  132. }
  133. /*
  134. WebPath returns a path relative to the web directory.
  135. */
  136. func WebPath(parts ...string) string {
  137. return path.Join("web", path.Join(parts...))
  138. }