setting.go 4.6 KB

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