Changed from goconfig to its new counter part go-ini

This commit is contained in:
Torkel Ödegaard 2015-01-27 10:09:54 +01:00
parent 951ce0a102
commit 95305e7e11
6 changed files with 99 additions and 65 deletions

View File

@ -1,5 +1,5 @@
app_name = Grafana app_name = Grafana
app_mode = development app_mode = production
[server] [server]
protocol = http protocol = http
@ -35,19 +35,22 @@ session_id_hashfunc = sha1
; Session hash key, default is use random string ; Session hash key, default is use random string
session_id_hashkey = session_id_hashkey =
[admin] [security]
; default admin user, created on startup ; default admin user, created on startup
user = admin admin_user = admin
; default admin password, can be changed before first start of grafana, or in profile settings ; default admin password, can be changed before first start of grafana, or in profile settings
password = admin admin_password = admin
; used for sig
secret_key = !#@FDEWREWR&*(
; Auto-login remember days
login_remember_days = 7
cookie_username = grafana_user
cookie_remember_name = grafana_remember
[auth] [auth]
anonymous = false anonymous = false
anonymous_account_id = anonymous_account_id =
[auth.grafana]
enabled = true
[auth.github] [auth.github]
enabled = false enabled = false
client_id = some_id client_id = some_id

View File

@ -22,7 +22,7 @@ func Register(r *macaron.Macaron) {
r.Post("/logout", LogoutPost) r.Post("/logout", LogoutPost)
r.Post("/login", bind(dtos.LoginCommand{}), LoginPost) r.Post("/login", bind(dtos.LoginCommand{}), LoginPost)
r.Get("/login/:name", OAuthLogin) r.Get("/login/:name", OAuthLogin)
r.Get("/login", Index) r.Get("/login", LoginView)
// authed views // authed views
r.Get("/profile/", reqSignedIn, Index) r.Get("/profile/", reqSignedIn, Index)

View File

@ -9,6 +9,15 @@ import (
"github.com/torkelo/grafana-pro/pkg/util" "github.com/torkelo/grafana-pro/pkg/util"
) )
func LoginView(c *middleware.Context) {
if err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
}
c.HTML(200, "index")
}
func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) { func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) {
userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User} userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User}

View File

@ -136,18 +136,20 @@ func getEngine() (*xorm.Engine, error) {
} }
func LoadConfig() { func LoadConfig() {
DbCfg.Type = setting.Cfg.MustValue("database", "type") sec := setting.Cfg.Section("database")
DbCfg.Type = sec.Key("type").String()
if DbCfg.Type == "sqlite3" { if DbCfg.Type == "sqlite3" {
UseSQLite3 = true UseSQLite3 = true
} }
DbCfg.Host = setting.Cfg.MustValue("database", "host") DbCfg.Host = sec.Key("host").String()
DbCfg.Name = setting.Cfg.MustValue("database", "name") DbCfg.Name = sec.Key("name").String()
DbCfg.User = setting.Cfg.MustValue("database", "user") DbCfg.User = sec.Key("user").String()
if len(DbCfg.Pwd) == 0 { if len(DbCfg.Pwd) == 0 {
DbCfg.Pwd = setting.Cfg.MustValue("database", "password") DbCfg.Pwd = sec.Key("password").String()
} }
DbCfg.SslMode = setting.Cfg.MustValue("database", "ssl_mode") DbCfg.SslMode = sec.Key("ssl_mode").String()
DbCfg.Path = setting.Cfg.MustValue("database", "path", "data/grafana.db") DbCfg.Path = sec.Key("path").MustString("data/grafana.db")
} }
type dbTransactionFunc func(sess *xorm.Session) error type dbTransactionFunc func(sess *xorm.Session) error

View File

