Commit 5c31f079 by Torkel Ödegaard Committed by GitHub

Settings: Remove unnessary error handling code (#27420)

* Settings: Remove unnecessary error handling code

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 6fabfee0
...@@ -17,12 +17,12 @@ type DateFormatIntervals struct { ...@@ -17,12 +17,12 @@ type DateFormatIntervals struct {
func (cfg *Cfg) readDateFormats() { func (cfg *Cfg) readDateFormats() {
dateFormats := cfg.Raw.Section("date_formats") dateFormats := cfg.Raw.Section("date_formats")
cfg.DateFormats.FullDate, _ = valueAsString(dateFormats, "full_date", "YYYY-MM-DD HH:mm:ss") cfg.DateFormats.FullDate = valueAsString(dateFormats, "full_date", "YYYY-MM-DD HH:mm:ss")
cfg.DateFormats.Interval.Second, _ = valueAsString(dateFormats, "interval_second", "HH:mm:ss") cfg.DateFormats.Interval.Second = valueAsString(dateFormats, "interval_second", "HH:mm:ss")
cfg.DateFormats.Interval.Minute, _ = valueAsString(dateFormats, "interval_minute", "HH:mm") cfg.DateFormats.Interval.Minute = valueAsString(dateFormats, "interval_minute", "HH:mm")
cfg.DateFormats.Interval.Hour, _ = valueAsString(dateFormats, "interval_hour", "MM-DD HH:mm") cfg.DateFormats.Interval.Hour = valueAsString(dateFormats, "interval_hour", "MM-DD HH:mm")
cfg.DateFormats.Interval.Day, _ = valueAsString(dateFormats, "interval_day", "YYYY-MM-DD") cfg.DateFormats.Interval.Day = valueAsString(dateFormats, "interval_day", "YYYY-MM-DD")
cfg.DateFormats.Interval.Month, _ = valueAsString(dateFormats, "interval_month", "YYYY-MM") cfg.DateFormats.Interval.Month = valueAsString(dateFormats, "interval_month", "YYYY-MM")
cfg.DateFormats.Interval.Year = "YYYY" cfg.DateFormats.Interval.Year = "YYYY"
cfg.DateFormats.UseBrowserLocale = dateFormats.Key("date_format_use_browser_locale").MustBool(false) cfg.DateFormats.UseBrowserLocale = dateFormats.Key("date_format_use_browser_locale").MustBool(false)
} }
...@@ -5,7 +5,6 @@ package setting ...@@ -5,7 +5,6 @@ package setting
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
...@@ -337,10 +336,8 @@ func init() { ...@@ -337,10 +336,8 @@ func init() {
} }
func parseAppUrlAndSubUrl(section *ini.Section) (string, string, error) { func parseAppUrlAndSubUrl(section *ini.Section) (string, string, error) {
appUrl, err := valueAsString(section, "root_url", "http://localhost:3000/") appUrl := valueAsString(section, "root_url", "http://localhost:3000/")
if err != nil {
return "", "", err
}
if appUrl[len(appUrl)-1] != '/' { if appUrl[len(appUrl)-1] != '/' {
appUrl += "/" appUrl += "/"
} }
...@@ -350,8 +347,8 @@ func parseAppUrlAndSubUrl(section *ini.Section) (string, string, error) { ...@@ -350,8 +347,8 @@ func parseAppUrlAndSubUrl(section *ini.Section) (string, string, error) {
if err != nil { if err != nil {
log.Fatalf(4, "Invalid root_url(%s): %s", appUrl, err) log.Fatalf(4, "Invalid root_url(%s): %s", appUrl, err)
} }
appSubUrl := strings.TrimSuffix(url.Path, "/")
appSubUrl := strings.TrimSuffix(url.Path, "/")
return appUrl, appSubUrl, nil return appUrl, appSubUrl, nil
} }
...@@ -591,10 +588,8 @@ func (cfg *Cfg) loadConfiguration(args *CommandLineArgs) (*ini.File, error) { ...@@ -591,10 +588,8 @@ func (cfg *Cfg) loadConfiguration(args *CommandLineArgs) (*ini.File, error) {
} }
// update data path and logging config // update data path and logging config
dataPath, err := valueAsString(parsedFile.Section("paths"), "data", "") dataPath := valueAsString(parsedFile.Section("paths"), "data", "")
if err != nil {
return nil, err
}
cfg.DataPath = makeAbsolute(dataPath, HomePath) cfg.DataPath = makeAbsolute(dataPath, HomePath)
err = cfg.initLogging(parsedFile) err = cfg.initLogging(parsedFile)
if err != nil { if err != nil {
...@@ -676,25 +671,14 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -676,25 +671,14 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
ApplicationName = APP_NAME ApplicationName = APP_NAME
Env, err = valueAsString(iniFile.Section(""), "app_mode", "development") Env = valueAsString(iniFile.Section(""), "app_mode", "development")
if err != nil { InstanceName = valueAsString(iniFile.Section(""), "instance_name", "unknown_instance_name")
return err plugins := valueAsString(iniFile.Section("paths"), "plugins", "")
}
InstanceName, err = valueAsString(iniFile.Section(""), "instance_name", "unknown_instance_name")
if err != nil {
return err
}
plugins, err := valueAsString(iniFile.Section("paths"), "plugins", "")
if err != nil {
return err
}
PluginsPath = makeAbsolute(plugins, HomePath) PluginsPath = makeAbsolute(plugins, HomePath)
cfg.BundledPluginsPath = makeAbsolute("plugins-bundled", HomePath) cfg.BundledPluginsPath = makeAbsolute("plugins-bundled", HomePath)
provisioning, err := valueAsString(iniFile.Section("paths"), "provisioning", "") provisioning := valueAsString(iniFile.Section("paths"), "provisioning", "")
if err != nil {
return err
}
cfg.ProvisioningPath = makeAbsolute(provisioning, HomePath) cfg.ProvisioningPath = makeAbsolute(provisioning, HomePath)
if err := readServerSettings(iniFile, cfg); err != nil { if err := readServerSettings(iniFile, cfg); err != nil {
return err return err
} }
...@@ -716,10 +700,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -716,10 +700,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
// read dashboard settings // read dashboard settings
dashboards := iniFile.Section("dashboards") dashboards := iniFile.Section("dashboards")
DashboardVersionsToKeep = dashboards.Key("versions_to_keep").MustInt(20) DashboardVersionsToKeep = dashboards.Key("versions_to_keep").MustInt(20)
MinRefreshInterval, err = valueAsString(dashboards, "min_refresh_interval", "5s") MinRefreshInterval = valueAsString(dashboards, "min_refresh_interval", "5s")
if err != nil {
return err
}
cfg.DefaultHomeDashboardPath = dashboards.Key("default_home_dashboard_path").MustString("") cfg.DefaultHomeDashboardPath = dashboards.Key("default_home_dashboard_path").MustString("")
...@@ -735,14 +716,8 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -735,14 +716,8 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
cfg.TempDataLifetime = iniFile.Section("paths").Key("temp_data_lifetime").MustDuration(time.Second * 3600 * 24) cfg.TempDataLifetime = iniFile.Section("paths").Key("temp_data_lifetime").MustDuration(time.Second * 3600 * 24)
cfg.MetricsEndpointEnabled = iniFile.Section("metrics").Key("enabled").MustBool(true) cfg.MetricsEndpointEnabled = iniFile.Section("metrics").Key("enabled").MustBool(true)
cfg.MetricsEndpointBasicAuthUsername, err = valueAsString(iniFile.Section("metrics"), "basic_auth_username", "") cfg.MetricsEndpointBasicAuthUsername = valueAsString(iniFile.Section("metrics"), "basic_auth_username", "")
if err != nil { cfg.MetricsEndpointBasicAuthPassword = valueAsString(iniFile.Section("metrics"), "basic_auth_password", "")
return err
}
cfg.MetricsEndpointBasicAuthPassword, err = valueAsString(iniFile.Section("metrics"), "basic_auth_password", "")
if err != nil {
return err
}
cfg.MetricsEndpointDisableTotalStats = iniFile.Section("metrics").Key("disable_total_stats").MustBool(false) cfg.MetricsEndpointDisableTotalStats = iniFile.Section("metrics").Key("disable_total_stats").MustBool(false)
analytics := iniFile.Section("analytics") analytics := iniFile.Section("analytics")
...@@ -775,10 +750,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -775,10 +750,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
// Read and populate feature toggles list // Read and populate feature toggles list
featureTogglesSection := iniFile.Section("feature_toggles") featureTogglesSection := iniFile.Section("feature_toggles")
cfg.FeatureToggles = make(map[string]bool) cfg.FeatureToggles = make(map[string]bool)
featuresTogglesStr, err := valueAsString(featureTogglesSection, "enable", "") featuresTogglesStr := valueAsString(featureTogglesSection, "enable", "")
if err != nil {
return err
}
for _, feature := range util.SplitString(featuresTogglesStr) { for _, feature := range util.SplitString(featuresTogglesStr) {
cfg.FeatureToggles[feature] = true cfg.FeatureToggles[feature] = true
} }
...@@ -799,38 +771,21 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -799,38 +771,21 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
} }
// check old key name // check old key name
GrafanaComUrl, err = valueAsString(iniFile.Section("grafana_net"), "url", "") GrafanaComUrl = valueAsString(iniFile.Section("grafana_net"), "url", "")
if err != nil {
return err
}
if GrafanaComUrl == "" { if GrafanaComUrl == "" {
GrafanaComUrl, err = valueAsString(iniFile.Section("grafana_com"), "url", "https://grafana.com") GrafanaComUrl = valueAsString(iniFile.Section("grafana_com"), "url", "https://grafana.com")
if err != nil {
return err
}
} }
imageUploadingSection := iniFile.Section("external_image_storage") imageUploadingSection := iniFile.Section("external_image_storage")
ImageUploadProvider, err = valueAsString(imageUploadingSection, "provider", "") ImageUploadProvider = valueAsString(imageUploadingSection, "provider", "")
if err != nil {
return err
}
enterprise := iniFile.Section("enterprise") enterprise := iniFile.Section("enterprise")
cfg.EnterpriseLicensePath, err = valueAsString(enterprise, "license_path", filepath.Join(cfg.DataPath, "license.jwt")) cfg.EnterpriseLicensePath = valueAsString(enterprise, "license_path", filepath.Join(cfg.DataPath, "license.jwt"))
if err != nil {
return err
}
cacheServer := iniFile.Section("remote_cache") cacheServer := iniFile.Section("remote_cache")
dbName, err := valueAsString(cacheServer, "type", "database") dbName := valueAsString(cacheServer, "type", "database")
if err != nil { connStr := valueAsString(cacheServer, "connstr", "")
return err
}
connStr, err := valueAsString(cacheServer, "connstr", "")
if err != nil {
return err
}
cfg.RemoteCacheOptions = &RemoteCacheOptions{ cfg.RemoteCacheOptions = &RemoteCacheOptions{
Name: dbName, Name: dbName,
ConnStr: connStr, ConnStr: connStr,
...@@ -841,14 +796,8 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { ...@@ -841,14 +796,8 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
return nil return nil
} }
func valueAsString(section *ini.Section, keyName string, defaultValue string) (value string, err error) { func valueAsString(section *ini.Section, keyName string, defaultValue string) string {
defer func() { return section.Key(keyName).MustString(defaultValue)
if err_ := recover(); err_ != nil {
err = errors.New("Invalid value for key '" + keyName + "' in configuration file")
}
}()
return section.Key(keyName).MustString(defaultValue), nil
} }
type RemoteCacheOptions struct { type RemoteCacheOptions struct {
...@@ -876,20 +825,14 @@ func (cfg *Cfg) readSessionConfig() { ...@@ -876,20 +825,14 @@ func (cfg *Cfg) readSessionConfig() {
} }
func (cfg *Cfg) initLogging(file *ini.File) error { func (cfg *Cfg) initLogging(file *ini.File) error {
logModeStr, err := valueAsString(file.Section("log"), "mode", "console") logModeStr := valueAsString(file.Section("log"), "mode", "console")
if err != nil {
return err
}
// split on comma // split on comma
logModes := strings.Split(logModeStr, ",") logModes := strings.Split(logModeStr, ",")
// also try space // also try space
if len(logModes) == 1 { if len(logModes) == 1 {
logModes = strings.Split(logModeStr, " ") logModes = strings.Split(logModeStr, " ")
} }
logsPath, err := valueAsString(file.Section("paths"), "logs", "") logsPath := valueAsString(file.Section("paths"), "logs", "")
if err != nil {
return err
}
cfg.LogsPath = makeAbsolute(logsPath, HomePath) cfg.LogsPath = makeAbsolute(logsPath, HomePath)
return log.ReadLoggingConfig(logModes, cfg.LogsPath, file) return log.ReadLoggingConfig(logModes, cfg.LogsPath, file)
} }
...@@ -955,11 +898,7 @@ func (cfg *Cfg) SectionWithEnvOverrides(s string) *DynamicSection { ...@@ -955,11 +898,7 @@ func (cfg *Cfg) SectionWithEnvOverrides(s string) *DynamicSection {
func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error { func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error {
security := iniFile.Section("security") security := iniFile.Section("security")
var err error SecretKey = valueAsString(security, "secret_key", "")
SecretKey, err = valueAsString(security, "secret_key", "")
if err != nil {
return err
}
DisableGravatar = security.Key("disable_gravatar").MustBool(true) DisableGravatar = security.Key("disable_gravatar").MustBool(true)
cfg.DisableBruteForceLoginProtection = security.Key("disable_brute_force_login_protection").MustBool(false) cfg.DisableBruteForceLoginProtection = security.Key("disable_brute_force_login_protection").MustBool(false)
DisableBruteForceLoginProtection = cfg.DisableBruteForceLoginProtection DisableBruteForceLoginProtection = cfg.DisableBruteForceLoginProtection
...@@ -967,10 +906,7 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -967,10 +906,7 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error {
CookieSecure = security.Key("cookie_secure").MustBool(false) CookieSecure = security.Key("cookie_secure").MustBool(false)
cfg.CookieSecure = CookieSecure cfg.CookieSecure = CookieSecure
samesiteString, err := valueAsString(security, "cookie_samesite", "lax") samesiteString := valueAsString(security, "cookie_samesite", "lax")
if err != nil {
return err
}
if samesiteString == "disabled" { if samesiteString == "disabled" {
CookieSameSiteDisabled = true CookieSameSiteDisabled = true
...@@ -1001,24 +937,16 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1001,24 +937,16 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error {
// read data source proxy whitelist // read data source proxy whitelist
DataProxyWhiteList = make(map[string]bool) DataProxyWhiteList = make(map[string]bool)
securityStr, err := valueAsString(security, "data_source_proxy_whitelist", "") securityStr := valueAsString(security, "data_source_proxy_whitelist", "")
if err != nil {
return err
}
for _, hostAndIP := range util.SplitString(securityStr) { for _, hostAndIP := range util.SplitString(securityStr) {
DataProxyWhiteList[hostAndIP] = true DataProxyWhiteList[hostAndIP] = true
} }
// admin // admin
cfg.DisableInitAdminCreation = security.Key("disable_initial_admin_creation").MustBool(false) cfg.DisableInitAdminCreation = security.Key("disable_initial_admin_creation").MustBool(false)
AdminUser, err = valueAsString(security, "admin_user", "") AdminUser = valueAsString(security, "admin_user", "")
if err != nil { AdminPassword = valueAsString(security, "admin_password", "")
return err
}
AdminPassword, err = valueAsString(security, "admin_password", "")
if err != nil {
return err
}
return nil return nil
} }
...@@ -1026,11 +954,7 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1026,11 +954,7 @@ func readSecuritySettings(iniFile *ini.File, cfg *Cfg) error {
func readAuthSettings(iniFile *ini.File, cfg *Cfg) error { func readAuthSettings(iniFile *ini.File, cfg *Cfg) error {
auth := iniFile.Section("auth") auth := iniFile.Section("auth")
var err error LoginCookieName = valueAsString(auth, "login_cookie_name", "grafana_session")
LoginCookieName, err = valueAsString(auth, "login_cookie_name", "grafana_session")
if err != nil {
return err
}
cfg.LoginCookieName = LoginCookieName cfg.LoginCookieName = LoginCookieName
cfg.LoginMaxInactiveLifetimeDays = auth.Key("login_maximum_inactive_lifetime_days").MustInt(7) cfg.LoginMaxInactiveLifetimeDays = auth.Key("login_maximum_inactive_lifetime_days").MustInt(7)
...@@ -1047,24 +971,15 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1047,24 +971,15 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error {
DisableSignoutMenu = auth.Key("disable_signout_menu").MustBool(false) DisableSignoutMenu = auth.Key("disable_signout_menu").MustBool(false)
OAuthAutoLogin = auth.Key("oauth_auto_login").MustBool(false) OAuthAutoLogin = auth.Key("oauth_auto_login").MustBool(false)
cfg.OAuthCookieMaxAge = auth.Key("oauth_state_cookie_max_age").MustInt(600) cfg.OAuthCookieMaxAge = auth.Key("oauth_state_cookie_max_age").MustInt(600)
SignoutRedirectUrl, err = valueAsString(auth, "signout_redirect_url", "") SignoutRedirectUrl = valueAsString(auth, "signout_redirect_url", "")
if err != nil {
return err
}
// SAML auth // SAML auth
cfg.SAMLEnabled = iniFile.Section("auth.saml").Key("enabled").MustBool(false) cfg.SAMLEnabled = iniFile.Section("auth.saml").Key("enabled").MustBool(false)
// anonymous access // anonymous access
AnonymousEnabled = iniFile.Section("auth.anonymous").Key("enabled").MustBool(false) AnonymousEnabled = iniFile.Section("auth.anonymous").Key("enabled").MustBool(false)
AnonymousOrgName, err = valueAsString(iniFile.Section("auth.anonymous"), "org_name", "") AnonymousOrgName = valueAsString(iniFile.Section("auth.anonymous"), "org_name", "")
if err != nil { AnonymousOrgRole = valueAsString(iniFile.Section("auth.anonymous"), "org_role", "")
return err
}
AnonymousOrgRole, err = valueAsString(iniFile.Section("auth.anonymous"), "org_role", "")
if err != nil {
return err
}
cfg.AnonymousHideVersion = iniFile.Section("auth.anonymous").Key("hide_version").MustBool(false) cfg.AnonymousHideVersion = iniFile.Section("auth.anonymous").Key("hide_version").MustBool(false)
// basic auth // basic auth
...@@ -1074,14 +989,8 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1074,14 +989,8 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error {
authProxy := iniFile.Section("auth.proxy") authProxy := iniFile.Section("auth.proxy")
AuthProxyEnabled = authProxy.Key("enabled").MustBool(false) AuthProxyEnabled = authProxy.Key("enabled").MustBool(false)
AuthProxyHeaderName, err = valueAsString(authProxy, "header_name", "") AuthProxyHeaderName = valueAsString(authProxy, "header_name", "")
if err != nil { AuthProxyHeaderProperty = valueAsString(authProxy, "header_property", "")
return err
}
AuthProxyHeaderProperty, err = valueAsString(authProxy, "header_property", "")
if err != nil {
return err
}
AuthProxyAutoSignUp = authProxy.Key("auto_sign_up").MustBool(true) AuthProxyAutoSignUp = authProxy.Key("auto_sign_up").MustBool(true)
AuthProxyEnableLoginToken = authProxy.Key("enable_login_token").MustBool(false) AuthProxyEnableLoginToken = authProxy.Key("enable_login_token").MustBool(false)
...@@ -1095,16 +1004,11 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1095,16 +1004,11 @@ func readAuthSettings(iniFile *ini.File, cfg *Cfg) error {
AuthProxySyncTtl = syncVal AuthProxySyncTtl = syncVal
} }
AuthProxyWhitelist, err = valueAsString(authProxy, "whitelist", "") AuthProxyWhitelist = valueAsString(authProxy, "whitelist", "")
if err != nil {
return err
}
AuthProxyHeaders = make(map[string]string) AuthProxyHeaders = make(map[string]string)
headers, err := valueAsString(authProxy, "headers", "") headers := valueAsString(authProxy, "headers", "")
if err != nil {
return err
}
for _, propertyAndHeader := range util.SplitString(headers) { for _, propertyAndHeader := range util.SplitString(headers) {
split := strings.SplitN(propertyAndHeader, ":", 2) split := strings.SplitN(propertyAndHeader, ":", 2)
if len(split) == 2 { if len(split) == 2 {
...@@ -1123,31 +1027,14 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1123,31 +1027,14 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error {
AutoAssignOrgId = users.Key("auto_assign_org_id").MustInt(1) AutoAssignOrgId = users.Key("auto_assign_org_id").MustInt(1)
AutoAssignOrgRole = users.Key("auto_assign_org_role").In("Editor", []string{"Editor", "Admin", "Viewer"}) AutoAssignOrgRole = users.Key("auto_assign_org_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
VerifyEmailEnabled = users.Key("verify_email_enabled").MustBool(false) VerifyEmailEnabled = users.Key("verify_email_enabled").MustBool(false)
var err error
LoginHint, err = valueAsString(users, "login_hint", "") LoginHint = valueAsString(users, "login_hint", "")
if err != nil { PasswordHint = valueAsString(users, "password_hint", "")
return err DefaultTheme = valueAsString(users, "default_theme", "")
} ExternalUserMngLinkUrl = valueAsString(users, "external_manage_link_url", "")
PasswordHint, err = valueAsString(users, "password_hint", "") ExternalUserMngLinkName = valueAsString(users, "external_manage_link_name", "")
if err != nil { ExternalUserMngInfo = valueAsString(users, "external_manage_info", "")
return err
}
DefaultTheme, err = valueAsString(users, "default_theme", "")
if err != nil {
return err
}
ExternalUserMngLinkUrl, err = valueAsString(users, "external_manage_link_url", "")
if err != nil {
return err
}
ExternalUserMngLinkName, err = valueAsString(users, "external_manage_link_name", "")
if err != nil {
return err
}
ExternalUserMngInfo, err = valueAsString(users, "external_manage_info", "")
if err != nil {
return err
}
ViewersCanEdit = users.Key("viewers_can_edit").MustBool(false) ViewersCanEdit = users.Key("viewers_can_edit").MustBool(false)
cfg.EditorsCanAdmin = users.Key("editors_can_admin").MustBool(false) cfg.EditorsCanAdmin = users.Key("editors_can_admin").MustBool(false)
...@@ -1156,15 +1043,9 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1156,15 +1043,9 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error {
func readRenderingSettings(iniFile *ini.File, cfg *Cfg) error { func readRenderingSettings(iniFile *ini.File, cfg *Cfg) error {
renderSec := iniFile.Section("rendering") renderSec := iniFile.Section("rendering")
var err error cfg.RendererUrl = valueAsString(renderSec, "server_url", "")
cfg.RendererUrl, err = valueAsString(renderSec, "server_url", "") cfg.RendererCallbackUrl = valueAsString(renderSec, "callback_url", "")
if err != nil {
return err
}
cfg.RendererCallbackUrl, err = valueAsString(renderSec, "callback_url", "")
if err != nil {
return err
}
if cfg.RendererCallbackUrl == "" { if cfg.RendererCallbackUrl == "" {
cfg.RendererCallbackUrl = AppUrl cfg.RendererCallbackUrl = AppUrl
} else { } else {
...@@ -1177,8 +1058,8 @@ func readRenderingSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1177,8 +1058,8 @@ func readRenderingSettings(iniFile *ini.File, cfg *Cfg) error {
log.Fatalf(4, "Invalid callback_url(%s): %s", cfg.RendererCallbackUrl, err) log.Fatalf(4, "Invalid callback_url(%s): %s", cfg.RendererCallbackUrl, err)
} }
} }
cfg.RendererConcurrentRequestLimit = renderSec.Key("concurrent_render_request_limit").MustInt(30)
cfg.RendererConcurrentRequestLimit = renderSec.Key("concurrent_render_request_limit").MustInt(30)
cfg.ImagesDir = filepath.Join(cfg.DataPath, "png") cfg.ImagesDir = filepath.Join(cfg.DataPath, "png")
return nil return nil
...@@ -1189,15 +1070,9 @@ func readAlertingSettings(iniFile *ini.File) error { ...@@ -1189,15 +1070,9 @@ func readAlertingSettings(iniFile *ini.File) error {
AlertingEnabled = alerting.Key("enabled").MustBool(true) AlertingEnabled = alerting.Key("enabled").MustBool(true)
ExecuteAlerts = alerting.Key("execute_alerts").MustBool(true) ExecuteAlerts = alerting.Key("execute_alerts").MustBool(true)
AlertingRenderLimit = alerting.Key("concurrent_render_limit").MustInt(5) AlertingRenderLimit = alerting.Key("concurrent_render_limit").MustInt(5)
var err error
AlertingErrorOrTimeout, err = valueAsString(alerting, "error_or_timeout", "alerting") AlertingErrorOrTimeout = valueAsString(alerting, "error_or_timeout", "alerting")
if err != nil { AlertingNoDataOrNullValues = valueAsString(alerting, "nodata_or_nullvalues", "no_data")
return err
}
AlertingNoDataOrNullValues, err = valueAsString(alerting, "nodata_or_nullvalues", "no_data")
if err != nil {
return err
}
evaluationTimeoutSeconds := alerting.Key("evaluation_timeout_seconds").MustInt64(30) evaluationTimeoutSeconds := alerting.Key("evaluation_timeout_seconds").MustInt64(30)
AlertingEvaluationTimeout = time.Second * time.Duration(evaluationTimeoutSeconds) AlertingEvaluationTimeout = time.Second * time.Duration(evaluationTimeoutSeconds)
...@@ -1211,15 +1086,10 @@ func readAlertingSettings(iniFile *ini.File) error { ...@@ -1211,15 +1086,10 @@ func readAlertingSettings(iniFile *ini.File) error {
func readSnapshotsSettings(iniFile *ini.File) error { func readSnapshotsSettings(iniFile *ini.File) error {
snapshots := iniFile.Section("snapshots") snapshots := iniFile.Section("snapshots")
var err error
ExternalSnapshotUrl, err = valueAsString(snapshots, "external_snapshot_url", "") ExternalSnapshotUrl = valueAsString(snapshots, "external_snapshot_url", "")
if err != nil { ExternalSnapshotName = valueAsString(snapshots, "external_snapshot_name", "")
return err
}
ExternalSnapshotName, err = valueAsString(snapshots, "external_snapshot_name", "")
if err != nil {
return err
}
ExternalEnabled = snapshots.Key("external_enabled").MustBool(true) ExternalEnabled = snapshots.Key("external_enabled").MustBool(true)
SnapShotRemoveExpired = snapshots.Key("snapshot_remove_expired").MustBool(true) SnapShotRemoveExpired = snapshots.Key("snapshot_remove_expired").MustBool(true)
SnapshotPublicMode = snapshots.Key("public_mode").MustBool(false) SnapshotPublicMode = snapshots.Key("public_mode").MustBool(false)
...@@ -1241,10 +1111,8 @@ func readServerSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1241,10 +1111,8 @@ func readServerSettings(iniFile *ini.File, cfg *Cfg) error {
cfg.ServeFromSubPath = ServeFromSubPath cfg.ServeFromSubPath = ServeFromSubPath
Protocol = HTTP Protocol = HTTP
protocolStr, err := valueAsString(server, "protocol", "http") protocolStr := valueAsString(server, "protocol", "http")
if err != nil {
return err
}
if protocolStr == "https" { if protocolStr == "https" {
Protocol = HTTPS Protocol = HTTPS
CertFile = server.Key("cert_file").String() CertFile = server.Key("cert_file").String()
...@@ -1260,26 +1128,14 @@ func readServerSettings(iniFile *ini.File, cfg *Cfg) error { ...@@ -1260,26 +1128,14 @@ func readServerSettings(iniFile *ini.File, cfg *Cfg) error {
SocketPath = server.Key("socket").String() SocketPath = server.Key("socket").String()
} }
Domain, err = valueAsString(server, "domain", "localhost") Domain = valueAsString(server, "domain", "localhost")
if err != nil { HttpAddr = valueAsString(server, "http_addr", DEFAULT_HTTP_ADDR)
return err HttpPort = valueAsString(server, "http_port", "3000")
}
HttpAddr, err = valueAsString(server, "http_addr", DEFAULT_HTTP_ADDR)
if err != nil {
return err
}
HttpPort, err = valueAsString(server, "http_port", "3000")
if err != nil {
return err
}
RouterLogging = server.Key("router_logging").MustBool(false) RouterLogging = server.Key("router_logging").MustBool(false)
EnableGzip = server.Key("enable_gzip").MustBool(false) EnableGzip = server.Key("enable_gzip").MustBool(false)
EnforceDomain = server.Key("enforce_domain").MustBool(false) EnforceDomain = server.Key("enforce_domain").MustBool(false)
staticRoot, err := valueAsString(server, "static_root_path", "") staticRoot := valueAsString(server, "static_root_path", "")
if err != nil {
return err
}
StaticRootPath = makeAbsolute(staticRoot, HomePath) StaticRootPath = makeAbsolute(staticRoot, HomePath)
cfg.StaticRootPath = StaticRootPath cfg.StaticRootPath = StaticRootPath
......
...@@ -279,14 +279,12 @@ func TestLoadingSettings(t *testing.T) { ...@@ -279,14 +279,12 @@ func TestLoadingSettings(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
Convey("If key is found - should return value from ini file", func() { Convey("If key is found - should return value from ini file", func() {
value, err := valueAsString(iniFile.Section("server"), "alt_url", "") value := valueAsString(iniFile.Section("server"), "alt_url", "")
So(err, ShouldBeNil)
So(value, ShouldEqual, "https://grafana.com/") So(value, ShouldEqual, "https://grafana.com/")
}) })
Convey("If key is not found - should return default value", func() { Convey("If key is not found - should return default value", func() {
value, err := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val") value := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val")
So(err, ShouldBeNil)
So(value, ShouldEqual, "default_url_val") So(value, ShouldEqual, "default_url_val")
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment