-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
89 lines (81 loc) · 1.92 KB
/
config.go
File metadata and controls
89 lines (81 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"encoding/json"
"log"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
)
type Config struct {
Port int `json:"port"`
MaxDurationMinutes int `json:"max_duration_minutes"`
MinViews int `json:"min_views"`
RepeatLimit int `json:"repeat_limit"`
CleanupAfterHours int `json:"cleanup_after_hours"`
MaxQueueSize int `json:"max_queue_size"`
DonationWidgetURL string `json:"donation_widget_url"`
DonationMinAmount int `json:"donation_min_amount"`
YouTubeAPIKey string `json:"youtube_api_key"`
FallbackPlaylistURL string `json:"fallback_playlist_url"`
}
type ConfigManager struct {
mu sync.RWMutex
cfg Config
}
func loadConfig() (*ConfigManager, error) {
data, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, err
}
if cfg.MaxQueueSize == 0 {
cfg.MaxQueueSize = 100
}
return &ConfigManager{cfg: cfg}, nil
}
func (m *ConfigManager) get() Config {
m.mu.RLock()
defer m.mu.RUnlock()
return m.cfg
}
func (m *ConfigManager) watch() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("Error creating watcher:", err)
}
defer watcher.Close()
if err := watcher.Add(filepath.Dir("config.json")); err != nil {
log.Fatal("Error watching config directory:", err)
}
for {
select {
case event := <-watcher.Events:
if filepath.Base(event.Name) == "config.json" && event.Has(fsnotify.Write) {
m.reload()
}
case err := <-watcher.Errors:
log.Println("Watcher error:", err)
}
}
}
func (m *ConfigManager) reload() {
data, err := os.ReadFile("config.json")
if err != nil {
return
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return
}
if cfg.MaxQueueSize == 0 {
cfg.MaxQueueSize = 100
}
m.mu.Lock()
m.cfg = cfg
m.mu.Unlock()
log.Println("Config reloaded")
}