setting.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. )
  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 NewConfigContext() {
  122. configFiles := findConfigFiles()
  123. //log.Info("Loading config files: %v", configFiles)
  124. var err error
  125. for i, file := range configFiles {
  126. if i == 0 {
  127. Cfg, err = ini.Load(configFiles[i])
  128. } else {
  129. err = Cfg.Append(configFiles[i])
  130. }
  131. if err != nil {
  132. log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err)
  133. }
  134. }
  135. AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
  136. Env = Cfg.Section("").Key("app_mode").MustString("development")
  137. server := Cfg.Section("server")
  138. AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
  139. Protocol = HTTP
  140. if server.Key("protocol").MustString("http") == "https" {
  141. Protocol = HTTPS
  142. CertFile = server.Key("cert_file").String()
  143. KeyFile = server.Key("cert_file").String()
  144. }
  145. Domain = server.Key("domain").MustString("localhost")
  146. HttpAddr = server.Key("http_addr").MustString("0.0.0.0")
  147. HttpPort = server.Key("http_port").MustString("3000")
  148. port := os.Getenv("PORT")
  149. if port != "" {
  150. HttpPort = port
  151. }
  152. StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp"))
  153. RouterLogging = server.Key("router_logging").MustBool(false)
  154. EnableGzip = server.Key("enable_gzip").MustBool(false)
  155. security := Cfg.Section("security")
  156. SecretKey = security.Key("secret_key").String()
  157. LogInRememberDays = security.Key("login_remember_days").MustInt()
  158. CookieUserName = security.Key("cookie_username").String()
  159. CookieRememberName = security.Key("cookie_remember_name").String()
  160. DisableUserSignUp = security.Key("disable_user_signup").MustBool(false)
  161. // admin
  162. AdminUser = security.Key("admin_user").String()
  163. AdminPassword = security.Key("admin_password").String()
  164. // single account
  165. SingleAccountMode = Cfg.Section("account.single").Key("enabled").MustBool(false)
  166. DefaultAccountName = Cfg.Section("account.single").Key("account_name").MustString("main")
  167. DefaultAccountRole = Cfg.Section("account.single").Key("default_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
  168. // anonymous access
  169. AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
  170. AnonymousAccountName = Cfg.Section("auth.anonymous").Key("account_name").String()
  171. AnonymousAccountRole = Cfg.Section("auth.anonymous").Key("account_role").String()
  172. // PhantomJS rendering
  173. ImagesDir = "data/png"
  174. PhantomDir = "vendor/phantomjs"
  175. LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log"))
  176. // Notifications
  177. NotificationsEnabled = Cfg.Section("notifications").Key("enabled").MustBool(false)
  178. RabbitmqUrl = Cfg.Section("notifications").Key("rabbitmq_url").MustString("amqp://localhost/")
  179. // validate rabbitmqUrl.
  180. _, err = url.Parse(RabbitmqUrl)
  181. if err != nil {
  182. log.Fatal(4, "Invalid rabbitmq_url(%s): %s", RabbitmqUrl, err)
  183. }
  184. NotificationsExchange = Cfg.Section("notifications").Key("notifications_exchange").MustString("notifications")
  185. readSessionConfig()
  186. }
  187. func readSessionConfig() {
  188. sec := Cfg.Section("session")
  189. SessionOptions = session.Options{}
  190. SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"})
  191. SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
  192. SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
  193. SessionOptions.CookiePath = AppSubUrl
  194. SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
  195. SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
  196. SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
  197. if SessionOptions.Provider == "file" {
  198. os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
  199. }
  200. }