mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 08:22:24 +01:00
improve (config): listen for config changes for reactive configuration
This commit is contained in:
parent
ba0243fa5d
commit
f5f0f30cea
5 changed files with 57 additions and 21 deletions
|
|
@ -20,7 +20,7 @@ var (
|
|||
)
|
||||
|
||||
type Configuration struct {
|
||||
OnChange chan interface{}
|
||||
onChange []ChangeListener
|
||||
mu sync.Mutex
|
||||
currentElement *FormElement
|
||||
cache KeyValueStore
|
||||
|
|
@ -60,9 +60,9 @@ func init() {
|
|||
|
||||
func NewConfiguration() Configuration {
|
||||
return Configuration{
|
||||
OnChange: make(chan interface{}),
|
||||
mu: sync.Mutex{},
|
||||
cache: NewKeyValueStore(),
|
||||
onChange: make([]ChangeListener, 0),
|
||||
mu: sync.Mutex{},
|
||||
cache: NewKeyValueStore(),
|
||||
form: []Form{
|
||||
Form{
|
||||
Title: "general",
|
||||
|
|
@ -240,7 +240,12 @@ func (this *Configuration) Load() {
|
|||
this.cache.Clear()
|
||||
|
||||
Log.SetVisibility(this.Get("log.level").String())
|
||||
go func() { this.OnChange <- nil }()
|
||||
|
||||
go func() { // Trigger all the event listeners
|
||||
for i:=0; i<len(this.onChange); i++ {
|
||||
this.onChange[i].Listener <- nil
|
||||
}
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -496,7 +501,7 @@ func (this Configuration) Interface() interface{} {
|
|||
}
|
||||
|
||||
func (this Configuration) MarshalJSON() ([]byte, error) {
|
||||
form := this.form
|
||||
form := this.form
|
||||
form = append(form, Form{
|
||||
Title: "constant",
|
||||
Elmnts: []FormElement{
|
||||
|
|
@ -527,3 +532,34 @@ func (this Configuration) MarshalJSON() ([]byte, error) {
|
|||
Form: form,
|
||||
}.MarshalJSON()
|
||||
}
|
||||
|
||||
func (this *Configuration) ListenForChange() ChangeListener {
|
||||
this.mu.Lock()
|
||||
change := ChangeListener{
|
||||
Id: QuickString(20),
|
||||
Listener: make(chan interface{}, 0),
|
||||
}
|
||||
this.onChange = append(this.onChange, change)
|
||||
this.mu.Unlock()
|
||||
return change
|
||||
}
|
||||
|
||||
func (this *Configuration) UnlistenForChange(c ChangeListener) {
|
||||
this.mu.Lock()
|
||||
for i:=0; i<len(this.onChange); i++ {
|
||||
if this.onChange[i].Id == c.Id {
|
||||
if len(this.onChange) - 1 >= 0 {
|
||||
close(this.onChange[i].Listener)
|
||||
this.onChange[i] = this.onChange[len(this.onChange)-1]
|
||||
this.onChange = this.onChange[:len(this.onChange)-1]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
this.mu.Unlock()
|
||||
}
|
||||
|
||||
type ChangeListener struct {
|
||||
Id string
|
||||
Listener chan interface{}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,15 +148,17 @@ func init(){
|
|||
}
|
||||
INDEXING_EXT()
|
||||
|
||||
|
||||
onChange := Config.ListenForChange()
|
||||
runner := func() {
|
||||
startSearch := false
|
||||
for {
|
||||
if SEARCH_ENABLE() == false {
|
||||
select {
|
||||
case <- Config.OnChange:
|
||||
case <- onChange.Listener: startSearch = SEARCH_ENABLE()
|
||||
}
|
||||
if startSearch == false {
|
||||
continue
|
||||
}
|
||||
continue
|
||||
}
|
||||
sidx := SProc.Peek()
|
||||
if sidx == nil {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package plugin
|
|||
|
||||
import (
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin/plg_starter_tunnel"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin/plg_starter_tor"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin/plg_image_light"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin/plg_backend_backblaze"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin/plg_backend_dav"
|
||||
|
|
|
|||
|
|
@ -47,17 +47,16 @@ func init() {
|
|||
Hooks.Register.Starter(func (r *mux.Router) {
|
||||
if enable_tor() == false {
|
||||
startTor := false
|
||||
onChange := Config.ListenForChange()
|
||||
for {
|
||||
select {
|
||||
case <- Config.OnChange:
|
||||
if enable_tor() == true {
|
||||
startTor = true
|
||||
break
|
||||
}
|
||||
case <- onChange.Listener: startTor = enable_tor()
|
||||
}
|
||||
if startTor == true { break }
|
||||
}
|
||||
Config.UnlistenForChange(onChange)
|
||||
}
|
||||
|
||||
Log.Info("[tor] starting ...")
|
||||
t, err := tor.Start(nil, &tor.StartConf{
|
||||
DataDir: TOR_PATH,
|
||||
|
|
|
|||
|
|
@ -65,19 +65,17 @@ func init() {
|
|||
return
|
||||
}
|
||||
}()
|
||||
Config.Get("features.server.tunnel_url").Set(nil)
|
||||
Config.Get("features.server.tunnel_url").Set(nil)
|
||||
if tunnel_enable() == false {
|
||||
startTunnel := false
|
||||
onChange := Config.ListenForChange()
|
||||
for {
|
||||
select {
|
||||
case <- Config.OnChange:
|
||||
if tunnel_enable() == true {
|
||||
startTunnel = true
|
||||
break
|
||||
}
|
||||
case <- onChange.Listener: startTunnel = tunnel_enable()
|
||||
}
|
||||
if startTunnel == true { break }
|
||||
if startTunnel == true { break }
|
||||
}
|
||||
Config.UnlistenForChange(onChange)
|
||||
}
|
||||
|
||||
Log.Info("[tunnel] starting ...")
|
||||
|
|
|
|||
Loading…
Reference in a new issue