@ -12,8 +12,8 @@ import (
"strings" "strings"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
"github.com/macaron-contrib/session" "github.com/macaron-contrib/session"
"gopkg.in/ini.v1"
"github.com/torkelo/grafana-pro/pkg/log" "github.com/torkelo/grafana-pro/pkg/log"
) )
@ -58,6 +58,12 @@ var (
StaticRootPath string StaticRootPath string
EnableGzip bool EnableGzip bool
// Security settings.
SecretKey string
LogInRememberDays int
CookieUserName string
CookieRememberName string
// Http auth // Http auth
AdminUser string AdminUser string
AdminPassword string AdminPassword string
@ -69,7 +75,7 @@ var (
// Global setting objects. // Global setting objects.
WorkDir string WorkDir string
Cfg *goconfig.ConfigFile Cfg *ini.File
ConfRootPath string ConfRootPath string
CustomPath string // Custom directory path. CustomPath string // Custom directory path.
ProdMode bool ProdMode bool
@ -118,85 +124,98 @@ func findConfigFiles() []string {
return filenames return filenames
} }
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
appUrl := section.Key("root_url").MustString("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)
}
appSubUrl := strings.TrimSuffix(url.Path, "/")
return appUrl, appSubUrl
}
func NewConfigContext() { func NewConfigContext() {
configFiles := findConfigFiles() configFiles := findConfigFiles()
//log.Info("Loading config files: %v", configFiles) //log.Info("Loading config files: %v", configFiles)
var err error var err error
Cfg, err = goconfig.LoadConfigFile(configFiles[0]) for i, file := range configFiles {
if err != nil { if i == 0 {
log.Fatal(4, "Fail to parse config file, error: %v", err) Cfg, err = ini.Load(configFiles[i])
} } else {
err = Cfg.Append(configFiles[i])
}
if len(configFiles) > 1 {
err = Cfg.AppendFiles(configFiles[1:]...)
if err != nil { if err != nil {
log.Fatal(4, "Fail to parse config file, error: %v", err) log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err)
} }
} }
AppName = Cfg.MustValue("", "app_name", "Grafana") AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/") Env = Cfg.Section("").Key("app_mode").MustString("development")
if AppUrl[len(AppUrl)-1] != '/' {
AppUrl += "/"
}
// Check if has app suburl. server := Cfg.Section("server")
url, err := url.Parse(AppUrl) AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
if err != nil {
log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err)
}
AppSubUrl = strings.TrimSuffix(url.Path, "/")
Protocol = HTTP Protocol = HTTP
if Cfg.MustValue("server", "protocol") == "https" { if server.Key("protocol").MustString("http") == "https" {
Protocol = HTTPS Protocol = HTTPS
CertFile = Cfg.MustValue("server", "cert_file") CertFile = server.Key("cert_file").String()
KeyFile = Cfg.MustValue("server", "key_file") KeyFile = server.Key("cert_file").String()
} }
Domain = Cfg.MustValue("server", "domain", "localhost")
HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0") Domain = server.Key("domain").MustString("localhost")
HttpPort = Cfg.MustValue("server", "http_port", "3000") HttpAddr = server.Key("http_addr").MustString("0.0.0.0")
HttpPort = server.Key("http_port").MustString("3000")
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port != "" { if port != "" {
HttpPort = port HttpPort = port
} }
StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp")) StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp"))
RouterLogging = Cfg.MustBool("server", "router_logging", false) RouterLogging = server.Key("router_logging").MustBool(false)
EnableGzip = Cfg.MustBool("server", "enable_gzip") EnableGzip = server.Key("enable_gzip").MustBool(false)
security := Cfg.Section("security")
SecretKey = security.Key("secret_key").String()
LogInRememberDays = security.Key("login_remember_days").MustInt()
CookieUserName = security.Key("cookie_username").String()
CookieRememberName = security.Key("cookie_remember_name").String()
// Http auth // Http auth
AdminUser = Cfg.MustValue("admin", "user", "admin") AdminUser = security.Key("admin_user").String()
AdminPassword = Cfg.MustValue("admin", "password", "admin") AdminPassword = security.Key("admin_password").String()
Anonymous = Cfg.MustBool("auth", "anonymous", false)
AnonymousAccountId = Cfg.MustInt64("auth", "anonymous_account_id", 0)
if Anonymous && AnonymousAccountId == 0 { // Anonymous = Cfg.MustBool("auth", "anonymous", false)
log.Fatal(3, "Must specify account id for anonymous access") // AnonymousAccountId = Cfg.MustInt64("auth", "anonymous_account_id", 0)
}
// PhantomJS rendering // PhantomJS rendering
ImagesDir = "data/png" ImagesDir = "data/png"
PhantomDir = "vendor/phantomjs" PhantomDir = "vendor/phantomjs"
LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log")) LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log"))
readSessionConfig() readSessionConfig()
} }
func readSessionConfig() { func readSessionConfig() {
sec := Cfg.Section("session")
SessionOptions = session.Options{} SessionOptions = session.Options{}
SessionOptions.Provider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"}) SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"})
SessionOptions.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ") SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
SessionOptions.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess") SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
SessionOptions.CookiePath = AppSubUrl SessionOptions.CookiePath = AppSubUrl
SessionOptions.Secure = Cfg.MustBool("session", "cookie_secure") SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
SessionOptions.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400) SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
SessionOptions.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400) SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
if SessionOptions.Provider == "file" { if SessionOptions.Provider == "file" {
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm) os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)

View File

@ -39,13 +39,14 @@ func NewOAuthService() {
allOauthes := []string{"github", "google"} allOauthes := []string{"github", "google"}
for _, name := range allOauthes { for _, name := range allOauthes {
sec := setting.Cfg.Section("auth." + name)
info := &setting.OAuthInfo{ info := &setting.OAuthInfo{
ClientId: setting.Cfg.MustValue("auth."+name, "client_id"), ClientId: sec.Key("client_id").String(),
ClientSecret: setting.Cfg.MustValue("auth."+name, "client_secret"), ClientSecret: sec.Key("client_secret").String(),
Scopes: setting.Cfg.MustValueArray("auth."+name, "scopes", " "), Scopes: sec.Key("scopes").Strings(" "),
AuthUrl: setting.Cfg.MustValue("auth."+name, "auth_url"), AuthUrl: sec.Key("auth_url").String(),
TokenUrl: setting.Cfg.MustValue("auth."+name, "token_url"), TokenUrl: sec.Key("token_url").String(),
Enabled: setting.Cfg.MustBool("auth."+name, "enabled"), Enabled: sec.Key("enabled").MustBool(),
} }
if !info.Enabled { if !info.Enabled {