setting.go 5.9 KB

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