setting.go 4.3 KB

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