Browse Source

Changes to config file loading

Torkel Ödegaard 11 years ago
parent
commit
29c9330965
6 changed files with 159 additions and 39 deletions
  1. 1 1
      .bra.toml
  2. 108 0
      conf/grafana.dev.ini
  3. 5 5
      conf/grafana.example.ini
  4. 1 1
      grafana
  5. 9 3
      pkg/cmd/web.go
  6. 35 29
      pkg/setting/setting.go

+ 1 - 1
.bra.toml

@@ -6,7 +6,7 @@ init_cmds = [
 watch_all = true
 watch_dirs = [
 	"$WORKDIR/pkg",
-	"$WORKDIR/templates",
+	"$WORKDIR/grafana/src/views",
 	"$WORKDIR/conf",
 ]
 watch_exts = [".go", ".ini"]

+ 108 - 0
conf/grafana.dev.ini

@@ -0,0 +1,108 @@
+app_name = Grafana
+app_mode = development
+
+[server]
+protocol = http
+domain = localhost
+root_url = %(protocol)s://%(domain)s:%(http_port)s/
+http_addr =
+http_port = 3000
+router_logging = false
+static_root_path = grafana/src
+
+[session]
+; Either "memory", "file", default is "memory"
+provider = file
+; Provider config options
+; memory: not have any config yet
+; file: session file path, e.g. `data/sessions`
+; redis: config like redis server addr, poolSize, password, e.g. `127.0.0.1:6379,100,grafana`
+; mysql: go-sql-driver/mysql dsn config string, e.g. `root:password@/session_table`
+provider_config = data/sessions
+; Session cookie name
+cookie_name = grafana_sess
+; If you use session in https only, default is false
+cookie_secure = false
+; Enable set cookie, default is true
+enable_set_cookie = true
+; Session GC time interval, default is 86400
+gc_time_interval = 86400
+; Session life time, default is 86400
+session_life_time = 86400
+; session id hash func, Either "sha1", "sha256" or "md5" default is sha1
+session_id_hashfunc = sha1
+; Session hash key, default is use random string
+session_id_hashkey =
+
+[oauth]
+enabled = true
+
+[oauth.github]
+enabled = true
+client_id = de054205006b9baa2e17
+client_secret = 72b7ea52d9f1096fdf36cea95e95362a307e0322
+scopes = user:email
+auth_url = https://github.com/login/oauth/authorize
+token_url = https://github.com/login/oauth/access_token
+
+[oauth.google]
+enabled = true
+client_id = 106011922963-4pvl05e9urtrm8bbqr0vouosj3e8p8kb.apps.googleusercontent.com
+client_secret = K2evIa4QhfbhhAm3SO72t2Zv
+scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
+auth_url = https://accounts.google.com/o/oauth2/auth
+token_url = https://accounts.google.com/o/oauth2/token
+
+[database]
+; Either "mysql", "postgres" or "sqlite3", it's your choice
+type = sqlite3
+host = 127.0.0.1:3306
+name = grafana
+user = root
+PASSWD =
+; For "postgres" only, either "disable", "require" or "verify-full"
+ssl_mode = disable
+; For "sqlite3" only
+path = data/grafana.db
+
+; [database]
+; ; Either "mysql", "postgres" or "sqlite3", it's your choice
+; type = postgres
+; host = 127.0.0.1:5432
+; name = grafana
+; user = grafana
+; password = grafana
+; ; For "postgres" only, either "disable", "require" or "verify-full"
+; ssl_mode = disable
+; ; For "sqlite3" only
+; path = data/grafana.db
+;
+[log]
+root_path =
+; Either "console", "file", "conn", "smtp" or "database", default is "console"
+; Use comma to separate multiple modes, e.g. "console, file"
+mode = console
+; Buffer length of channel, keep it as it is if you don't know what it is.
+buffer_len = 10000
+; Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Trace"
+level = Trace
+
+; For "console" mode only
+[log.console]
+level =
+
+; For "file" mode only
+[log.file]
+level =
+; This enables automated log rotate(switch of following options), default is true
+log_rotate = true
+; Max line number of single file, default is 1000000
+max_lines = 1000000
+; Max size shift of single file, default is 28 means 1 << 28, 256MB
+max_lines_shift = 28
+; Segment log daily, default is true
+daily_rotate = true
+; Expired days of log file(delete after max days), default is 7
+max_days = 7
+
+

+ 5 - 5
conf/grafana.ini → conf/grafana.example.ini

@@ -1,5 +1,5 @@
-app_name = Grafana Pro Server
-app_mode = dev
+app_name = Grafana
+app_mode = development
 
 [server]
 protocol = http
@@ -7,8 +7,8 @@ domain = localhost
 root_url = %(protocol)s://%(domain)s:%(http_port)s/
 http_addr =
 http_port = 3000
-ssh_port = 22
 router_logging = false
+static_root_path = public
 
 [session]
 ; Either "memory", "file", default is "memory"
@@ -16,11 +16,11 @@ provider = file
 ; Provider config options
 ; memory: not have any config yet
 ; file: session file path, e.g. `data/sessions`
-; redis: config like redis server addr, poolSize, password, e.g. `127.0.0.1:6379,100,gogs`
+; redis: config like redis server addr, poolSize, password, e.g. `127.0.0.1:6379,100,grafana`
 ; mysql: go-sql-driver/mysql dsn config string, e.g. `root:password@/session_table`
 provider_config = data/sessions
 ; Session cookie name
-cookie_name = grafana_pro_sess
+cookie_name = grafana_sess
 ; If you use session in https only, default is false
 cookie_secure = false
 ; Enable set cookie, default is true

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit 33e3fc70b286e0a1ebee901006e8f70b63b69198
+Subproject commit 7c72705bc5418ddf7030b3b50e86b236bd07e1fc

+ 9 - 3
pkg/cmd/web.go

@@ -25,7 +25,13 @@ var CmdWeb = cli.Command{
 	Usage:       "grafana web",
 	Description: "Starts Grafana backend & web server",
 	Action:      runWeb,
-	Flags:       []cli.Flag{},
+	Flags: []cli.Flag{
+		cli.StringFlag{
+			Name:  "config",
+			Value: "grafana.ini",
+			Usage: "path to config file",
+		},
+	},
 }
 
 func newMacaron() *macaron.Macaron {
@@ -61,8 +67,8 @@ func mapStatic(m *macaron.Macaron, dir string, prefix string) {
 	))
 }
 
