️ Do not save config changes to disk immediately

This commit is contained in:
2024-12-16 21:29:51 +01:00
parent 35681102b4
commit 5122791f85
3 changed files with 64 additions and 13 deletions

40
api.go
View File

@ -123,7 +123,7 @@ func createPort(body CreatePortRequest) (gin.H, gin.H) {
registerPort(&newPort, "output") registerPort(&newPort, "output")
portConfig.Output = append(portConfig.Output, newPort) portConfig.Output = append(portConfig.Output, newPort)
} }
saveConfig("ports", &portConfig) markConfig("ports")
onPortListChange() onPortListChange()
return gin.H{"success": true, "UUID": id}, nil return gin.H{"success": true, "UUID": id}, nil
} }
@ -155,7 +155,7 @@ func deletePort(id string) gin.H {
// Unregister port // Unregister port
unregisterPort(port) unregisterPort(port)
saveConfig("ports", &portConfig) markConfig("ports")
onPortListChange() onPortListChange()
return nil 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!"} 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 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 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 port.State.Balance = *body.Balance
didChange = true
} }
saveConfig("ports", &portConfig) if didChange {
markConfig("ports")
onPortChange(port) onPortChange(port)
}
return gin.H{"success": true, "state": port.State}, nil 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 port.Name = *body.Name
} }
saveConfig("ports", &portConfig) markConfig("ports")
onPortChange(port) onPortChange(port)
return gin.H{"success": true, "port": port}, nil 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}) from.Route = append(from.Route, PortRoute{ToUUID: body.To, Mute: true, Volume: 0, Balance: 0})
saveConfig("ports", &portConfig) markConfig("ports")
onPortChange(from) onPortChange(from)
return nil return nil
} }
@ -331,18 +338,25 @@ func setRoute(fromId string, toId string, body SetRouteRequest) (gin.H, gin.H) {
continue continue
} }
if body.Mute != nil { didChange := false
if body.Mute != nil && from.Route[i].Mute != *body.Mute {
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 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 from.Route[i].Balance = *body.Balance
didChange = true
} }
saveConfig("ports", &portConfig) if didChange {
markConfig("ports")
onPortChange(from) onPortChange(from)
}
return gin.H{"success": true, "state": from.Route[i]}, nil 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:]...) from.Route = append(from.Route[:i], from.Route[i+1:]...)
saveConfig("ports", &portConfig) markConfig("ports")
onPortListChange() onPortListChange()
return nil return nil
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"sync"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
) )
@ -27,6 +28,11 @@ type Config struct {
var config Config 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 // Copy default config if `{name}.toml` file does not exist
func createDefaultConfig(name string) { func createDefaultConfig(name string) {
dest_path := fmt.Sprintf("%s/%s.toml", CONFIG_DIR, name) 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 // Save struct as a `.toml` file
func saveConfig(name string, dest interface{}) { func saveConfig(name string, dest interface{}) {
bin, err := toml.Marshal(dest) bin, err := toml.Marshal(dest)
@ -70,3 +83,12 @@ func saveConfig(name string, dest interface{}) {
fmt.Println(err) 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
View File

@ -4,6 +4,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
) )
func main() { func main() {
@ -25,6 +26,20 @@ func main() {
// Start audio routing // Start audio routing
startRouting() 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 // Wait until SIGINT received
<-end <-end