setting.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/Unknwon/goconfig"
  13. "github.com/macaron-contrib/session"
  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. // Http auth
  50. AdminUser string
  51. AdminPassword string
  52. Anonymous bool
  53. AnonymousAccountId int64
  54. // Session settings.
  55. SessionOptions session.Options
  56. // Global setting objects.
  57. WorkDir string
  58. Cfg *goconfig.ConfigFile
  59. ConfRootPath string
  60. CustomPath string // Custom directory path.
  61. ProdMode bool
  62. RunUser string
  63. IsWindows bool
  64. // PhantomJs Rendering
  65. ImagesDir string
  66. PhantomDir string
  67. )
  68. func init() {
  69. IsWindows = runtime.GOOS == "windows"
  70. log.NewLogger(0, "console", `{"level": 0}`)
  71. }
  72. func getWorkDir() string {
  73. p, _ := filepath.Abs(".")
  74. return p
  75. }
  76. func findConfigFiles() []string {
  77. WorkDir = getWorkDir()
  78. ConfRootPath = path.Join(WorkDir, "conf")
  79. filenames := make([]string, 0)
  80. configFile := path.Join(ConfRootPath, "grafana.ini")
  81. if com.IsFile(configFile) {
  82. filenames = append(filenames, configFile)
  83. }
  84. configFile = path.Join(ConfRootPath, "grafana.dev.ini")
  85. if com.IsFile(configFile) {
  86. filenames = append(filenames, configFile)
  87. }
  88. configFile = path.Join(ConfRootPath, "grafana.custom.ini")
  89. if com.IsFile(configFile) {
  90. filenames = append(filenames, configFile)
  91. }
  92. if len(filenames) == 0 {
  93. log.Fatal(3, "Could not find any config file")
  94. }
  95. return filenames
  96. }
  97. func NewConfigContext() {
  98. configFiles := findConfigFiles()
  99. //log.Info("Loading config files: %v", configFiles)
  100. var err error
  101. Cfg, err = goconfig.LoadConfigFile(configFiles[0])
  102. if err != nil {
  103. log.Fatal(4, "Fail to parse config file, error: %v", err)
  104. }
  105. if len(configFiles) > 1 {
  106. err = Cfg.AppendFiles(configFiles[1:]...)
  107. if err != nil {
  108. log.Fatal(4, "Fail to parse config file, error: %v", err)
  109. }
  110. }
  111. AppName = Cfg.MustValue("", "app_name", "Grafana")
  112. AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
  113. if AppUrl[len(AppUrl)-1] != '/' {
  114. AppUrl += "/"
  115. }
  116. // Check if has app suburl.
  117. url, err := url.Parse(AppUrl)
  118. if err != nil {
  119. log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
  120. }
  121. AppSubUrl = strings.TrimSuffix(url.Path, "/")
  122. Protocol = HTTP
  123. if Cfg.MustValue("server", "protocol") == "https" {
  124. Protocol = HTTPS
  125. CertFile = Cfg.MustValue("server", "cert_file")
  126. KeyFile = Cfg.MustValue("server", "key_file")
  127. }
  128. Domain = Cfg.MustValue("server", "domain", "localhost")
  129. HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
  130. HttpPort = Cfg.MustValue("server", "http_port", "3000")
  131. port := os.Getenv("PORT")
  132. if port != "" {
  133. HttpPort = port
  134. }
  135. StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp"))
  136. RouterLogging = Cfg.MustBool("server", "router_logging", false)
  137. EnableGzip = Cfg.MustBool("server", "enable_gzip")
  138. // Http auth
  139. AdminUser = Cfg.MustValue("admin", "user", "admin")
  140. AdminPassword = Cfg.MustValue("admin", "password", "admin")
  141. Anonymous = Cfg.MustBool("auth", "anonymous", false)
  142. AnonymousAccountId = Cfg.MustInt64("auth", "anonymous_account_id", 0)
  143. if Anonymous && AnonymousAccountId == 0 {
  144. log.Fatal(3, "Must specify account id for anonymous access")
  145. }
  146. // PhantomJS rendering
  147. ImagesDir = "data/png"
  148. PhantomDir = "_vendor/phantomjs"
  149. LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log"))
  150. readSessionConfig()
  151. }
  152. func readSessionConfig() {
  153. SessionOptions = session.Options{}
  154. SessionOptions.Provider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
  155. SessionOptions.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
  156. SessionOptions.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
  157. SessionOptions.CookiePath = AppSubUrl
  158. SessionOptions.Secure = Cfg.MustBool("session", "cookie_secure")
  159. SessionOptions.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
  160. SessionOptions.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
  161. if SessionOptions.Provider == "file" {
  162. os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
  163. }
  164. log.Info("Session Service Enabled")
  165. }