Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

55 行
1.4KB

  1. package control
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "sync"
  6. "github.com/jan/fm-rds-tx/internal/config"
  7. drypkg "github.com/jan/fm-rds-tx/internal/dryrun"
  8. )
  9. type Server struct {
  10. mu sync.RWMutex
  11. cfg config.Config
  12. }
  13. func NewServer(cfg config.Config) *Server { return &Server{cfg: cfg} }
  14. func (s *Server) Handler() http.Handler {
  15. mux := http.NewServeMux()
  16. mux.HandleFunc("/healthz", s.handleHealth)
  17. mux.HandleFunc("/status", s.handleStatus)
  18. mux.HandleFunc("/dry-run", s.handleDryRun)
  19. return mux
  20. }
  21. func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
  22. w.Header().Set("Content-Type", "application/json")
  23. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true})
  24. }
  25. func (s *Server) handleStatus(w http.ResponseWriter, _ *http.Request) {
  26. s.mu.RLock()
  27. cfg := s.cfg
  28. s.mu.RUnlock()
  29. w.Header().Set("Content-Type", "application/json")
  30. _ = json.NewEncoder(w).Encode(map[string]any{
  31. "service": "fm-rds-tx",
  32. "backend": cfg.Backend.Kind,
  33. "frequencyMHz": cfg.FM.FrequencyMHz,
  34. "stereoEnabled": cfg.FM.StereoEnabled,
  35. "rdsEnabled": cfg.RDS.Enabled,
  36. })
  37. }
  38. func (s *Server) handleDryRun(w http.ResponseWriter, _ *http.Request) {
  39. s.mu.RLock()
  40. cfg := s.cfg
  41. s.mu.RUnlock()
  42. w.Header().Set("Content-Type", "application/json")
  43. _ = json.NewEncoder(w).Encode(drypkg.Generate(cfg))
  44. }