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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
package config
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
)
// Config represents the root configuration structure
type Config struct {
Site SiteConfig `yaml:"site"`
Server ServerConfig `yaml:"server"`
Storage StorageConfig `yaml:"storage"`
Display DisplayConfig `yaml:"display"`
Groups []GroupConfig `yaml:"groups"`
Incidents []IncidentConfig `yaml:"incidents"`
}
// DisplayConfig contains display/UI settings
type DisplayConfig struct {
// TickMode controls how history is aggregated: ping, minute, hour, day
TickMode string `yaml:"tick_mode"`
// TickCount is the number of ticks/bars to display in the history
TickCount int `yaml:"tick_count"`
// PingFixedSlots: when true and tick_mode=ping, shows fixed slots with empty bars
// when false, bars grow dynamically as pings come in
PingFixedSlots bool `yaml:"ping_fixed_slots"`
// Timezone for display (e.g., "UTC", "America/New_York", "Local")
Timezone string `yaml:"timezone"`
// ShowThemeToggle controls whether to show the theme toggle button (defaults to true)
ShowThemeToggle *bool `yaml:"show_theme_toggle"`
}
// SiteConfig contains site metadata
type SiteConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Logo string `yaml:"logo"`
Favicon string `yaml:"favicon"`
}
// ServerConfig contains HTTP server settings
type ServerConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
// StorageConfig contains database settings
type StorageConfig struct {
Path string `yaml:"path"`
HistoryDays int `yaml:"history_days"`
}
// GroupConfig represents a group of monitors
type GroupConfig struct {
Name string `yaml:"name"`
Monitors []MonitorConfig `yaml:"monitors"`
DefaultCollapsed *bool `yaml:"default_collapsed"` // nil = false (expanded by default)
ShowGroupUptime *bool `yaml:"show_group_uptime"` // nil = true (show by default)
}
// MonitorConfig represents a single monitor
type MonitorConfig struct {
Name string `yaml:"name"`
Type string `yaml:"type"` // http, https, tcp
Target string `yaml:"target"`
Interval Duration `yaml:"interval"`
Timeout Duration `yaml:"timeout"`
ExpectedStatus int `yaml:"expected_status,omitempty"`
VerifySSL *bool `yaml:"verify_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Body string `yaml:"body,omitempty"`
}
// IncidentConfig represents an incident or maintenance
type IncidentConfig struct {
Title string `yaml:"title"`
Status string `yaml:"status"` // scheduled, investigating, identified, monitoring, resolved
Message string `yaml:"message"`
ScheduledStart *time.Time `yaml:"scheduled_start,omitempty"`
ScheduledEnd *time.Time `yaml:"scheduled_end,omitempty"`
CreatedAt *time.Time `yaml:"created_at,omitempty"`
ResolvedAt *time.Time `yaml:"resolved_at,omitempty"`
AffectedMonitors []string `yaml:"affected_monitors,omitempty"`
Updates []IncidentUpdate `yaml:"updates,omitempty"`
}
// IncidentUpdate represents an update to an incident
type IncidentUpdate struct {
Time time.Time `yaml:"time"`
Status string `yaml:"status"`
Message string `yaml:"message"`
}
// Duration is a wrapper around time.Duration for YAML parsing
type Duration struct {
time.Duration
}
// UnmarshalYAML implements yaml.Unmarshaler for Duration
func (d *Duration) UnmarshalYAML(value *yaml.Node) error {
var s string
if err := value.Decode(&s); err != nil {
return err
}
duration, err := time.ParseDuration(s)
if err != nil {
return fmt.Errorf("invalid duration %q: %w", s, err)
}
d.Duration = duration
return nil
}
// MarshalYAML implements yaml.Marshaler for Duration
func (d Duration) MarshalYAML() (interface{}, error) {
return d.Duration.String(), nil
}
// Load reads and parses a configuration file
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
// Apply defaults
cfg.applyDefaults()
// Validate configuration
if err := cfg.validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
return &cfg, nil
}
// applyDefaults sets default values for missing configuration
func (c *Config) applyDefaults() {
if c.Site.Name == "" {
c.Site.Name = "Kaze Status"
}
if c.Site.Description == "" {
c.Site.Description = "Service Status Page"
}
if c.Server.Host == "" {
c.Server.Host = "0.0.0.0"
}
if c.Server.Port == 0 {
c.Server.Port = 8080
}
if c.Storage.Path == "" {
c.Storage.Path = "./kaze.db"
}
if c.Storage.HistoryDays == 0 {
c.Storage.HistoryDays = 90
}
// Apply display defaults
if c.Display.TickMode == "" {
c.Display.TickMode = "hour"
}
if c.Display.TickCount == 0 {
c.Display.TickCount = 45
}
if c.Display.Timezone == "" {
c.Display.Timezone = "Local"
}
if c.Display.ShowThemeToggle == nil {
defaultShow := true
c.Display.ShowThemeToggle = &defaultShow
}
// Apply group defaults
for i := range c.Groups {
grp := &c.Groups[i]
if grp.DefaultCollapsed == nil {
defaultCollapsed := false
grp.DefaultCollapsed = &defaultCollapsed
}
if grp.ShowGroupUptime == nil {
defaultShow := true
grp.ShowGroupUptime = &defaultShow
}
for j := range c.Groups[i].Monitors {
m := &c.Groups[i].Monitors[j]
if m.Interval.Duration == 0 {
m.Interval.Duration = 30 * time.Second
}
if m.Timeout.Duration == 0 {
m.Timeout.Duration = 10 * time.Second
}
if m.Type == "http" || m.Type == "https" {
if m.ExpectedStatus == 0 {
m.ExpectedStatus = 200
}
if m.Method == "" {
m.Method = "GET"
}
if m.VerifySSL == nil {
defaultVerify := true
m.VerifySSL = &defaultVerify
}
}
}
}
}
// validate checks the configuration for errors
func (c *Config) validate() error {
if len(c.Groups) == 0 {
return fmt.Errorf("at least one group with monitors is required")
}
monitorNames := make(map[string]bool)
for _, group := range c.Groups {
if group.Name == "" {
return fmt.Errorf("group name cannot be empty")
}
if len(group.Monitors) == 0 {
return fmt.Errorf("group %q must have at least one monitor", group.Name)
}
for _, monitor := range group.Monitors {
if monitor.Name == "" {
return fmt.Errorf("monitor name cannot be empty in group %q", group.Name)
}
if monitorNames[monitor.Name] {
return fmt.Errorf("duplicate monitor name: %q", monitor.Name)
}
monitorNames[monitor.Name] = true
if monitor.Target == "" {
return fmt.Errorf("monitor %q must have a target", monitor.Name)
}
switch monitor.Type {
case "http", "https", "tcp":
// Valid types
default:
return fmt.Errorf("monitor %q has invalid type %q (must be http, https, or tcp)", monitor.Name, monitor.Type)
}
}
}
// Validate incidents
for _, incident := range c.Incidents {
if incident.Title == "" {
return fmt.Errorf("incident title cannot be empty")
}
switch incident.Status {
case "scheduled", "investigating", "identified", "monitoring", "resolved":
// Valid statuses
default:
return fmt.Errorf("incident %q has invalid status %q", incident.Title, incident.Status)
}
}
// Validate display config
switch c.Display.TickMode {
case "ping", "minute", "hour", "day":
// Valid modes
default:
return fmt.Errorf("invalid tick_mode %q (must be ping, minute, hour, or day)", c.Display.TickMode)
}
if c.Display.TickCount < 1 || c.Display.TickCount > 200 {
return fmt.Errorf("tick_count must be between 1 and 200, got %d", c.Display.TickCount)
}
return nil
}
// GetAllMonitors returns all monitors from all groups with their group names
func (c *Config) GetAllMonitors() []MonitorWithGroup {
var monitors []MonitorWithGroup
for _, group := range c.Groups {
for _, monitor := range group.Monitors {
monitors = append(monitors, MonitorWithGroup{
GroupName: group.Name,
Monitor: monitor,
})
}
}
return monitors
}
// MonitorWithGroup pairs a monitor with its group name
type MonitorWithGroup struct {
GroupName string
Monitor MonitorConfig
}
|