config.go 4.7 KB

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