mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Add RequiresRestart attribute to config option
This commit is contained in:
parent
456b53ae72
commit
05bead1890
2 changed files with 101 additions and 100 deletions
|
@ -1,127 +1,127 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
"github.com/safing/portbase/database/storage"
|
|
||||||
"github.com/safing/portbase/database/record"
|
|
||||||
"github.com/safing/portbase/database/query"
|
|
||||||
"github.com/safing/portbase/database/iterator"
|
"github.com/safing/portbase/database/iterator"
|
||||||
|
"github.com/safing/portbase/database/query"
|
||||||
|
"github.com/safing/portbase/database/record"
|
||||||
|
"github.com/safing/portbase/database/storage"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dbController *database.Controller
|
dbController *database.Controller
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigStorageInterface provices a storage.Interface to the configuration manager.
|
// StorageInterface provices a storage.Interface to the configuration manager.
|
||||||
type ConfigStorageInterface struct {
|
type StorageInterface struct {
|
||||||
storage.InjectBase
|
storage.InjectBase
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns a database record.
|
// Get returns a database record.
|
||||||
func (s *ConfigStorageInterface) Get(key string) (record.Record, error) {
|
func (s *StorageInterface) Get(key string) (record.Record, error) {
|
||||||
optionsLock.Lock()
|
optionsLock.Lock()
|
||||||
defer optionsLock.Unlock()
|
defer optionsLock.Unlock()
|
||||||
|
|
||||||
opt, ok := options[key]
|
opt, ok := options[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, storage.ErrNotFound
|
return nil, storage.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return opt.Export()
|
return opt.Export()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put stores a record in the database.
|
// Put stores a record in the database.
|
||||||
func (s *ConfigStorageInterface) Put(r record.Record) error {
|
func (s *StorageInterface) Put(r record.Record) error {
|
||||||
if r.Meta().Deleted > 0 {
|
if r.Meta().Deleted > 0 {
|
||||||
return setConfigOption(r.DatabaseKey(), nil, false)
|
return setConfigOption(r.DatabaseKey(), nil, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
acc := r.GetAccessor(r)
|
acc := r.GetAccessor(r)
|
||||||
if acc == nil {
|
if acc == nil {
|
||||||
return errors.New("invalid data")
|
return errors.New("invalid data")
|
||||||
}
|
}
|
||||||
|
|
||||||
val, ok := acc.Get("Value")
|
val, ok := acc.Get("Value")
|
||||||
if !ok || val == nil {
|
if !ok || val == nil {
|
||||||
return setConfigOption(r.DatabaseKey(), nil, false)
|
return setConfigOption(r.DatabaseKey(), nil, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
optionsLock.RLock()
|
optionsLock.RLock()
|
||||||
option, ok := options[r.DatabaseKey()]
|
option, ok := options[r.DatabaseKey()]
|
||||||
optionsLock.RUnlock()
|
optionsLock.RUnlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("config option does not exist")
|
return errors.New("config option does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
var value interface{}
|
var value interface{}
|
||||||
switch option.OptType {
|
switch option.OptType {
|
||||||
case OptTypeString :
|
case OptTypeString:
|
||||||
value, ok = acc.GetString("Value")
|
value, ok = acc.GetString("Value")
|
||||||
case OptTypeStringArray :
|
case OptTypeStringArray:
|
||||||
value, ok = acc.GetStringArray("Value")
|
value, ok = acc.GetStringArray("Value")
|
||||||
case OptTypeInt :
|
case OptTypeInt:
|
||||||
value, ok = acc.GetInt("Value")
|
value, ok = acc.GetInt("Value")
|
||||||
case OptTypeBool :
|
case OptTypeBool:
|
||||||
value, ok = acc.GetBool("Value")
|
value, ok = acc.GetBool("Value")
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("received invalid value in \"Value\"")
|
return errors.New("received invalid value in \"Value\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := setConfigOption(r.DatabaseKey(), value, false)
|
err := setConfigOption(r.DatabaseKey(), value, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes a record from the database.
|
// Delete deletes a record from the database.
|
||||||
func (s *ConfigStorageInterface) Delete(key string) error {
|
func (s *StorageInterface) Delete(key string) error {
|
||||||
return setConfigOption(key, nil, false)
|
return setConfigOption(key, nil, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query returns a an iterator for the supplied query.
|
// Query returns a an iterator for the supplied query.
|
||||||
func (s *ConfigStorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error) {
|
func (s *StorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error) {
|
||||||
|
|
||||||
optionsLock.Lock()
|
optionsLock.Lock()
|
||||||
defer optionsLock.Unlock()
|
defer optionsLock.Unlock()
|
||||||
|
|
||||||
it := iterator.New()
|
it := iterator.New()
|
||||||
var opts []*Option
|
var opts []*Option
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
if strings.HasPrefix(opt.Key, q.DatabaseKeyPrefix()) {
|
if strings.HasPrefix(opt.Key, q.DatabaseKeyPrefix()) {
|
||||||
opts = append(opts, opt)
|
opts = append(opts, opt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go s.processQuery(q, it, opts)
|
go s.processQuery(q, it, opts)
|
||||||
|
|
||||||
return it, nil
|
return it, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ConfigStorageInterface) processQuery(q *query.Query, it *iterator.Iterator, opts []*Option) {
|
func (s *StorageInterface) processQuery(q *query.Query, it *iterator.Iterator, opts []*Option) {
|
||||||
|
|
||||||
sort.Sort(sortableOptions(opts))
|
sort.Sort(sortableOptions(opts))
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
r, err := opt.Export()
|
r, err := opt.Export()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
it.Finish(err)
|
it.Finish(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
it.Next <- r
|
it.Next <- r
|
||||||
}
|
}
|
||||||
|
|
||||||
it.Finish(nil)
|
it.Finish(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOnly returns whether the database is read only.
|
// ReadOnly returns whether the database is read only.
|
||||||
func (s *ConfigStorageInterface) ReadOnly() bool {
|
func (s *StorageInterface) ReadOnly() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,33 +132,33 @@ func registerAsDatabase() error {
|
||||||
StorageType: "injected",
|
StorageType: "injected",
|
||||||
PrimaryAPI: "",
|
PrimaryAPI: "",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
controller, err := database.InjectDatabase("config", &ConfigStorageInterface{})
|
controller, err := database.InjectDatabase("config", &StorageInterface{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dbController = controller
|
dbController = controller
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushFullUpdate() {
|
func pushFullUpdate() {
|
||||||
optionsLock.RLock()
|
optionsLock.RLock()
|
||||||
defer optionsLock.RUnlock()
|
defer optionsLock.RUnlock()
|
||||||
|
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
pushUpdate(option)
|
pushUpdate(option)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushUpdate(option *Option) {
|
func pushUpdate(option *Option) {
|
||||||
r, err := option.Export()
|
r, err := option.Export()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to export option to push update: %s", err)
|
log.Errorf("failed to export option to push update: %s", err)
|
||||||
} else {
|
} else {
|
||||||
dbController.PushUpdate(r)
|
dbController.PushUpdate(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@ type Option struct {
|
||||||
DefaultValue interface{}
|
DefaultValue interface{}
|
||||||
ExternalOptType string
|
ExternalOptType string
|
||||||
ValidationRegex string
|
ValidationRegex string
|
||||||
|
RequiresRestart bool
|
||||||
compiledRegex *regexp.Regexp
|
compiledRegex *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue