2014-10-05 09:50:04 -05:00
|
|
|
// Copyright 2014 Unknwon
|
|
|
|
// Copyright 2014 Torkel Ödegaard
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
|
|
|
"github.com/Unknwon/goconfig"
|
2014-10-05 09:50:04 -05:00
|
|
|
"github.com/macaron-contrib/session"
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Scheme string
|
|
|
|
|
|
|
|
const (
|
|
|
|
HTTP Scheme = "http"
|
|
|
|
HTTPS Scheme = "https"
|
|
|
|
)
|
|
|
|
|
2014-12-16 05:04:08 -06:00
|
|
|
const (
|
|
|
|
DEV string = "development"
|
|
|
|
PROD string = "production"
|
|
|
|
TEST string = "test"
|
|
|
|
)
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
var (
|
|
|
|
// App settings.
|
2014-12-16 05:04:08 -06:00
|
|
|
Env string = DEV
|
2014-10-04 06:33:20 -05:00
|
|
|
AppVer string
|
|
|
|
AppName string
|
|
|
|
AppUrl string
|
|
|
|
AppSubUrl string
|
|
|
|
|
|
|
|
// Log settings.
|
|
|
|
LogRootPath string
|
|
|
|
LogModes []string
|
|
|
|
LogConfigs []string
|
|
|
|
|
|
|
|
// Http server options
|
|
|
|
Protocol Scheme
|
|
|
|
Domain string
|
|
|
|
HttpAddr, HttpPort string
|
|
|
|
SshPort int
|
|
|
|
CertFile, KeyFile string
|
2014-10-05 09:50:04 -05:00
|
|
|
RouterLogging bool
|
|
|
|
StaticRootPath string
|
|
|
|
|
|
|
|
// Session settings.
|
2014-12-30 03:28:27 -06:00
|
|
|
SessionOptions session.Options
|
2014-10-04 06:33:20 -05:00
|
|
|
|
|
|
|
// Global setting objects.
|
2015-01-01 08:29:10 -06:00
|
|
|
WorkDir string
|
2014-10-04 06:33:20 -05:00
|
|
|
Cfg *goconfig.ConfigFile
|
|
|
|
ConfRootPath string
|
|
|
|
CustomPath string // Custom directory path.
|
|
|
|
ProdMode bool
|
|
|
|
RunUser string
|
|
|
|
IsWindows bool
|
2014-10-06 14:31:54 -05:00
|
|
|
|
|
|
|
// PhantomJs Rendering
|
|
|
|
ImagesDir string
|
|
|
|
PhantomDir string
|
2014-10-04 06:33:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
IsWindows = runtime.GOOS == "windows"
|
|
|
|
log.NewLogger(0, "console", `{"level": 0}`)
|
|
|
|
}
|
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
func getWorkDir() string {
|
|
|
|
p, _ := filepath.Abs(".")
|
|
|
|
return p
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
func findConfigFile() string {
|
|
|
|
WorkDir = getWorkDir()
|
|
|
|
ConfRootPath = path.Join(WorkDir, "conf")
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
configFile := path.Join(ConfRootPath, "grafana.ini")
|
|
|
|
//log.Info("Looking for config file: %v", configFile)
|
|
|
|
if com.IsFile(configFile) {
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
configFile = path.Join(ConfRootPath, "grafana.dev.ini")
|
|
|
|
//log.Info("Looking for config file: %v", configFile)
|
|
|
|
if com.IsFile(configFile) {
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
configFile = path.Join(ConfRootPath, "grafana.example.ini")
|
|
|
|
//log.Info("Looking for config file: %v", configFile)
|
|
|
|
if com.IsFile(configFile) {
|
|
|
|
return configFile
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
log.Fatal(3, "Could not find any config file")
|
|
|
|
return ""
|
|
|
|
}
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
func NewConfigContext() {
|
|
|
|
configFile := findConfigFile()
|
|
|
|
|
|
|
|
log.Info("Loading config file: %v", configFile)
|
|
|
|
var err error
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
Cfg, err = goconfig.LoadConfigFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(4, "Fail to parse %v, error: %v", configFile, err)
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
AppName = Cfg.MustValue("", "app_name", "Grafana")
|
2014-10-04 06:33:20 -05:00
|
|
|
AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
|
|
|
|
if AppUrl[len(AppUrl)-1] != '/' {
|
|
|
|
AppUrl += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if has app suburl.
|
|
|
|
url, err := url.Parse(AppUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
|
|
|
|
}
|
2015-01-04 14:03:40 -06:00
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
AppSubUrl = strings.TrimSuffix(url.Path, "/")
|
2015-01-04 14:03:40 -06:00
|
|
|
log.Info("AppSubUrl: %v", AppSubUrl)
|
2014-10-04 06:33:20 -05:00
|
|
|
|
|
|
|
Protocol = HTTP
|
|
|
|
if Cfg.MustValue("server", "protocol") == "https" {
|
|
|
|
Protocol = HTTPS
|
|
|
|
CertFile = Cfg.MustValue("server", "cert_file")
|
|
|
|
KeyFile = Cfg.MustValue("server", "key_file")
|
|
|
|
}
|
|
|
|
Domain = Cfg.MustValue("server", "domain", "localhost")
|
|
|
|
HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0")
|
|
|
|
HttpPort = Cfg.MustValue("server", "http_port", "3000")
|
2014-10-05 08:34:24 -05:00
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port != "" {
|
|
|
|
HttpPort = port
|
|
|
|
}
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp"))
|
2014-10-05 09:50:04 -05:00
|
|
|
RouterLogging = Cfg.MustBool("server", "router_logging", false)
|
2014-10-06 14:31:54 -05:00
|
|
|
|
|
|
|
// PhantomJS rendering
|
|
|
|
ImagesDir = "data/png"
|
|
|
|
PhantomDir = "_vendor/phantomjs"
|
2014-11-14 10:13:33 -06:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log"))
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func initSessionService() {
|
2014-12-30 03:28:27 -06:00
|
|
|
|
|
|
|
SessionOptions = session.Options{}
|
|
|
|
SessionOptions.Provider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"})
|
|
|
|
SessionOptions.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ")
|
|
|
|
SessionOptions.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess")
|
|
|
|
SessionOptions.CookiePath = AppSubUrl
|
|
|
|
SessionOptions.Secure = Cfg.MustBool("session", "cookie_secure")
|
|
|
|
SessionOptions.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400)
|
|
|
|
SessionOptions.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400)
|
|
|
|
|
|
|
|
if SessionOptions.Provider == "file" {
|
|
|
|
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Session Service Enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitServices() {
|
|
|
|
initSessionService()
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|