⚡️ Do not save config changes to disk immediately
This commit is contained in:
40
api.go
40
api.go
@ -123,7 +123,7 @@ func createPort(body CreatePortRequest) (gin.H, gin.H) {
|
||||
registerPort(&newPort, "output")
|
||||
portConfig.Output = append(portConfig.Output, newPort)
|
||||
}
|
||||
saveConfig("ports", &portConfig)
|
||||
markConfig("ports")
|
||||
onPortListChange()
|
||||
return gin.H{"success": true, "UUID": id}, nil
|
||||
}
|
||||
@ -155,7 +155,7 @@ func deletePort(id string) gin.H {
|
||||
// Unregister port
|
||||
unregisterPort(port)
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
markConfig("ports")
|
||||
onPortListChange()
|
||||
return nil
|
||||
}
|
||||
@ -206,18 +206,25 @@ func setState(id string, body SetPortRequest) (gin.H, gin.H) {
|
||||
return nil, gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
|
||||
}
|
||||
|
||||
if body.Mute != nil {
|
||||
didChange := false
|
||||
|
||||
if body.Mute != nil && port.State.Mute != *body.Mute {
|
||||
port.State.Mute = *body.Mute
|
||||
didChange = true
|
||||
}
|
||||
if body.Volume != nil {
|
||||
if body.Volume != nil && port.State.Volume != *body.Volume {
|
||||
port.State.Volume = *body.Volume
|
||||
didChange = true
|
||||
}
|
||||
if body.Balance != nil {
|
||||
if body.Balance != nil && port.State.Balance != *body.Balance {
|
||||
port.State.Balance = *body.Balance
|
||||
didChange = true
|
||||
}
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
if didChange {
|
||||
markConfig("ports")
|
||||
onPortChange(port)
|
||||
}
|
||||
return gin.H{"success": true, "state": port.State}, nil
|
||||
}
|
||||
|
||||
@ -253,7 +260,7 @@ func editPort(id string, body EditPortRequest) (gin.H, gin.H) {
|
||||
port.Name = *body.Name
|
||||
}
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
markConfig("ports")
|
||||
onPortChange(port)
|
||||
return gin.H{"success": true, "port": port}, nil
|
||||
}
|
||||
@ -291,7 +298,7 @@ func createRoute(id string, body CreateRouteRequest) gin.H {
|
||||
}
|
||||
|
||||
from.Route = append(from.Route, PortRoute{ToUUID: body.To, Mute: true, Volume: 0, Balance: 0})
|
||||
saveConfig("ports", &portConfig)
|
||||
markConfig("ports")
|
||||
onPortChange(from)
|
||||
return nil
|
||||
}
|
||||
@ -331,18 +338,25 @@ func setRoute(fromId string, toId string, body SetRouteRequest) (gin.H, gin.H) {
|
||||
continue
|
||||
}
|
||||
|
||||
if body.Mute != nil {
|
||||
didChange := false
|
||||
|
||||
if body.Mute != nil && from.Route[i].Mute != *body.Mute {
|
||||
from.Route[i].Mute = *body.Mute
|
||||
didChange = true
|
||||
}
|
||||
if body.Volume != nil {
|
||||
if body.Volume != nil && from.Route[i].Volume != *body.Volume {
|
||||
from.Route[i].Volume = *body.Volume
|
||||
didChange = true
|
||||
}
|
||||
if body.Balance != nil {
|
||||
if body.Balance != nil && from.Route[i].Balance != *body.Balance {
|
||||
from.Route[i].Balance = *body.Balance
|
||||
didChange = true
|
||||
}
|
||||
|
||||
saveConfig("ports", &portConfig)
|
||||
if didChange {
|
||||
markConfig("ports")
|
||||
onPortChange(from)
|
||||
}
|
||||
return gin.H{"success": true, "state": from.Route[i]}, nil
|
||||
}
|
||||
|
||||
@ -372,7 +386,7 @@ func deleteRoute(fromId string, toId string) gin.H {
|
||||
}
|
||||
|
||||
from.Route = append(from.Route[:i], from.Route[i+1:]...)
|
||||
saveConfig("ports", &portConfig)
|
||||
markConfig("ports")
|
||||
onPortListChange()
|
||||
return nil
|
||||
}
|
||||
|
22
config.go
22
config.go
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
@ -27,6 +28,11 @@ type Config struct {
|
||||
|
||||
var config Config
|
||||
|
||||
var shouldSaveConfigsMutex = new(sync.Mutex)
|
||||
var shouldSaveConfigs = map[string]bool{
|
||||
"ports": false,
|
||||
}
|
||||
|
||||
// Copy default config if `{name}.toml` file does not exist
|
||||
func createDefaultConfig(name string) {
|
||||
dest_path := fmt.Sprintf("%s/%s.toml", CONFIG_DIR, name)
|
||||
@ -59,6 +65,13 @@ func loadConfig(name string, dest interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Mark a config file as changed, so it gets saved to disk later
|
||||
func markConfig(name string) {
|
||||
shouldSaveConfigsMutex.Lock()
|
||||
shouldSaveConfigs[name] = true
|
||||
shouldSaveConfigsMutex.Unlock()
|
||||
}
|
||||
|
||||
// Save struct as a `.toml` file
|
||||
func saveConfig(name string, dest interface{}) {
|
||||
bin, err := toml.Marshal(dest)
|
||||
@ -70,3 +83,12 @@ func saveConfig(name string, dest interface{}) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func saveConfigIfMarked(name string, dest interface{}) {
|
||||
shouldSaveConfigsMutex.Lock()
|
||||
if shouldSaveConfigs[name] {
|
||||
shouldSaveConfigs[name] = false
|
||||
saveConfig(name, dest)
|
||||
}
|
||||
shouldSaveConfigsMutex.Unlock()
|
||||
}
|
||||
|
15
main.go
15
main.go
@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -25,6 +26,20 @@ func main() {
|
||||
// Start audio routing
|
||||
startRouting()
|
||||
|
||||
// Periodically save config files
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
saveConfigIfMarked("ports", portConfig)
|
||||
case <-end:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait until SIGINT received
|
||||
<-end
|
||||
|
||||
|
Reference in New Issue
Block a user