Skip to content

Commit 6946114

Browse files
authored
chore(config): merge userConfig with defaultConfig and add a lock (#164)
* chore(config): merge userConfig with defaultConfig and add a lock * chore(config): remove lock for LoadConfig
1 parent cd333c4 commit 6946114

File tree

5 files changed

+11
-34
lines changed

5 files changed

+11
-34
lines changed

config.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"os"
7+
"sync"
78
)
89

910
type WakeOnLanDevice struct {
@@ -40,32 +41,38 @@ var defaultConfig = &Config{
4041
DisplayOffAfterSec: 1800, // 30 minutes
4142
}
4243

43-
var config *Config
44+
var (
45+
config *Config
46+
configLock = &sync.Mutex{}
47+
)
4448

4549
func LoadConfig() {
4650
if config != nil {
51+
logger.Info("config already loaded, skipping")
4752
return
4853
}
4954

5055
file, err := os.Open(configPath)
5156
if err != nil {
5257
logger.Debug("default config file doesn't exist, using default")
53-
config = defaultConfig
5458
return
5559
}
5660
defer file.Close()
5761

58-
var loadedConfig Config
62+
// load and merge the default config with the user config
63+
loadedConfig := *defaultConfig
5964
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
6065
logger.Errorf("config file JSON parsing failed, %v", err)
61-
config = defaultConfig
6266
return
6367
}
6468

6569
config = &loadedConfig
6670
}
6771

