setting.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package setting
  4. import (
  5. "net/url"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "github.com/Unknwon/com"
  12. "github.com/macaron-contrib/session"
  13. "gopkg.in/ini.v1"
  14. "github.com/torkelo/grafana-pro/pkg/log"
  15. )
  16. type Scheme string
  17. const (
  18. HTTP Scheme = "http"
  19. HTTPS Scheme = "https"
  20. )
  21. const (
  22. DEV string = "development"
  23. PROD string = "production"
  24. TEST string = "test"
  25. )
  26. var (
  27. // App settings.
  28. Env string = DEV
  29. AppName string
  30. AppUrl string
  31. AppSubUrl string
  32. // build
  33. BuildVersion string
  34. BuildCommit string
  35. BuildStamp int64
  36. // Log settings.
  37. LogRootPath string
  38. LogModes []string
  39. LogConfigs []string
  40. // Http server options
  41. Protocol Scheme
  42. Domain string
  43. HttpAddr, HttpPort string
  44. SshPort int
  45. CertFile, KeyFile string
  46. RouterLogging bool
  47. StaticRootPath string
  48. EnableGzip bool
  49. // Security settings.
  50. SecretKey string
  51. LogInRememberDays int
  52. CookieUserName string
  53. CookieRememberName string
  54. DisableUserSignUp bool
  55. // single account
  56. SingleAccountMode bool
  57. DefaultAccountName string
  58. DefaultAccountRole string
  59. // Http auth
  60. AdminUser string
  61. AdminPassword string
  62. AnonymousEnabled bool
  63. AnonymousAccountName string
  64. AnonymousAccountRole string
  65. // Session settings.
  66. SessionOptions session.Options
  67. // Global setting objects.
  68. WorkDir string
  69. Cfg *ini.File
  70. ConfRootPath string
  71. CustomPath string // Custom directory path.
  72. ProdMode bool
  73. RunUser string
  74. IsWindows bool
  75. // PhantomJs Rendering
  76. ImagesDir string
  77. PhantomDir string
  78. //Notifications
  79. NotificationsEnabled bool
  80. RabbitmqUrl string
  81. NotificationsExchange string
  82. )
  83. func init() {
  84. IsWindows = runtime.GOOS == "windows"
  85. log.NewLogger(0, "console", `{"level": 0}`)
  86. }
  87. func getWorkDir() string {
  88. p, _ := filepath.Abs(".")
  89. return p
  90. }
  91. func findConfigFiles() []string {
  92. WorkDir = getWorkDir()
  93. ConfRootPath = path.Join(WorkDir, "conf")
  94. filenames := make([]string, 0)
  95. configFile := path.Join(ConfRootPath, "grafana.ini")
  96. if com.IsFile(configFile) {
  97. filenames = append(filenames, configFile)
  98. }
  99. configFile = path.Join(ConfRootPath, "grafana.dev.ini")
  100. if com.IsFile(configFile) {
  101. filenames = append(filenames, configFile)
  102. }
  103. configFile = path.Join(ConfRootPath, "grafana.custom.ini")
  104. if com.IsFile(configFile) {
  105. filenames = append(filenames, configFile)
  106. }
  107. if len(filenames) == 0 {
  108. log.Fatal(3, "Could not find any config file")
  109. }
  110. return filenames
  111. }
  112. func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
  113. appUrl := section.Key("root_url").MustString("http://localhost:3000/")
  114. if appUrl[len(appUrl)-1] != '/' {
  115. appUrl += "/"
  116. }
  117. // Check if has app suburl.
  118. url, err := url.Parse(appUrl)
  119. if err != nil {
  120. log.Fatal(4, "Invalid root_url(%s): %s", appUrl, err)
  121. }
  122. appSubUrl := strings.TrimSuffix(url.Path, "/")
  123. return appUrl, appSubUrl
  124. }
  125. func NewConfigContext() {
  126. configFiles := findConfigFiles()
  127. //log.Info("Loading config files: %v", configFiles)
  128. var err error
  129. for i, file := range configFiles {
  130. if i == 0 {
  131. Cfg, err = ini.Load(configFiles[i])
  132. } else {
  133. err = Cfg.Append(configFiles[i])
  134. }
  135. if err != nil {
  136. log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err)
  137. }
  138. }
  139. AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
  140. Env = Cfg.Section("").Key("app_mode").MustString("development")
  141. server := Cfg.Section("server")
  142. AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
  143. Protocol = HTTP
  144. if server.Key("protocol").MustString("http") == "https" {
  145. Protocol = HTTPS
  146. CertFile = server.Key("cert_file").String()
  147. KeyFile = server.Key("cert_file").String()
  148. }
  149. Domain = server.Key("domain").MustString("localhost")
  150. HttpAddr = server.Key("http_addr").MustString("0.0.0.0")
  151. HttpPort = server.Key("http_port").MustString("3000")
  152. port := os.Getenv("PORT")
  153. if port != "" {
  154. HttpPort = port
  155. }
  156. StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp"))
  157. RouterLogging = server.Key("router_logging").MustBool(false)
  158. EnableGzip = server.Key("enable_gzip").MustBool(false)
  159. security := Cfg.Section("security")
  160. SecretKey = security.Key("secret_key").String()
  161. LogInRememberDays = security.Key("login_remember_days").MustInt()
  162. CookieUserName = security.Key("cookie_username").String()
  163. CookieRememberName = security.Key("cookie_remember_name").String()
  164. DisableUserSignUp = security.Key("disable_user_signup").MustBool(false)
  165. // admin
  166. AdminUser = security.Key("admin_user").String()
  167. AdminPassword = security.Key("admin_password").String()
  168. // single account
  169. SingleAccountMode = Cfg.Section("account.single").Key("enabled").MustBool(false)
  170. DefaultAccountName = Cfg.Section("account.single").Key("account_name").MustString("main")
  171. DefaultAccountRole = Cfg.Section("account.single").Key("default_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
  172. // anonymous access
  173. AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
  174. AnonymousAccountName = Cfg.Section("auth.anonymous").Key("account_name").String()
  175. AnonymousAccountRole = Cfg.Section("auth.anonymous").Key("account_role").String()
  176. // PhantomJS rendering
  177. ImagesDir = "data/png"
  178. PhantomDir = "vendor/phantomjs"
  179. LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log"))
  180. // Notifications
  181. NotificationsEnabled = Cfg.Section("notifications").Key("enabled").MustBool(false)
  182. RabbitmqUrl = Cfg.Section("notifications").Key("rabbitmq_url").MustString("amqp://localhost/")
  183. // validate rabbitmqUrl.
  184. _, err = url.Parse(RabbitmqUrl)
  185. if err != nil {
  186. log.Fatal(4, "Invalid rabbitmq_url(%s): %s", RabbitmqUrl, err)
  187. }
  188. NotificationsExchange = Cfg.Section("notifications").Key("notifications_exchange").MustString("notifications")
  189. readSessionConfig()
  190. }
  191. func readSessionConfig() {
  192. sec := Cfg.Section("session")
  193. SessionOptions = session.Options{}
  194. SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"})
  195. SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
  196. SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
  197. SessionOptions.CookiePath = AppSubUrl
  198. SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
  199. SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
  200. SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
  201. if SessionOptions.Provider == "file" {
  202. os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
  203. }
  204. }