|
- package control
-
- import (
- "encoding/json"
- "net/http"
- "sync"
-
- "github.com/jan/fm-rds-tx/internal/config"
- drypkg "github.com/jan/fm-rds-tx/internal/dryrun"
- )
-
- type Server struct {
- mu sync.RWMutex
- cfg config.Config
- }
-
- func NewServer(cfg config.Config) *Server { return &Server{cfg: cfg} }
-
- func (s *Server) Handler() http.Handler {
- mux := http.NewServeMux()
- mux.HandleFunc("/healthz", s.handleHealth)
- mux.HandleFunc("/status", s.handleStatus)
- mux.HandleFunc("/dry-run", s.handleDryRun)
- return mux
- }
-
- func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]any{"ok": true})
- }
-
- func (s *Server) handleStatus(w http.ResponseWriter, _ *http.Request) {
- s.mu.RLock()
- cfg := s.cfg
- s.mu.RUnlock()
-
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]any{
- "service": "fm-rds-tx",
- "backend": cfg.Backend.Kind,
- "frequencyMHz": cfg.FM.FrequencyMHz,
- "stereoEnabled": cfg.FM.StereoEnabled,
- "rdsEnabled": cfg.RDS.Enabled,
- })
- }
-
- func (s *Server) handleDryRun(w http.ResponseWriter, _ *http.Request) {
- s.mu.RLock()
- cfg := s.cfg
- s.mu.RUnlock()
-
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(drypkg.Generate(cfg))
- }
|