1
0
forked from MTSR/mapserver
mapserver/server/settings/settings.go

110 lines
2.0 KiB
Go
Raw Normal View History

2019-02-07 11:02:15 +03:00
package settings
import (
2019-02-08 18:02:24 +03:00
"mapserver/mapobjectdb"
"strconv"
2019-02-07 11:02:15 +03:00
)
const (
2019-02-08 18:02:24 +03:00
SETTING_LAST_MTIME = "last_mtime"
SETTING_LASTX = "last_x"
SETTING_LASTY = "last_y"
SETTING_LASTZ = "last_z"
SETTING_INITIAL_RUN = "initial_run"
SETTING_LEGACY_PROCESSED = "legacy_processed"
2019-02-07 11:02:15 +03:00
)
type Settings struct {
2019-02-08 18:02:24 +03:00
db mapobjectdb.DBAccessor
2019-02-07 11:02:15 +03:00
}
2019-02-08 18:02:24 +03:00
func New(db mapobjectdb.DBAccessor) *Settings {
return &Settings{
db: db,
}
2019-02-07 11:02:15 +03:00
}
func (this *Settings) GetString(key string, defaultValue string) string {
2019-02-08 18:02:24 +03:00
str, err := this.db.GetSetting(key, defaultValue)
if err != nil {
panic(err)
}
2019-02-07 11:02:15 +03:00
2019-02-08 18:02:24 +03:00
return str
2019-02-07 11:02:15 +03:00
}
func (this *Settings) SetString(key string, value string) {
2019-02-08 18:02:24 +03:00
err := this.db.SetSetting(key, value)
if err != nil {
panic(err)
}
2019-02-07 11:02:15 +03:00
}
func (this *Settings) GetInt(key string, defaultValue int) int {
2019-02-08 18:02:24 +03:00
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
if err != nil {
panic(err)
}
2019-02-07 11:02:15 +03:00
value, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
2019-02-08 18:02:24 +03:00
return value
2019-02-07 11:02:15 +03:00
}
func (this *Settings) SetInt(key string, value int) {
2019-02-08 18:02:24 +03:00
err := this.db.SetSetting(key, strconv.Itoa(value))
if err != nil {
2019-02-07 11:02:15 +03:00
panic(err)
}
}
func (this *Settings) GetInt64(key string, defaultValue int64) int64 {
2019-02-08 18:02:24 +03:00
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
if err != nil {
panic(err)
}
2019-02-07 11:02:15 +03:00
value, err := strconv.ParseInt(str, 10, 64)
if err != nil {
panic(err)
}
2019-02-08 18:02:24 +03:00
return value
2019-02-07 11:02:15 +03:00
}
func (this *Settings) SetInt64(key string, value int64) {
2019-02-08 18:02:24 +03:00
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
if err != nil {
2019-02-07 11:02:15 +03:00
panic(err)
}
}
2019-02-07 11:11:11 +03:00
func (this *Settings) GetBool(key string, defaultValue bool) bool {
2019-02-08 18:02:24 +03:00
defStr := "false"
if defaultValue {
defStr = "true"
}
2019-02-07 11:11:11 +03:00
2019-02-08 18:02:24 +03:00
str, err := this.db.GetSetting(key, defStr)
if err != nil {
panic(err)
}
2019-02-07 11:11:11 +03:00
2019-02-08 18:02:24 +03:00
return str == "true"
2019-02-07 11:11:11 +03:00
}
func (this *Settings) SetBool(key string, value bool) {
2019-02-08 18:02:24 +03:00
defStr := "false"
if value {
defStr = "true"
}
2019-02-07 11:11:11 +03:00
2019-02-08 18:02:24 +03:00
err := this.db.SetSetting(key, defStr)
if err != nil {
2019-02-07 11:11:11 +03:00
panic(err)
}
}