first commit
This commit is contained in:
46
internal/status/status.go
Normal file
46
internal/status/status.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Package status loads and manages the service status JSON.
|
||||
package status
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Service represents a single monitored service.
|
||||
type Service struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Status string `json:"status"` // "up", "degraded", "down", "unknown"
|
||||
Note string `json:"note,omitempty"`
|
||||
}
|
||||
|
||||
// Page is the full status page data loaded from JSON.
|
||||
type Page struct {
|
||||
LastChecked time.Time `json:"last_checked"`
|
||||
Services []Service `json:"services"`
|
||||
}
|
||||
|
||||
// Load reads and parses the status JSON from path.
|
||||
func Load(path string) (*Page, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var p Page
|
||||
if err := json.Unmarshal(raw, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Save writes the status page data back to path.
|
||||
func Save(path string, p *Page) error {
|
||||
p.LastChecked = time.Now().UTC()
|
||||
raw, err := json.MarshalIndent(p, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, raw, 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user