check numeric setting value, add numeric value range comment in config file
This commit is contained in:
@@ -53,13 +53,13 @@ ssl_mode = disable
|
|||||||
# For "sqlite3" only, db file path (relative or absolute path)
|
# For "sqlite3" only, db file path (relative or absolute path)
|
||||||
db_path = data/ezbookkeeping.db
|
db_path = data/ezbookkeeping.db
|
||||||
|
|
||||||
# Max idle connection number, default is 2
|
# Max idle connection number (0 - 65535, 0 means no idle connections are retained), default is 2
|
||||||
max_idle_conn = 2
|
max_idle_conn = 2
|
||||||
|
|
||||||
# Max opened connection number, default is 0 (unlimited)
|
# Max opened connection number (0 - 65535), default is 0 (unlimited)
|
||||||
max_open_conn = 0
|
max_open_conn = 0
|
||||||
|
|
||||||
# Max connection lifetime (seconds), default is 14400 (4 hours)
|
# Max connection lifetime (0 - 4294967295 seconds), default is 14400 (4 hours)
|
||||||
conn_max_lifetime = 14400
|
conn_max_lifetime = 14400
|
||||||
|
|
||||||
# Set to true to log each sql statement and execution time
|
# Set to true to log each sql statement and execution time
|
||||||
@@ -83,7 +83,7 @@ log_path = log/ezbookkeeping.log
|
|||||||
# Uuid generator type, supports "internal" currently
|
# Uuid generator type, supports "internal" currently
|
||||||
generator_type = internal
|
generator_type = internal
|
||||||
|
|
||||||
# For "internal" only, each server must have unique id
|
# For "internal" only, each server must have unique id (0 - 255)
|
||||||
server_id = 0
|
server_id = 0
|
||||||
|
|
||||||
[security]
|
[security]
|
||||||
@@ -93,10 +93,10 @@ secret_key =
|
|||||||
# Set to true to enable two factor authorization
|
# Set to true to enable two factor authorization
|
||||||
enable_two_factor = true
|
enable_two_factor = true
|
||||||
|
|
||||||
# Token expired seconds, default is 2592000 (30 days)
|
# Token expired seconds (0 - 4294967295), default is 2592000 (30 days)
|
||||||
token_expired_time = 2592000
|
token_expired_time = 2592000
|
||||||
|
|
||||||
# Temporary token expired seconds, default is 300 (5 minutes)
|
# Temporary token expired seconds (0 - 4294967295), default is 300 (5 minutes)
|
||||||
temporary_token_expired_time = 300
|
temporary_token_expired_time = 300
|
||||||
|
|
||||||
# Add X-Request-Id header to response to track user request or error, default is true
|
# Add X-Request-Id header to response to track user request or error, default is true
|
||||||
@@ -114,5 +114,5 @@ enable_export = true
|
|||||||
# Exchange rates data source, supports "euro_central_bank", "bank_of_canada", "reserve_bank_of_australia", "czech_national_bank", "national_bank_of_poland" currently
|
# Exchange rates data source, supports "euro_central_bank", "bank_of_canada", "reserve_bank_of_australia", "czech_national_bank", "national_bank_of_poland" currently
|
||||||
data_source = euro_central_bank
|
data_source = euro_central_bank
|
||||||
|
|
||||||
# Requesting exchange rates data timeout (milliseconds), default is 10000 (10 seconds)
|
# Requesting exchange rates data timeout (0 - 4294967295 milliseconds), default is 10000 (10 seconds)
|
||||||
request_timeout = 10000
|
request_timeout = 10000
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ func initializeDatabase(dbConfig *settings.DatabaseConfig) (*Database, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
engineGroup.SetMaxIdleConns(dbConfig.MaxIdleConnection)
|
engineGroup.SetMaxIdleConns(int(dbConfig.MaxIdleConnection))
|
||||||
engineGroup.SetMaxOpenConns(dbConfig.MaxOpenConnection)
|
engineGroup.SetMaxOpenConns(int(dbConfig.MaxOpenConnection))
|
||||||
engineGroup.SetConnMaxLifetime(time.Duration(dbConfig.ConnectionMaxLifeTime) * time.Second)
|
engineGroup.SetConnMaxLifetime(time.Duration(dbConfig.ConnectionMaxLifeTime) * time.Second)
|
||||||
|
|
||||||
return &Database{
|
return &Database{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func ServerSettingsCookie(config *settings.Config) core.MiddlewareHandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bundledSettings := strings.Join(settingsArr, "_")
|
bundledSettings := strings.Join(settingsArr, "_")
|
||||||
c.SetCookie(settingsCookieName, bundledSettings, config.TokenExpiredTime, "", "", false, false)
|
c.SetCookie(settingsCookieName, bundledSettings, int(config.TokenExpiredTime), "", "", false, false)
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|||||||
+79
-29
@@ -75,23 +75,23 @@ const (
|
|||||||
defaultAppName string = "ezBookkeeping"
|
defaultAppName string = "ezBookkeeping"
|
||||||
|
|
||||||
defaultHttpAddr string = "0.0.0.0"
|
defaultHttpAddr string = "0.0.0.0"
|
||||||
defaultHttpPort int = 8080
|
defaultHttpPort uint16 = 8080
|
||||||
defaultDomain string = "localhost"
|
defaultDomain string = "localhost"
|
||||||
|
|
||||||
defaultDatabaseHost string = "127.0.0.1:3306"
|
defaultDatabaseHost string = "127.0.0.1:3306"
|
||||||
defaultDatabaseName string = "ezbookkeeping"
|
defaultDatabaseName string = "ezbookkeeping"
|
||||||
defaultDatabaseMaxIdleConn int = 2
|
defaultDatabaseMaxIdleConn uint16 = 2
|
||||||
defaultDatabaseMaxOpenConn int = 0
|
defaultDatabaseMaxOpenConn uint16 = 0
|
||||||
defaultDatabaseConnMaxLifetime int = 14400
|
defaultDatabaseConnMaxLifetime uint32 = 14400
|
||||||
|
|
||||||
defaultLogMode string = "console"
|
defaultLogMode string = "console"
|
||||||
defaultLoglevel Level = LOGLEVEL_INFO
|
defaultLoglevel Level = LOGLEVEL_INFO
|
||||||
|
|
||||||
defaultSecretKey string = "ezbookkeeping"
|
defaultSecretKey string = "ezbookkeeping"
|
||||||
defaultTokenExpiredTime int = 604800 // 7 days
|
defaultTokenExpiredTime uint32 = 604800 // 7 days
|
||||||
defaultTemporaryTokenExpiredTime int = 300 // 5 minutes
|
defaultTemporaryTokenExpiredTime uint32 = 300 // 5 minutes
|
||||||
|
|
||||||
defaultExchangeRatesDataRequestTimeout int = 10000 // 10 seconds
|
defaultExchangeRatesDataRequestTimeout uint32 = 10000 // 10 seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
// DatabaseConfig represents the database setting config
|
// DatabaseConfig represents the database setting config
|
||||||
@@ -106,9 +106,9 @@ type DatabaseConfig struct {
|
|||||||
|
|
||||||
DatabasePath string
|
DatabasePath string
|
||||||
|
|
||||||
MaxIdleConnection int
|
MaxIdleConnection uint16
|
||||||
MaxOpenConnection int
|
MaxOpenConnection uint16
|
||||||
ConnectionMaxLifeTime int
|
ConnectionMaxLifeTime uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config represents the global setting config
|
// Config represents the global setting config
|
||||||
@@ -121,7 +121,7 @@ type Config struct {
|
|||||||
// Server
|
// Server
|
||||||
Protocol Scheme
|
Protocol Scheme
|
||||||
HttpAddr string
|
HttpAddr string
|
||||||
HttpPort int
|
HttpPort uint16
|
||||||
|
|
||||||
Domain string
|
Domain string
|
||||||
RootUrl string
|
RootUrl string
|
||||||
@@ -156,9 +156,9 @@ type Config struct {
|
|||||||
// Secret
|
// Secret
|
||||||
SecretKey string
|
SecretKey string
|
||||||
EnableTwoFactor bool
|
EnableTwoFactor bool
|
||||||
TokenExpiredTime int
|
TokenExpiredTime uint32
|
||||||
TokenExpiredTimeDuration time.Duration
|
TokenExpiredTimeDuration time.Duration
|
||||||
TemporaryTokenExpiredTime int
|
TemporaryTokenExpiredTime uint32
|
||||||
TemporaryTokenExpiredTimeDuration time.Duration
|
TemporaryTokenExpiredTimeDuration time.Duration
|
||||||
EnableRequestIdHeader bool
|
EnableRequestIdHeader bool
|
||||||
|
|
||||||
@@ -170,7 +170,7 @@ type Config struct {
|
|||||||
|
|
||||||
// Exchange Rates
|
// Exchange Rates
|
||||||
ExchangeRatesDataSource string
|
ExchangeRatesDataSource string
|
||||||
ExchangeRatesRequestTimeout int
|
ExchangeRatesRequestTimeout uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfiguration loads setting config from given config file path
|
// LoadConfiguration loads setting config from given config file path
|
||||||
@@ -281,12 +281,12 @@ func loadServerConfiguration(config *Config, configFile *ini.File, sectionName s
|
|||||||
config.Protocol = SCHEME_HTTP
|
config.Protocol = SCHEME_HTTP
|
||||||
|
|
||||||
config.HttpAddr = getConfigItemStringValue(configFile, sectionName, "http_addr", defaultHttpAddr)
|
config.HttpAddr = getConfigItemStringValue(configFile, sectionName, "http_addr", defaultHttpAddr)
|
||||||
config.HttpPort = getConfigItemIntValue(configFile, sectionName, "http_port", defaultHttpPort)
|
config.HttpPort = getConfigItemUint16Value(configFile, sectionName, "http_port", defaultHttpPort)
|
||||||
} else if getConfigItemStringValue(configFile, sectionName, "protocol") == "https" {
|
} else if getConfigItemStringValue(configFile, sectionName, "protocol") == "https" {
|
||||||
config.Protocol = SCHEME_HTTPS
|
config.Protocol = SCHEME_HTTPS
|
||||||
|
|
||||||
config.HttpAddr = getConfigItemStringValue(configFile, sectionName, "http_addr", defaultHttpAddr)
|
config.HttpAddr = getConfigItemStringValue(configFile, sectionName, "http_addr", defaultHttpAddr)
|
||||||
config.HttpPort = getConfigItemIntValue(configFile, sectionName, "http_port", defaultHttpPort)
|
config.HttpPort = getConfigItemUint16Value(configFile, sectionName, "http_port", defaultHttpPort)
|
||||||
|
|
||||||
config.CertFile = getConfigItemStringValue(configFile, sectionName, "cert_file")
|
config.CertFile = getConfigItemStringValue(configFile, sectionName, "cert_file")
|
||||||
config.CertKeyFile = getConfigItemStringValue(configFile, sectionName, "cert_key_file")
|
config.CertKeyFile = getConfigItemStringValue(configFile, sectionName, "cert_key_file")
|
||||||
@@ -339,9 +339,9 @@ func loadDatabaseConfiguration(config *Config, configFile *ini.File, sectionName
|
|||||||
dbConfig.DatabasePath = finalStaticDBPath
|
dbConfig.DatabasePath = finalStaticDBPath
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfig.MaxIdleConnection = getConfigItemIntValue(configFile, sectionName, "max_idle_conn", defaultDatabaseMaxIdleConn)
|
dbConfig.MaxIdleConnection = getConfigItemUint16Value(configFile, sectionName, "max_idle_conn", defaultDatabaseMaxIdleConn)
|
||||||
dbConfig.MaxOpenConnection = getConfigItemIntValue(configFile, sectionName, "max_open_conn", defaultDatabaseMaxOpenConn)
|
dbConfig.MaxOpenConnection = getConfigItemUint16Value(configFile, sectionName, "max_open_conn", defaultDatabaseMaxOpenConn)
|
||||||
dbConfig.ConnectionMaxLifeTime = getConfigItemIntValue(configFile, sectionName, "conn_max_lifetime", defaultDatabaseConnMaxLifetime)
|
dbConfig.ConnectionMaxLifeTime = getConfigItemUint32Value(configFile, sectionName, "conn_max_lifetime", defaultDatabaseConnMaxLifetime)
|
||||||
|
|
||||||
config.DatabaseConfig = dbConfig
|
config.DatabaseConfig = dbConfig
|
||||||
config.EnableQueryLog = getConfigItemBoolValue(configFile, sectionName, "log_query", false)
|
config.EnableQueryLog = getConfigItemBoolValue(configFile, sectionName, "log_query", false)
|
||||||
@@ -383,7 +383,7 @@ func loadUuidConfiguration(config *Config, configFile *ini.File, sectionName str
|
|||||||
return errs.ErrInvalidUuidMode
|
return errs.ErrInvalidUuidMode
|
||||||
}
|
}
|
||||||
|
|
||||||
config.UuidServerId = uint8(getConfigItemIntValue(configFile, sectionName, "server_id", 0))
|
config.UuidServerId = getConfigItemUint8Value(configFile, sectionName, "server_id", 0)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -392,10 +392,10 @@ func loadSecurityConfiguration(config *Config, configFile *ini.File, sectionName
|
|||||||
config.SecretKey = getConfigItemStringValue(configFile, sectionName, "secret_key", defaultSecretKey)
|
config.SecretKey = getConfigItemStringValue(configFile, sectionName, "secret_key", defaultSecretKey)
|
||||||
config.EnableTwoFactor = getConfigItemBoolValue(configFile, sectionName, "enable_two_factor", true)
|
config.EnableTwoFactor = getConfigItemBoolValue(configFile, sectionName, "enable_two_factor", true)
|
||||||
|
|
||||||
config.TokenExpiredTime = getConfigItemIntValue(configFile, sectionName, "token_expired_time", defaultTokenExpiredTime)
|
config.TokenExpiredTime = getConfigItemUint32Value(configFile, sectionName, "token_expired_time", defaultTokenExpiredTime)
|
||||||
config.TokenExpiredTimeDuration = time.Duration(config.TokenExpiredTime) * time.Second
|
config.TokenExpiredTimeDuration = time.Duration(config.TokenExpiredTime) * time.Second
|
||||||
|
|
||||||
config.TemporaryTokenExpiredTime = getConfigItemIntValue(configFile, sectionName, "temporary_token_expired_time", defaultTemporaryTokenExpiredTime)
|
config.TemporaryTokenExpiredTime = getConfigItemUint32Value(configFile, sectionName, "temporary_token_expired_time", defaultTemporaryTokenExpiredTime)
|
||||||
config.TemporaryTokenExpiredTimeDuration = time.Duration(config.TemporaryTokenExpiredTime) * time.Second
|
config.TemporaryTokenExpiredTimeDuration = time.Duration(config.TemporaryTokenExpiredTime) * time.Second
|
||||||
|
|
||||||
config.EnableRequestIdHeader = getConfigItemBoolValue(configFile, sectionName, "request_id_header", true)
|
config.EnableRequestIdHeader = getConfigItemBoolValue(configFile, sectionName, "request_id_header", true)
|
||||||
@@ -430,7 +430,7 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio
|
|||||||
return errs.ErrInvalidExchangeRatesDataSource
|
return errs.ErrInvalidExchangeRatesDataSource
|
||||||
}
|
}
|
||||||
|
|
||||||
config.ExchangeRatesRequestTimeout = getConfigItemIntValue(configFile, sectionName, "request_timeout", defaultExchangeRatesDataRequestTimeout)
|
config.ExchangeRatesRequestTimeout = getConfigItemUint32Value(configFile, sectionName, "request_timeout", defaultExchangeRatesDataRequestTimeout)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -482,23 +482,73 @@ func getConfigItemStringValue(configFile *ini.File, sectionName string, itemName
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemIntValue(configFile *ini.File, sectionName string, itemName string, defaultValue ...int) int {
|
func getConfigItemUint8Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint8) uint8 {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentKey := getEnvironmentKey(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
environmentValue := os.Getenv(environmentKey)
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
value, err := strconv.ParseInt(environmentValue, 0, 64)
|
value, err := strconv.ParseUint(environmentValue, 10, 8)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return int(value)
|
return uint8(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
section := configFile.Section(sectionName)
|
section := configFile.Section(sectionName)
|
||||||
return section.Key(itemName).MustInt(defaultValue...)
|
value, err := strconv.ParseUint(section.Key(itemName).String(), 10, 8)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return uint8(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName string, defaultValue ...bool) bool {
|
func getConfigItemUint16Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint16) uint16 {
|
||||||
|
environmentKey := getEnvironmentKey(sectionName, itemName)
|
||||||
|
environmentValue := os.Getenv(environmentKey)
|
||||||
|
|
||||||
|
if len(environmentValue) > 0 {
|
||||||
|
value, err := strconv.ParseUint(environmentValue, 10, 16)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return uint16(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
section := configFile.Section(sectionName)
|
||||||
|
value, err := strconv.ParseUint(section.Key(itemName).String(), 10, 16)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return uint16(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigItemUint32Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint32) uint32 {
|
||||||
|
environmentKey := getEnvironmentKey(sectionName, itemName)
|
||||||
|
environmentValue := os.Getenv(environmentKey)
|
||||||
|
|
||||||
|
if len(environmentValue) > 0 {
|
||||||
|
value, err := strconv.ParseUint(environmentValue, 10, 32)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return uint32(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
section := configFile.Section(sectionName)
|
||||||
|
value, err := strconv.ParseUint(section.Key(itemName).String(), 10, 32)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return uint32(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName string, defaultValue bool) bool {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentKey := getEnvironmentKey(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
environmentValue := os.Getenv(environmentKey)
|
||||||
|
|
||||||
@@ -511,7 +561,7 @@ func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName s
|
|||||||
}
|
}
|
||||||
|
|
||||||
section := configFile.Section(sectionName)
|
section := configFile.Section(sectionName)
|
||||||
return section.Key(itemName).MustBool(defaultValue...)
|
return section.Key(itemName).MustBool(defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEnvironmentKey(sectionName string, itemName string) string {
|
func getEnvironmentKey(sectionName string, itemName string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user