setting.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. var (
  22. // App settings.
  23. AppVer string
  24. AppName string
  25. AppUrl string
  26. AppSubUrl string
  27. // Log settings.
  28. LogRootPath string
  29. LogModes []string
  30. LogConfigs []string
  31. // Http server options
  32. Protocol Scheme
  33. Domain string
  34. HttpAddr, HttpPort string
  35. SshPort int
  36. CertFile, KeyFile string
  37. RouterLogging bool
  38. StaticRootPath string
  39. // Session settings.
  40. SessionProvider string
  41. SessionConfig *session.Config
  42. // Global setting objects.
  43. Cfg *goconfig.ConfigFile
  44. ConfRootPath string
  45. CustomPath string // Custom directory path.
  46. ProdMode bool
  47. RunUser string
  48. IsWindows bool
  49. // PhantomJs Rendering
  50. ImagesDir string
  51. PhantomDir string
  52. )
  53. func init() {
  54. IsWindows = runtime.GOOS == "windows"
  55. log.NewLogger(0, "console", `{"level": 0}`)
  56. }
  57. func WorkDir() (string, error) {
  58. p, err := filepath.Abs(".")
  59. if err != nil {
  60. return "", err
  61. }
  62. return p, nil
  63. }
  64. func NewConfigContext() {
  65. workDir, err := WorkDir()
  66. if err != nil {
  67. log.Fatal(4, "Fail to get work directory: %v", err)
  68. }
  69. ConfRootPath = path.Join(workDir, "conf")
  70. Cfg, err = goconfig.LoadConfigFile(path.Join(workDir, "conf/grafana.ini"))
  71. if err != nil {
  72. log.Fatal(4, "Fail to parse '%v/conf/grafana.ini': %v", workDir, err)
  73. }
  74. CustomPath = os.Getenv("GRAFANA_CONF")
  75. if len(CustomPath) == 0 {
  76. CustomPath = path.Join(workDir, "custom")
  77. }
  78. cfgPath := path.Join(CustomPath, "conf/grafana.ini")
  79. if com.IsFile(cfgPath) {
  80. if err = Cfg.AppendFiles(cfgPath); err != nil {
  81. log.Fatal(4, "Fail to load custom 'conf/grafana.ini': %v", err)
  82. }
  83. } else {
  84. log.Warn("No custom 'conf/grafana.ini'")
  85. }
  86. AppName = Cfg.MustValue("", "app_name", "Grafana Pro")
  87. AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
  88. if AppUrl[len(AppUrl)-1] != '/' {
  89. AppUrl += "/"
  90. }
  91. // Check if has app suburl.
  92. url, err := url.Parse(AppUrl)
  93. if err != nil {
  94. log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
  95. }
  96. AppSubUrl = strings.TrimSuffix(url.Path, "/")
  97. Protocol = HTTP
  98. if Cfg.MustValue("server", "protocol") == "https" {
  99. Protocol = HTTPS
  100. CertFile = Cfg.MustValue("server", "cert_file")
  101. KeyFile = Cfg.MustValue("server", "key_file")
  102. }
  103. Domain = Cfg.MustValue("server", "domain", "localhost")
  104. HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
  105. HttpPort = Cfg.MustValue("server", "http_port", "3000")
  106. port := os.Getenv("PORT")
  107. if port != "" {
  108. HttpPort = port
  109. }
  110. StaticRootPath = Cfg.MustValue("server", "static_root_path", workDir)
  111. RouterLogging = Cfg.MustBool("server", "router_logging", false)
  112. // PhantomJS rendering
  113. ImagesDir = "data/png"
  114. PhantomDir = "_vendor/phantomjs"
  115. LogRootPath = Cfg.MustValue("log", "root_path", path.Join(workDir, "/data/log"))
  116. }
  117. func initSessionService() {
  118. SessionProvider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
  119. SessionConfig = new(session.Config)
  120. SessionConfig.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
  121. SessionConfig.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
  122. SessionConfig.CookiePath = AppSubUrl
  123. SessionConfig.Secure = Cfg.MustBool("session", "cookie_secure")
  124. SessionConfig.EnableSetCookie = Cfg.MustBool("session", "enable_set_cookie", true)
  125. SessionConfig.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
  126. SessionConfig.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
  127. if SessionProvider == "file" {
  128. os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm)
  129. }
  130. log.Info("Session Service Enabled")
  131. }
  132. func InitServices() {
  133. initSessionService()
  134. }