config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.1.2"
  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. EnableECALScripts = "EnableECALScripts"
  46. EnableECALDebugServer = "EnableECALDebugServer"
  47. EnableWebFolder = "EnableWebFolder"
  48. EnableAccessControl = "EnableAccessControl"
  49. EnableWebTerminal = "EnableWebTerminal"
  50. EnableCluster = "EnableCluster"
  51. EnableClusterTerminal = "EnableClusterTerminal"
  52. ResultCacheMaxSize = "ResultCacheMaxSize"
  53. ResultCacheMaxAgeSeconds = "ResultCacheMaxAgeSeconds"
  54. ClusterStateInfoFile = "ClusterStateInfoFile"
  55. ClusterConfigFile = "ClusterConfigFile"
  56. ClusterLogHistory = "ClusterLogHistory"
  57. ECALScriptFolder = "ECALScriptFolder"
  58. ECALEntryScript = "ECALEntryScript"
  59. ECALLogLevel = "ECALLogLevel"
  60. ECALLogFile = "ECALLogFile"
  61. ECALDebugServerHost = "ECALDebugServerHost"
  62. ECALDebugServerPort = "ECALDebugServerPort"
  63. )
  64. /*
  65. DefaultConfig is the defaut configuration
  66. */
  67. var DefaultConfig = map[string]interface{}{
  68. MemoryOnlyStorage: false,
  69. EnableReadOnly: false,
  70. EnableECALScripts: false,
  71. EnableECALDebugServer: false,
  72. EnableWebFolder: true,
  73. EnableAccessControl: false,
  74. EnableWebTerminal: true,
  75. EnableCluster: false,
  76. EnableClusterTerminal: false,
  77. LocationDatastore: "db",
  78. LocationHTTPS: "ssl",
  79. LocationWebFolder: "web",
  80. LocationUserDB: "users.db",
  81. LocationAccessDB: "access.db",
  82. HTTPSHost: "127.0.0.1",
  83. HTTPSPort: "9090",
  84. CookieMaxAgeSeconds: "86400",
  85. HTTPSCertificate: "cert.pem",
  86. HTTPSKey: "key.pem",
  87. LockFile: "eliasdb.lck",
  88. ResultCacheMaxSize: 0,
  89. ResultCacheMaxAgeSeconds: 0,
  90. ClusterStateInfoFile: "cluster.stateinfo",
  91. ClusterConfigFile: "cluster.config.json",
  92. ClusterLogHistory: 100.0,
  93. ECALScriptFolder: "ecal",
  94. ECALEntryScript: "main.ecal",
  95. ECALLogLevel: "info",
  96. ECALLogFile: "",
  97. ECALDebugServerHost: "127.0.0.1",
  98. ECALDebugServerPort: "33274",
  99. }
  100. /*
  101. Config is the actual config which is used
  102. */
  103. var Config map[string]interface{}
  104. /*
  105. LoadConfigFile loads a given config file. If the config file does not exist it is
  106. created with the default options.
  107. */
  108. func LoadConfigFile(configfile string) error {
  109. var err error
  110. Config, err = fileutil.LoadConfig(configfile, DefaultConfig)
  111. return err
  112. }
  113. /*
  114. LoadDefaultConfig loads the default configuration.
  115. */
  116. func LoadDefaultConfig() {
  117. data := make(map[string]interface{})
  118. for k, v := range DefaultConfig {
  119. data[k] = v
  120. }
  121. Config = data
  122. }
  123. // Helper functions
  124. // ================
  125. /*
  126. Str reads a config value as a string value.
  127. */
  128. func Str(key string) string {
  129. return fmt.Sprint(Config[key])
  130. }
  131. /*
  132. Int reads a config value as an int value.
  133. */
  134. func Int(key string) int64 {
  135. ret, err := strconv.ParseInt(fmt.Sprint(Config[key]), 10, 64)
  136. errorutil.AssertTrue(err == nil,
  137. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  138. return ret
  139. }
  140. /*
  141. Bool reads a config value as a boolean value.
  142. */
  143. func Bool(key string) bool {
  144. ret, err := strconv.ParseBool(fmt.Sprint(Config[key]))
  145. errorutil.AssertTrue(err == nil,
  146. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  147. return ret
  148. }
  149. /*
  150. WebPath returns a path relative to the web directory.
  151. */
  152. func WebPath(parts ...string) string {
  153. return path.Join("web", path.Join(parts...))
  154. }