setting.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package setting
  4. import (
  5. "net/url"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/goconfig"
  14. "github.com/macaron-contrib/session"
  15. "github.com/torkelo/grafana-pro/pkg/log"
  16. )
  17. type Scheme string
  18. const (
  19. HTTP Scheme = "http"
  20. HTTPS Scheme = "https"
  21. )
  22. var (
  23. // App settings.
  24. AppVer string
  25. AppName string
  26. AppUrl string
  27. AppSubUrl string
  28. // Log settings.
  29. LogRootPath string
  30. LogModes []string
  31. LogConfigs []string
  32. // Http server options
  33. Protocol Scheme
  34. Domain string
  35. HttpAddr, HttpPort string
  36. SshPort int
  37. CertFile, KeyFile string
  38. RouterLogging bool
  39. StaticRootPath string
  40. // Session settings.
  41. SessionProvider string
  42. SessionConfig *session.Config
  43. // Global setting objects.
  44. Cfg *goconfig.ConfigFile
  45. ConfRootPath string
  46. CustomPath string // Custom directory path.
  47. ProdMode bool
  48. RunUser string
  49. IsWindows bool
  50. )
  51. func init() {
  52. IsWindows = runtime.GOOS == "windows"
  53. log.NewLogger(0, "console", `{"level": 0}`)
  54. }
  55. // WorkDir returns absolute path of work directory.
  56. func WorkDir() (string, error) {
  57. execPath, err := ExecPath()
  58. return path.Dir(strings.Replace(execPath, "\\", "/", -1)), err
  59. }
  60. func ExecPath() (string, error) {
  61. file, err := exec.LookPath(os.Args[0])
  62. if err != nil {
  63. return "", err
  64. }
  65. p, err := filepath.Abs(file)
  66. if err != nil {
  67. return "", err
  68. }
  69. return p, nil
  70. }
  71. func NewConfigContext() {
  72. workDir, err := WorkDir()
  73. if err != nil {
  74. log.Fatal(4, "Fail to get work directory: %v", err)
  75. }
  76. ConfRootPath = path.Join(workDir, "conf")
  77. Cfg, err = goconfig.LoadConfigFile(path.Join(workDir, "conf/grafana.ini"))
  78. if err != nil {
  79. log.Fatal(4, "Fail to parse 'conf/grafana.ini': %v", err)
  80. }
  81. CustomPath = os.Getenv("GRAFANA_CONF")
  82. if len(CustomPath) == 0 {
  83. CustomPath = path.Join(workDir, "custom")
  84. }
  85. cfgPath := path.Join(CustomPath, "conf/grafana.ini")
  86. if com.IsFile(cfgPath) {
  87. if err = Cfg.AppendFiles(cfgPath); err != nil {
  88. log.Fatal(4, "Fail to load custom 'conf/grafana.ini': %v", err)
  89. }
  90. } else {
  91. log.Warn("No custom 'conf/grafana.ini'")
  92. }
  93. AppName = Cfg.MustValue("", "app_name", "Grafana Pro")
  94. AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
  95. if AppUrl[len(AppUrl)-1] != '/' {
  96. AppUrl += "/"
  97. }
  98. // Check if has app suburl.
  99. url, err := url.Parse(AppUrl)
  100. if err != nil {
  101. log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
  102. }
  103. AppSubUrl = strings.TrimSuffix(url.Path, "/")
  104. Protocol = HTTP
  105. if Cfg.MustValue("server", "protocol") == "https" {
  106. Protocol = HTTPS
  107. CertFile = Cfg.MustValue("server", "cert_file")
  108. KeyFile = Cfg.MustValue("server", "key_file")
  109. }
  110. Domain = Cfg.MustValue("server", "domain", "localhost")
  111. HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
  112. HttpPort = Cfg.MustValue("server", "http_port", "3000")
  113. port := os.Getenv("PORT")
  114. if port != "" {
  115. HttpPort = port
  116. }
  117. StaticRootPath = Cfg.MustValue("server", "static_root_path", workDir)
  118. RouterLogging = Cfg.MustBool("server", "router_logging", false)
  119. }
  120. func initSessionService() {
  121. SessionProvider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
  122. SessionConfig = new(session.Config)
  123. SessionConfig.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
  124. SessionConfig.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
  125. SessionConfig.CookiePath = AppSubUrl
  126. SessionConfig.Secure = Cfg.MustBool("session", "cookie_secure")
  127. SessionConfig.EnableSetCookie = Cfg.MustBool("session", "enable_set_cookie", true)
  128. SessionConfig.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
  129. SessionConfig.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
  130. SessionConfig.SessionIDHashFunc = Cfg.MustValueRange("session", "session_id_hashfunc",
  131. "sha1", []string{"sha1", "sha256", "md5"})
  132. SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "session_id_hashkey", string(com.RandomCreateBytes(16)))
  133. if SessionProvider == "file" {
  134. os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm)
  135. }
  136. log.Info("Session Service Enabled")
  137. }
  138. func InitServices() {
  139. initSessionService()
  140. }