setting.go 5.4 KB

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