-func runWeb(*cli.Context) {
-	log.Info("Starting Grafana-Pro v.2-alpha")
+func runWeb(c *cli.Context) {
+	log.Info("Starting Grafana 2.0-alpha")
 
 	setting.NewConfigContext()
 	setting.InitServices()

+ 35 - 29
pkg/setting/setting.go

@@ -57,6 +57,7 @@ var (
 	SessionOptions session.Options
 
 	// Global setting objects.
+	WorkDir      string
 	Cfg          *goconfig.ConfigFile
 	ConfRootPath string
 	CustomPath   string // Custom directory path.
@@ -74,42 +75,47 @@ func init() {
 	log.NewLogger(0, "console", `{"level": 0}`)
 }
 
-func WorkDir() (string, error) {
-	p, err := filepath.Abs(".")
-	if err != nil {
-		return "", err
-	}
-	return p, nil
+func getWorkDir() string {
+	p, _ := filepath.Abs(".")
+	return p
 }
 
-func NewConfigContext() {
-	workDir, err := WorkDir()
-	if err != nil {
-		log.Fatal(4, "Fail to get work directory: %v", err)
-	}
-	ConfRootPath = path.Join(workDir, "conf")
+func findConfigFile() string {
+	WorkDir = getWorkDir()
+	ConfRootPath = path.Join(WorkDir, "conf")
 
-	Cfg, err = goconfig.LoadConfigFile(path.Join(workDir, "conf/grafana.ini"))
-	if err != nil {
-		log.Fatal(4, "Fail to parse '%v/conf/grafana.ini': %v", workDir, err)
+	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
 	}
 
-	CustomPath = os.Getenv("GRAFANA_CONF")
+	log.Fatal(3, "Could not find any config file")
+	return ""
+}
 
-	if len(CustomPath) == 0 {
-		CustomPath = path.Join(workDir, "custom")
-	}
+func NewConfigContext() {
+	configFile := findConfigFile()
+
+	log.Info("Loading config file: %v", configFile)
+	var err error
 
-	cfgPath := path.Join(CustomPath, "conf/grafana.ini")
-	if com.IsFile(cfgPath) {
-		if err = Cfg.AppendFiles(cfgPath); err != nil {
-			log.Fatal(4, "Fail to load custom 'conf/grafana.ini': %v", err)
-		}
-	} else {
-		log.Warn("No custom 'conf/grafana.ini'")
+	Cfg, err = goconfig.LoadConfigFile(configFile)
+	if err != nil {
+		log.Fatal(4, "Fail to parse %v, error: %v", configFile, err)
 	}
 
-	AppName = Cfg.MustValue("", "app_name", "Grafana Pro")
+	AppName = Cfg.MustValue("", "app_name", "Grafana")
 	AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/")
 	if AppUrl[len(AppUrl)-1] != '/' {
 		AppUrl += "/"
@@ -137,14 +143,14 @@ func NewConfigContext() {
 		HttpPort = port
 	}
 
-	StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(workDir, "grafana/src"))
+	StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp"))
 	RouterLogging = Cfg.MustBool("server", "router_logging", false)
 
 	// PhantomJS rendering
 	ImagesDir = "data/png"
 	PhantomDir = "_vendor/phantomjs"
 
-	LogRootPath = Cfg.MustValue("log", "root_path", path.Join(workDir, "/data/log"))
+	LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log"))
 }
 
 func initSessionService() {