setting.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/grafana/grafana/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. )
  79. func init() {
  80. IsWindows = runtime.GOOS == "windows"
  81. log.NewLogger(0, "console", `{"level": 0}`)
  82. }
  83. func getWorkDir() string {
  84. p, _ := filepath.Abs(".")
  85. return p
  86. }
  87. func findConfigFiles() []string {
  88. WorkDir = getWorkDir()
  89. ConfRootPath = path.Join(WorkDir, "conf")
  90. filenames := make([]string, 0)
  91. configFile := path.Join(ConfRootPath, "grafana.ini")
  92. if com.IsFile(configFile) {
  93. filenames = append(filenames, configFile)
  94. }
  95. configFile = path.Join(ConfRootPath, "grafana.dev.ini")
  96. if com.IsFile(configFile) {
  97. filenames = append(filenames, configFile)
  98. }
  99. configFile = path.Join(ConfRootPath, "grafana.custom.ini")
  100. if com.IsFile(configFile) {
  101. filenames = append(filenames, configFile)
  102. }
  103. if len(filenames) == 0 {
  104. log.Fatal(3, "Could not find any config file")
  105. }
  106. return filenames
  107. }
  108. func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
  109. appUrl := section.Key("root_url").MustString("http://localhost:3000/")
  110. if appUrl[len(appUrl)-1] != '/' {
  111. appUrl += "/"
  112. }
  113. // Check if has app suburl.
  114. url, err := url.Parse(appUrl)
  115. if err != nil {
  116. log.Fatal(4, "Invalid root_url(%s): %s", appUrl, err)
  117. }
  118. appSubUrl := strings.TrimSuffix(url.Path, "/")
  119. return appUrl, appSubUrl
  120. }
  121. func AbsUrlTo(relativeUrl string) string {
  122. return AppUrl + relativeUrl
  123. }
  124. func NewConfigContext() {
  125. configFiles := findConfigFiles()
  126. //log.Info("Loading config files: %v", configFiles)
  127. var err error
  128. for i, file := range configFiles {
  129. if i == 0 {
  130. Cfg, err = ini.Load(configFiles[i])
  131. } else {
  132. err = Cfg.Append(configFiles[i])
  133. }
  134. if err != nil {
  135. log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err)
  136. }
  137. }
  138. AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
  139. Env = Cfg.Section("").Key("app_mode").MustString("development")
  140. server := Cfg.Section("server")
  141. AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
  142. Protocol = HTTP
  143. if server.Key("protocol").MustString("http") == "https" {
  144. Protocol = HTTPS
  145. CertFile = server.Key("cert_file").String()
  146. KeyFile = server.Key("cert_file").String()
  147. }
  148. Domain = server.Key("domain").MustString("localhost")
  149. HttpAddr = server.Key("http_addr").MustString("0.0.0.0")
  150. HttpPort = server.Key("http_port").MustString("3000")
  151. port := os.Getenv("PORT")
  152. if port != "" {
  153. HttpPort = port
  154. }
  155. StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp"))
  156. RouterLogging = server.Key("router_logging").MustBool(false)
  157. EnableGzip = server.Key("enable_gzip").MustBool(false)
  158. security := Cfg.Section("security")
  159. SecretKey = security.Key("secret_key").String()
  160. LogInRememberDays = security.Key("login_remember_days").MustInt()
  161. CookieUserName = security.Key("cookie_username").String()
  162. CookieRememberName = security.Key("cookie_remember_name").String()
  163. DisableUserSignUp = security.Key("disable_user_signup").MustBool(false)
  164. // admin
  165. AdminUser = security.Key("admin_user").String()
  166. AdminPassword = security.Key("admin_password").String()
  167. // single account
  168. SingleAccountMode = Cfg.Section("account.single").Key("enabled").MustBool(false)
  169. DefaultAccountName = Cfg.Section("account.single").Key("account_name").MustString("main")
  170. DefaultAccountRole = Cfg.Section("account.single").Key("default_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
  171. // anonymous access
  172. AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
  173. AnonymousAccountName = Cfg.Section("auth.anonymous").Key("account_name").String()
  174. AnonymousAccountRole = Cfg.Section("auth.anonymous").Key("account_role").String()
  175. // PhantomJS rendering
  176. ImagesDir = "data/png"
  177. PhantomDir = "vendor/phantomjs"
  178. LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log"))
  179. readSessionConfig()
  180. }
  181. func readSessionConfig() {
  182. sec := Cfg.Section("session")
  183. SessionOptions = session.Options{}
  184. SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"})
  185. SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
  186. SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
  187. SessionOptions.CookiePath = AppSubUrl
  188. SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
  189. SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
  190. SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
  191. if SessionOptions.Provider == "file" {
  192. os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
  193. }
  194. }