setting.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // PhantomJs Rendering
  51. ImagesDir string
  52. PhantomDir string
  53. )
  54. func init() {
  55. IsWindows = runtime.GOOS == "windows"
  56. log.NewLogger(0, "console", `{"level": 0}`)
  57. }
  58. // WorkDir returns absolute path of work directory.
  59. func WorkDir() (string, error) {
  60. execPath, err := ExecPath()
  61. return path.Dir(strings.Replace(execPath, "\\", "/", -1)), err
  62. }
  63. func ExecPath() (string, error) {
  64. file, err := exec.LookPath(os.Args[0])
  65. if err != nil {
  66. return "", err
  67. }
  68. p, err := filepath.Abs(file)
  69. if err != nil {
  70. return "", err
  71. }
  72. return p, nil
  73. }
  74. func NewConfigContext() {
  75. workDir, err := WorkDir()
  76. if err != nil {
  77. log.Fatal(4, "Fail to get work directory: %v", err)
  78. }
  79. ConfRootPath = path.Join(workDir, "conf")
  80. Cfg, err = goconfig.LoadConfigFile(path.Join(workDir, "conf/grafana.ini"))
  81. if err != nil {
  82. log.Fatal(4, "Fail to parse 'conf/grafana.ini': %v", err)
  83. }
  84. CustomPath = os.Getenv("GRAFANA_CONF")
  85. if len(CustomPath) == 0 {
  86. CustomPath = path.Join(workDir, "custom")
  87. }
  88. cfgPath := path.Join(CustomPath, "conf/grafana.ini")
  89. if com.IsFile(cfgPath) {
  90. if err = Cfg.AppendFiles(cfgPath); err != nil {
  91. log.Fatal(4, "Fail to load custom 'conf/grafana.ini': %v", err)
  92. }
  93. } else {
  94. log.Warn("No custom 'conf/grafana.ini'")
  95. }
  96. AppName = Cfg.MustValue("", "app_name", "Grafana Pro")
  97. AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
  98. if AppUrl[len(AppUrl)-1] != '/' {
  99. AppUrl += "/"
  100. }
  101. // Check if has app suburl.
  102. url, err := url.Parse(AppUrl)
  103. if err != nil {
  104. log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
  105. }
  106. AppSubUrl = strings.TrimSuffix(url.Path, "/")
  107. Protocol = HTTP
  108. if Cfg.MustValue("server", "protocol") == "https" {
  109. Protocol = HTTPS
  110. CertFile = Cfg.MustValue("server", "cert_file")
  111. KeyFile = Cfg.MustValue("server", "key_file")
  112. }
  113. Domain = Cfg.MustValue("server", "domain", "localhost")
  114. HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
  115. HttpPort = Cfg.MustValue("server", "http_port", "3000")
  116. port := os.Getenv("PORT")
  117. if port != "" {
  118. HttpPort = port
  119. }
  120. StaticRootPath = Cfg.MustValue("server", "static_root_path", workDir)
  121. RouterLogging = Cfg.MustBool("server", "router_logging", false)
  122. // PhantomJS rendering
  123. ImagesDir = "data/png"
  124. PhantomDir = "_vendor/phantomjs"
  125. LogRootPath = Cfg.MustValue("log", "root_path", path.Join(workDir, "/data/log"))
  126. }
  127. func initSessionService() {
  128. SessionProvider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
  129. SessionConfig = new(session.Config)
  130. SessionConfig.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
  131. SessionConfig.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
  132. SessionConfig.CookiePath = AppSubUrl
  133. SessionConfig.Secure = Cfg.MustBool("session", "cookie_secure")
  134. SessionConfig.EnableSetCookie = Cfg.MustBool("session", "enable_set_cookie", true)
  135. SessionConfig.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
  136. SessionConfig.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
  137. if SessionProvider == "file" {
  138. os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm)
  139. }
  140. log.Info("Session Service Enabled")
  141. }
  142. func InitServices() {
  143. initSessionService()
  144. }