setting.go 4.7 KB

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