mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-10 01:00:05 +00:00
[editor] Notify frontend to reload data after token change
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
e672737172
commit
014f90c94c
5 changed files with 52 additions and 31 deletions
4
auth.go
4
auth.go
|
@ -98,6 +98,8 @@ func handleAuthUpdateBotToken(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Error(w, fmt.Sprintf("Authorization as %q complete, you can now close this window.", botUser), http.StatusOK)
|
http.Error(w, fmt.Sprintf("Authorization as %q complete, you can now close this window.", botUser), http.StatusOK)
|
||||||
|
|
||||||
|
frontendReloadHooks.Ping() // Tell frontend to update its config
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAuthUpdateChannelGrant(w http.ResponseWriter, r *http.Request) {
|
func handleAuthUpdateChannelGrant(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -147,4 +149,6 @@ func handleAuthUpdateChannelGrant(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Error(w, fmt.Sprintf("Scopes for %q updated, you can now close this window.", grantUser), http.StatusOK)
|
http.Error(w, fmt.Sprintf("Scopes for %q updated, you can now close this window.", grantUser), http.StatusOK)
|
||||||
|
|
||||||
|
frontendReloadHooks.Ping() // Tell frontend to update its config
|
||||||
}
|
}
|
||||||
|
|
25
config.go
25
config.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
"github.com/go-irc/irc"
|
||||||
|
@ -27,27 +26,9 @@ var (
|
||||||
|
|
||||||
hashstructUUIDNamespace = uuid.Must(uuid.FromString("3a0ccc46-d3ba-46b5-ac07-27528c933174"))
|
hashstructUUIDNamespace = uuid.Must(uuid.FromString("3a0ccc46-d3ba-46b5-ac07-27528c933174"))
|
||||||
|
|
||||||
configReloadHooks = map[string]func(){}
|
|
||||||
configReloadHooksLock sync.RWMutex
|
|
||||||
|
|
||||||
errSaveNotRequired = errors.New("save not required")
|
errSaveNotRequired = errors.New("save not required")
|
||||||
)
|
)
|
||||||
|
|
||||||
func registerConfigReloadHook(hook func()) func() {
|
|
||||||
configReloadHooksLock.Lock()
|
|
||||||
defer configReloadHooksLock.Unlock()
|
|
||||||
|
|
||||||
id := uuid.Must(uuid.NewV4()).String()
|
|
||||||
configReloadHooks[id] = hook
|
|
||||||
|
|
||||||
return func() {
|
|
||||||
configReloadHooksLock.Lock()
|
|
||||||
defer configReloadHooksLock.Unlock()
|
|
||||||
|
|
||||||
delete(configReloadHooks, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
configAuthToken struct {
|
configAuthToken struct {
|
||||||
Hash string `json:"-" yaml:"hash"`
|
Hash string `json:"-" yaml:"hash"`
|
||||||
|
@ -148,11 +129,7 @@ func loadConfig(filename string) error {
|
||||||
}).Info("Config file (re)loaded")
|
}).Info("Config file (re)loaded")
|
||||||
|
|
||||||
// Notify listener config has changed
|
// Notify listener config has changed
|
||||||
configReloadHooksLock.RLock()
|
frontendReloadHooks.Ping()
|
||||||
defer configReloadHooksLock.RUnlock()
|
|
||||||
for _, fn := range configReloadHooks {
|
|
||||||
fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"github.com/Luzifer/twitch-bot/v3/plugins"
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var frontendReloadHooks = newHooker()
|
||||||
|
|
||||||
func registerEditorGlobalMethods() {
|
func registerEditorGlobalMethods() {
|
||||||
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
|
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
|
||||||
{
|
{
|
||||||
|
@ -143,7 +145,7 @@ func configEditorGlobalSubscribe(w http.ResponseWriter, r *http.Request) {
|
||||||
var (
|
var (
|
||||||
configReloadNotify = make(chan struct{}, 1)
|
configReloadNotify = make(chan struct{}, 1)
|
||||||
pingTimer = time.NewTicker(websocketPingInterval)
|
pingTimer = time.NewTicker(websocketPingInterval)
|
||||||
unsubscribe = registerConfigReloadHook(func() { configReloadNotify <- struct{}{} })
|
unsubscribe = frontendReloadHooks.Register(func() { configReloadNotify <- struct{}{} })
|
||||||
)
|
)
|
||||||
defer unsubscribe()
|
defer unsubscribe()
|
||||||
|
|
||||||
|
|
42
hooker.go
Normal file
42
hooker.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
hooker struct {
|
||||||
|
hooks map[string]func()
|
||||||
|
lock sync.RWMutex
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func newHooker() *hooker { return &hooker{hooks: map[string]func(){}} }
|
||||||
|
|
||||||
|
func (h *hooker) Ping() {
|
||||||
|
h.lock.RLock()
|
||||||
|
defer h.lock.RUnlock()
|
||||||
|
|
||||||
|
for _, hf := range h.hooks {
|
||||||
|
hf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hooker) Register(hook func()) func() {
|
||||||
|
h.lock.Lock()
|
||||||
|
defer h.lock.Unlock()
|
||||||
|
|
||||||
|
id := uuid.Must(uuid.NewV4()).String()
|
||||||
|
h.hooks[id] = hook
|
||||||
|
|
||||||
|
return func() { h.unregister(id) }
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hooker) unregister(id string) {
|
||||||
|
h.lock.Lock()
|
||||||
|
defer h.lock.Unlock()
|
||||||
|
|
||||||
|
delete(h.hooks, id)
|
||||||
|
}
|
8
main.go
8
main.go
|
@ -255,12 +255,8 @@ func main() {
|
||||||
TwitchClientSecret: cfg.TwitchClientSecret,
|
TwitchClientSecret: cfg.TwitchClientSecret,
|
||||||
FallbackToken: cfg.TwitchToken,
|
FallbackToken: cfg.TwitchToken,
|
||||||
TokenUpdateHook: func() {
|
TokenUpdateHook: func() {
|
||||||
// Misuse the config reload hook to let the frontend reload its state
|
// make frontend reload its state as of token change
|
||||||
configReloadHooksLock.RLock()
|
frontendReloadHooks.Ping()
|
||||||
defer configReloadHooksLock.RUnlock()
|
|
||||||
for _, fn := range configReloadHooks {
|
|
||||||
fn()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.WithError(err).Fatal("Unable to initialize Twitch client")
|
log.WithError(err).Fatal("Unable to initialize Twitch client")
|
||||||
|
|
Loading…
Reference in a new issue