6872
func SaveConfig() error {
73+
configLock.Lock()
74+
defer configLock.Unlock()
75+
6976
file, err := os.Create(configPath)
7077
if err != nil {
7178
return fmt.Errorf("failed to create config file: %w", err)

display.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ func watchTsEvents() {
209209
// if they're not already set. This is done separately to the init routine as the "never dim"
210210
// option has the value set to zero, but time.NewTicker only accept positive values.
211211
func startBacklightTickers() {
212-
LoadConfig()
213212
// Don't start the tickers if the display is switched off.
214213
// Set the display to off if that's the case.
215214
if config.DisplayMaxBrightness == 0 {

jsonrpc.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func rpcSetEDID(edid string) error {
194194
}
195195

196196
// Save EDID to config, allowing it to be restored on reboot.
197-
LoadConfig()
198197
config.EdidString = edid
199198
SaveConfig()
200199

@@ -235,8 +234,6 @@ func rpcTryUpdate() error {
235234
}
236235

237236
func rpcSetBacklightSettings(params BacklightSettings) error {
238-
LoadConfig()
239-
240237
blConfig := params
241238

242239
// NOTE: by default, the frontend limits the brightness to 64, as that's what the device originally shipped with.
@@ -275,8 +272,6 @@ func rpcSetBacklightSettings(params BacklightSettings) error {
275272
}
276273

277274
func rpcGetBacklightSettings() (*BacklightSettings, error) {
278-
LoadConfig()
279-
280275
return &BacklightSettings{
281276
MaxBrightness: config.DisplayMaxBrightness,
282277
DimAfter: int(config.DisplayDimAfterSec),
@@ -544,7 +539,6 @@ func rpcSetUsbEmulationState(enabled bool) error {
544539
}
545540

546541
func rpcGetWakeOnLanDevices() ([]WakeOnLanDevice, error) {
547-
LoadConfig()
548542
if config.WakeOnLanDevices == nil {
549543
return []WakeOnLanDevice{}, nil
550544
}
@@ -556,13 +550,11 @@ type SetWakeOnLanDevicesParams struct {
556550
}
557551

558552
func rpcSetWakeOnLanDevices(params SetWakeOnLanDevicesParams) error {
559-
LoadConfig()
560553
config.WakeOnLanDevices = params.Devices
561554
return SaveConfig()
562555
}
563556

564557
func rpcResetConfig() error {
565-
LoadConfig()
566558
config = defaultConfig
567559
if err := SaveConfig(); err != nil {
568560
return fmt.Errorf("failed to reset config: %w", err)

native.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ func ensureBinaryUpdated(destPath string) error {
311311
// Restore the HDMI EDID value from the config.
312312
// Called after successful connection to jetkvm_native.
313313
func restoreHdmiEdid() {
314-
LoadConfig()
315314
if config.EdidString != "" {
316315
logger.Infof("Restoring HDMI EDID to %v", config.EdidString)
317316
_, err := CallCtrlAction("set_edid", map[string]interface{}{"edid": config.EdidString})

web.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ func handleWebRTCSession(c *gin.Context) {
147147
}
148148

149149
func handleLogin(c *gin.Context) {
150-
LoadConfig()
151-
152150
if config.LocalAuthMode == "noPassword" {
153151
c.JSON(http.StatusBadRequest, gin.H{"error": "Login is disabled in noPassword mode"})
154152
return
@@ -161,7 +159,6 @@ func handleLogin(c *gin.Context) {
161159
return
162160
}
163161

164-
LoadConfig()
165162
err := bcrypt.CompareHashAndPassword([]byte(config.HashedPassword), []byte(req.Password))
166163
if err != nil {
167164
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid password"})
@@ -177,7 +174,6 @@ func handleLogin(c *gin.Context) {
177174
}
178175

179176
func handleLogout(c *gin.Context) {
180-
LoadConfig()
181177
config.LocalAuthToken = ""
182178
if err := SaveConfig(); err != nil {
183179
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save configuration"})
@@ -191,8 +187,6 @@ func handleLogout(c *gin.Context) {
191187

192188
func protectedMiddleware() gin.HandlerFunc {
193189
return func(c *gin.Context) {
194-
LoadConfig()
195-
196190
if config.LocalAuthMode == "noPassword" {
197191
c.Next()
198192
return
@@ -221,8 +215,6 @@ func RunWebServer() {
221215
}
222216

223217
func handleDevice(c *gin.Context) {
224-
LoadConfig()
225-
226218
response := LocalDevice{
227219
AuthMode: &config.LocalAuthMode,
228220
DeviceID: GetDeviceID(),
@@ -232,8 +224,6 @@ func handleDevice(c *gin.Context) {
232224
}
233225

234226
func handleCreatePassword(c *gin.Context) {
235-
LoadConfig()
236-
237227
if config.HashedPassword != "" {
238228
c.JSON(http.StatusBadRequest, gin.H{"error": "Password already set"})
239229
return
@@ -274,8 +264,6 @@ func handleCreatePassword(c *gin.Context) {
274264
}
275265

276266
func handleUpdatePassword(c *gin.Context) {
277-
LoadConfig()
278-
279267
if config.HashedPassword == "" {
280268
c.JSON(http.StatusBadRequest, gin.H{"error": "Password is not set"})
281269
return
@@ -319,8 +307,6 @@ func handleUpdatePassword(c *gin.Context) {
319307
}
320308

321309
func handleDeletePassword(c *gin.Context) {
322-
LoadConfig()
323-
324310
if config.HashedPassword == "" {
325311
c.JSON(http.StatusBadRequest, gin.H{"error": "Password is not set"})
326312
return
@@ -357,8 +343,6 @@ func handleDeletePassword(c *gin.Context) {
357343
}
358344

359345
func handleDeviceStatus(c *gin.Context) {
360-
LoadConfig()
361-
362346
response := DeviceStatus{
363347
IsSetup: config.LocalAuthMode != "",
364348
}
@@ -367,8 +351,6 @@ func handleDeviceStatus(c *gin.Context) {
367351
}
368352

369353
func handleDeviceUIConfig(c *gin.Context) {
370-
LoadConfig()
371-
372354
config, _ := json.Marshal(gin.H{
373355
"CLOUD_API": config.CloudURL,
374356
"DEVICE_VERSION": builtAppVersion,
@@ -384,8 +366,6 @@ func handleDeviceUIConfig(c *gin.Context) {
384366
}
385367

386368
func handleSetup(c *gin.Context) {
387-
LoadConfig()
388-
389369
// Check if the device is already set up
390370
if config.LocalAuthMode != "" || config.HashedPassword != "" {
391371
c.JSON(http.StatusBadRequest, gin.H{"error": "Device is already set up"})

0 commit comments

Comments
 (0)