setting.go 4.3 KB

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