setting.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 findConfigFile() string {
  68. WorkDir = getWorkDir()
  69. ConfRootPath = path.Join(WorkDir, "conf")
  70. configFile := path.Join(ConfRootPath, "grafana.ini")
  71. //log.Info("Looking for config file: %v", configFile)
  72. if com.IsFile(configFile) {
  73. return configFile
  74. }
  75. configFile = path.Join(ConfRootPath, "grafana.dev.ini")
  76. //log.Info("Looking for config file: %v", configFile)
  77. if com.IsFile(configFile) {
  78. return configFile
  79. }
  80. configFile = path.Join(ConfRootPath, "grafana.example.ini")
  81. //log.Info("Looking for config file: %v", configFile)
  82. if com.IsFile(configFile) {
  83. return configFile
  84. }
  85. log.Fatal(3, "Could not find any config file")
  86. return ""
  87. }
  88. func NewConfigContext() {
  89. configFile := findConfigFile()
  90. log.Info("Loading config file: %v", configFile)
  91. var err error
  92. Cfg, err = goconfig.LoadConfigFile(configFile)
  93. if err != nil {
  94. log.Fatal(4, "Fail to parse %v, error: %v", configFile, err)
  95. }
  96. AppName = Cfg.MustValue("", "app_name", "Grafana")
  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. log.Info("AppSubUrl: %v", AppSubUrl)
  108. Protocol = HTTP
  109. if Cfg.MustValue("server", "protocol") == "https" {
  110. Protocol = HTTPS
  111. CertFile = Cfg.MustValue("server", "cert_file")
  112. KeyFile = Cfg.MustValue("server", "key_file")
  113. }
  114. Domain = Cfg.MustValue("server", "domain", "localhost")
  115. HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
  116. HttpPort = Cfg.MustValue("server", "http_port", "3000")
  117. port := os.Getenv("PORT")
  118. if port != "" {
  119. HttpPort = port
  120. }
  121. StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp"))
  122. RouterLogging = Cfg.MustBool("server", "router_logging", false)
  123. // PhantomJS rendering
  124. ImagesDir = "data/png"
  125. PhantomDir = "_vendor/phantomjs"
  126. LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log"))
  127. }
  128. func initSessionService() {
  129. SessionOptions = session.Options{}
  130. SessionOptions.Provider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
  131. SessionOptions.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
  132. SessionOptions.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
  133. SessionOptions.CookiePath = AppSubUrl
  134. SessionOptions.Secure = Cfg.MustBool("session", "cookie_secure")
  135. SessionOptions.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
  136. SessionOptions.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
  137. if SessionOptions.Provider == "file" {
  138. os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
  139. }
  140. log.Info("Session Service Enabled")
  141. }
  142. func InitServices() {
  143. initSessionService()
  144. }