Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

707 lines
20KB

  1. package control
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "mime"
  8. "net/http"
  9. "strings"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "github.com/jan/fm-rds-tx/internal/audio"
  14. "github.com/jan/fm-rds-tx/internal/config"
  15. drypkg "github.com/jan/fm-rds-tx/internal/dryrun"
  16. "github.com/jan/fm-rds-tx/internal/ingest"
  17. "github.com/jan/fm-rds-tx/internal/platform"
  18. )
  19. //go:embed ui.html
  20. var uiHTML []byte
  21. // TXController is an optional interface the Server uses to start/stop TX
  22. // and apply live config changes.
  23. type TXController interface {
  24. StartTX() error
  25. StopTX() error
  26. TXStats() map[string]any
  27. UpdateConfig(patch LivePatch) error
  28. ResetFault() error
  29. }
  30. // LivePatch mirrors the patchable fields from ConfigPatch for the engine.
  31. // nil = no change.
  32. type LivePatch struct {
  33. FrequencyMHz *float64
  34. OutputDrive *float64
  35. StereoEnabled *bool
  36. PilotLevel *float64
  37. RDSInjection *float64
  38. RDSEnabled *bool
  39. LimiterEnabled *bool
  40. LimiterCeiling *float64
  41. PS *string
  42. RadioText *string
  43. ToneLeftHz *float64
  44. ToneRightHz *float64
  45. ToneAmplitude *float64
  46. AudioGain *float64
  47. }
  48. type Server struct {
  49. mu sync.RWMutex
  50. cfg config.Config
  51. tx TXController
  52. drv platform.SoapyDriver // optional, for runtime stats
  53. streamSrc *audio.StreamSource // optional, for live audio ring stats
  54. audioIngress AudioIngress // optional, for /audio/stream
  55. ingestRt IngestRuntime // optional, for /runtime ingest stats
  56. saveConfig func(config.Config) error
  57. hardReload func()
  58. // BUG-F fix: reloadPending prevents multiple concurrent goroutines from
  59. // calling hardReload when handleIngestSave is hit multiple times quickly.
  60. reloadPending atomic.Bool
  61. audit auditCounters
  62. }
  63. type AudioIngress interface {
  64. WritePCM16(data []byte) (int, error)
  65. }
  66. type IngestRuntime interface {
  67. Stats() ingest.Stats
  68. }
  69. type auditEvent string
  70. const (
  71. auditMethodNotAllowed auditEvent = "methodNotAllowed"
  72. auditUnsupportedMediaType auditEvent = "unsupportedMediaType"
  73. auditBodyTooLarge auditEvent = "bodyTooLarge"
  74. auditUnexpectedBody auditEvent = "unexpectedBody"
  75. )
  76. type auditCounters struct {
  77. methodNotAllowed uint64
  78. unsupportedMediaType uint64
  79. bodyTooLarge uint64
  80. unexpectedBody uint64
  81. }
  82. const (
  83. maxConfigBodyBytes = 64 << 10 // 64 KiB
  84. configContentTypeHeader = "application/json"
  85. noBodyErrMsg = "request must not include a body"
  86. audioStreamContentTypeError = "Content-Type must be application/octet-stream or audio/L16"
  87. audioStreamBodyLimitDefault = 512 << 20 // 512 MiB
  88. )
  89. var audioStreamAllowedMediaTypes = []string{
  90. "application/octet-stream",
  91. "audio/l16",
  92. }
  93. var audioStreamBodyLimit = int64(audioStreamBodyLimitDefault) // bytes allowed per /audio/stream request; tests may override.
  94. func isJSONContentType(r *http.Request) bool {
  95. ct := strings.TrimSpace(r.Header.Get("Content-Type"))
  96. if ct == "" {
  97. return false
  98. }
  99. mediaType, _, err := mime.ParseMediaType(ct)
  100. if err != nil {
  101. return false
  102. }
  103. return strings.EqualFold(mediaType, configContentTypeHeader)
  104. }
  105. type ConfigPatch struct {
  106. FrequencyMHz *float64 `json:"frequencyMHz,omitempty"`
  107. OutputDrive *float64 `json:"outputDrive,omitempty"`
  108. StereoEnabled *bool `json:"stereoEnabled,omitempty"`
  109. PilotLevel *float64 `json:"pilotLevel,omitempty"`
  110. RDSInjection *float64 `json:"rdsInjection,omitempty"`
  111. RDSEnabled *bool `json:"rdsEnabled,omitempty"`
  112. ToneLeftHz *float64 `json:"toneLeftHz,omitempty"`
  113. ToneRightHz *float64 `json:"toneRightHz,omitempty"`
  114. ToneAmplitude *float64 `json:"toneAmplitude,omitempty"`
  115. PS *string `json:"ps,omitempty"`
  116. RadioText *string `json:"radioText,omitempty"`
  117. PreEmphasisTauUS *float64 `json:"preEmphasisTauUS,omitempty"`
  118. LimiterEnabled *bool `json:"limiterEnabled,omitempty"`
  119. LimiterCeiling *float64 `json:"limiterCeiling,omitempty"`
  120. AudioGain *float64 `json:"audioGain,omitempty"`
  121. PI *string `json:"pi,omitempty"`
  122. PTY *int `json:"pty,omitempty"`
  123. BS412Enabled *bool `json:"bs412Enabled,omitempty"`
  124. BS412ThresholdDBr *float64 `json:"bs412ThresholdDBr,omitempty"`
  125. MpxGain *float64 `json:"mpxGain,omitempty"`
  126. }
  127. type IngestSaveRequest struct {
  128. Ingest config.IngestConfig `json:"ingest"`
  129. }
  130. func NewServer(cfg config.Config) *Server {
  131. return &Server{cfg: cfg}
  132. }
  133. func hasRequestBody(r *http.Request) bool {
  134. if r.ContentLength > 0 {
  135. return true
  136. }
  137. for _, te := range r.TransferEncoding {
  138. if strings.EqualFold(te, "chunked") {
  139. return true
  140. }
  141. }
  142. return false
  143. }
  144. func (s *Server) rejectBody(w http.ResponseWriter, r *http.Request) bool {
  145. // Returns true when the request has an unexpected body and the error response
  146. // has already been written — callers should return immediately in that case.
  147. // Returns false when there is no body (happy path — request should proceed).
  148. if !hasRequestBody(r) {
  149. return false
  150. }
  151. s.recordAudit(auditUnexpectedBody)
  152. http.Error(w, noBodyErrMsg, http.StatusBadRequest)
  153. return true
  154. }
  155. func (s *Server) recordAudit(evt auditEvent) {
  156. switch evt {
  157. case auditMethodNotAllowed:
  158. atomic.AddUint64(&s.audit.methodNotAllowed, 1)
  159. case auditUnsupportedMediaType:
  160. atomic.AddUint64(&s.audit.unsupportedMediaType, 1)
  161. case auditBodyTooLarge:
  162. atomic.AddUint64(&s.audit.bodyTooLarge, 1)
  163. case auditUnexpectedBody:
  164. atomic.AddUint64(&s.audit.unexpectedBody, 1)
  165. }
  166. }
  167. func (s *Server) auditSnapshot() map[string]uint64 {
  168. return map[string]uint64{
  169. "methodNotAllowed": atomic.LoadUint64(&s.audit.methodNotAllowed),
  170. "unsupportedMediaType": atomic.LoadUint64(&s.audit.unsupportedMediaType),
  171. "bodyTooLarge": atomic.LoadUint64(&s.audit.bodyTooLarge),
  172. "unexpectedBody": atomic.LoadUint64(&s.audit.unexpectedBody),
  173. }
  174. }
  175. func isAudioStreamContentType(r *http.Request) bool {
  176. ct := strings.TrimSpace(r.Header.Get("Content-Type"))
  177. if ct == "" {
  178. return false
  179. }
  180. mediaType, _, err := mime.ParseMediaType(ct)
  181. if err != nil {
  182. return false
  183. }
  184. for _, allowed := range audioStreamAllowedMediaTypes {
  185. if strings.EqualFold(mediaType, allowed) {
  186. return true
  187. }
  188. }
  189. return false
  190. }
  191. func (s *Server) SetTXController(tx TXController) {
  192. s.mu.Lock()
  193. s.tx = tx
  194. s.mu.Unlock()
  195. }
  196. func (s *Server) SetDriver(drv platform.SoapyDriver) {
  197. s.mu.Lock()
  198. s.drv = drv
  199. s.mu.Unlock()
  200. }
  201. func (s *Server) SetStreamSource(src *audio.StreamSource) {
  202. s.mu.Lock()
  203. s.streamSrc = src
  204. s.mu.Unlock()
  205. }
  206. func (s *Server) SetAudioIngress(ingress AudioIngress) {
  207. s.mu.Lock()
  208. s.audioIngress = ingress
  209. s.mu.Unlock()
  210. }
  211. func (s *Server) SetIngestRuntime(rt IngestRuntime) {
  212. s.mu.Lock()
  213. s.ingestRt = rt
  214. s.mu.Unlock()
  215. }
  216. func (s *Server) SetConfigSaver(save func(config.Config) error) {
  217. s.mu.Lock()
  218. s.saveConfig = save
  219. s.mu.Unlock()
  220. }
  221. func (s *Server) SetHardReload(fn func()) {
  222. s.mu.Lock()
  223. s.hardReload = fn
  224. s.mu.Unlock()
  225. }
  226. func (s *Server) Handler() http.Handler {
  227. mux := http.NewServeMux()
  228. mux.HandleFunc("/", s.handleUI)
  229. mux.HandleFunc("/healthz", s.handleHealth)
  230. mux.HandleFunc("/status", s.handleStatus)
  231. mux.HandleFunc("/dry-run", s.handleDryRun)
  232. mux.HandleFunc("/config", s.handleConfig)
  233. mux.HandleFunc("/config/ingest/save", s.handleIngestSave)
  234. mux.HandleFunc("/runtime", s.handleRuntime)
  235. mux.HandleFunc("/runtime/fault/reset", s.handleRuntimeFaultReset)
  236. mux.HandleFunc("/tx/start", s.handleTXStart)
  237. mux.HandleFunc("/tx/stop", s.handleTXStop)
  238. mux.HandleFunc("/audio/stream", s.handleAudioStream)
  239. return mux
  240. }
  241. func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
  242. w.Header().Set("Content-Type", "application/json")
  243. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true})
  244. }
  245. func (s *Server) handleUI(w http.ResponseWriter, r *http.Request) {
  246. if r.URL.Path != "/" {
  247. http.NotFound(w, r)
  248. return
  249. }
  250. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  251. w.Header().Set("Cache-Control", "no-cache")
  252. w.Write(uiHTML)
  253. }
  254. func (s *Server) handleStatus(w http.ResponseWriter, _ *http.Request) {
  255. s.mu.RLock()
  256. cfg := s.cfg
  257. tx := s.tx
  258. s.mu.RUnlock()
  259. status := map[string]any{
  260. "service": "fm-rds-tx",
  261. "backend": cfg.Backend.Kind,
  262. "frequencyMHz": cfg.FM.FrequencyMHz,
  263. "stereoEnabled": cfg.FM.StereoEnabled,
  264. "rdsEnabled": cfg.RDS.Enabled,
  265. "preEmphasisTauUS": cfg.FM.PreEmphasisTauUS,
  266. "limiterEnabled": cfg.FM.LimiterEnabled,
  267. "fmModulationEnabled": cfg.FM.FMModulationEnabled,
  268. }
  269. if tx != nil {
  270. if stats := tx.TXStats(); stats != nil {
  271. if ri, ok := stats["runtimeIndicator"]; ok {
  272. status["runtimeIndicator"] = ri
  273. }
  274. if alert, ok := stats["runtimeAlert"]; ok {
  275. status["runtimeAlert"] = alert
  276. }
  277. if queue, ok := stats["queue"]; ok {
  278. status["queue"] = queue
  279. }
  280. if runtimeState, ok := stats["state"]; ok {
  281. status["runtimeState"] = runtimeState
  282. }
  283. }
  284. }
  285. w.Header().Set("Content-Type", "application/json")
  286. _ = json.NewEncoder(w).Encode(status)
  287. }
  288. func (s *Server) handleRuntime(w http.ResponseWriter, _ *http.Request) {
  289. s.mu.RLock()
  290. drv := s.drv
  291. tx := s.tx
  292. stream := s.streamSrc
  293. ingestRt := s.ingestRt
  294. s.mu.RUnlock()
  295. result := map[string]any{}
  296. if drv != nil {
  297. result["driver"] = drv.Stats()
  298. }
  299. if tx != nil {
  300. if stats := tx.TXStats(); stats != nil {
  301. result["engine"] = stats
  302. }
  303. }
  304. if stream != nil {
  305. result["audioStream"] = stream.Stats()
  306. }
  307. if ingestRt != nil {
  308. result["ingest"] = ingestRt.Stats()
  309. }
  310. result["controlAudit"] = s.auditSnapshot()
  311. w.Header().Set("Content-Type", "application/json")
  312. _ = json.NewEncoder(w).Encode(result)
  313. }
  314. func (s *Server) handleRuntimeFaultReset(w http.ResponseWriter, r *http.Request) {
  315. if r.Method != http.MethodPost {
  316. s.recordAudit(auditMethodNotAllowed)
  317. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  318. return
  319. }
  320. if s.rejectBody(w, r) { // BUG-01 fix: rejectBody returns true when rejected
  321. return
  322. }
  323. s.mu.RLock()
  324. tx := s.tx
  325. s.mu.RUnlock()
  326. if tx == nil {
  327. http.Error(w, "tx controller not available", http.StatusServiceUnavailable)
  328. return
  329. }
  330. if err := tx.ResetFault(); err != nil {
  331. http.Error(w, err.Error(), http.StatusConflict)
  332. return
  333. }
  334. w.Header().Set("Content-Type", "application/json")
  335. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true})
  336. }
  337. // handleAudioStream accepts raw S16LE PCM via HTTP POST and pushes
  338. // it into the configured ingest http-raw source. Use with:
  339. //
  340. // curl -X POST --data-binary @- http://host:8088/audio/stream < audio.raw
  341. // ffmpeg ... -f s16le -ar 44100 -ac 2 - | curl -X POST --data-binary @- http://host:8088/audio/stream
  342. func (s *Server) handleAudioStream(w http.ResponseWriter, r *http.Request) {
  343. if r.Method != http.MethodPost {
  344. s.recordAudit(auditMethodNotAllowed)
  345. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  346. return
  347. }
  348. if !isAudioStreamContentType(r) {
  349. s.recordAudit(auditUnsupportedMediaType)
  350. http.Error(w, audioStreamContentTypeError, http.StatusUnsupportedMediaType)
  351. return
  352. }
  353. s.mu.RLock()
  354. ingress := s.audioIngress
  355. s.mu.RUnlock()
  356. if ingress == nil {
  357. http.Error(w, "audio ingest not configured (use --audio-http with ingest runtime)", http.StatusServiceUnavailable)
  358. return
  359. }
  360. // BUG-10 fix: /audio/stream is a long-lived streaming endpoint.
  361. // The global HTTP server ReadTimeout (5s) and WriteTimeout (10s) would
  362. // kill connections mid-stream. Disable them per-request via ResponseController
  363. // (requires Go 1.20+, confirmed Go 1.22).
  364. rc := http.NewResponseController(w)
  365. _ = rc.SetReadDeadline(time.Time{})
  366. _ = rc.SetWriteDeadline(time.Time{})
  367. r.Body = http.MaxBytesReader(w, r.Body, audioStreamBodyLimit)
  368. // Read body in chunks and push to ring buffer
  369. buf := make([]byte, 32768)
  370. totalFrames := 0
  371. for {
  372. n, err := r.Body.Read(buf)
  373. if n > 0 {
  374. written, writeErr := ingress.WritePCM16(buf[:n])
  375. totalFrames += written
  376. if writeErr != nil {
  377. http.Error(w, writeErr.Error(), http.StatusServiceUnavailable)
  378. return
  379. }
  380. }
  381. if err != nil {
  382. if err == io.EOF {
  383. break
  384. }
  385. var maxErr *http.MaxBytesError
  386. if errors.As(err, &maxErr) {
  387. s.recordAudit(auditBodyTooLarge)
  388. http.Error(w, maxErr.Error(), http.StatusRequestEntityTooLarge)
  389. return
  390. }
  391. http.Error(w, err.Error(), http.StatusInternalServerError)
  392. return
  393. }
  394. }
  395. w.Header().Set("Content-Type", "application/json")
  396. _ = json.NewEncoder(w).Encode(map[string]any{
  397. "ok": true,
  398. "frames": totalFrames,
  399. })
  400. }
  401. func (s *Server) handleTXStart(w http.ResponseWriter, r *http.Request) {
  402. if r.Method != http.MethodPost {
  403. s.recordAudit(auditMethodNotAllowed)
  404. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  405. return
  406. }
  407. if s.rejectBody(w, r) { // BUG-01 fix: rejectBody returns true when rejected
  408. return
  409. }
  410. s.mu.RLock()
  411. tx := s.tx
  412. s.mu.RUnlock()
  413. if tx == nil {
  414. http.Error(w, "tx controller not available", http.StatusServiceUnavailable)
  415. return
  416. }
  417. if err := tx.StartTX(); err != nil {
  418. http.Error(w, err.Error(), http.StatusConflict)
  419. return
  420. }
  421. w.Header().Set("Content-Type", "application/json")
  422. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "action": "started"})
  423. }
  424. func (s *Server) handleTXStop(w http.ResponseWriter, r *http.Request) {
  425. if r.Method != http.MethodPost {
  426. s.recordAudit(auditMethodNotAllowed)
  427. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  428. return
  429. }
  430. if s.rejectBody(w, r) { // BUG-01 fix: rejectBody returns true when rejected
  431. return
  432. }
  433. s.mu.RLock()
  434. tx := s.tx
  435. s.mu.RUnlock()
  436. if tx == nil {
  437. http.Error(w, "tx controller not available", http.StatusServiceUnavailable)
  438. return
  439. }
  440. go func() {
  441. _ = tx.StopTX()
  442. }()
  443. w.Header().Set("Content-Type", "application/json")
  444. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "action": "stop-requested"})
  445. }
  446. func (s *Server) handleDryRun(w http.ResponseWriter, _ *http.Request) {
  447. s.mu.RLock()
  448. cfg := s.cfg
  449. s.mu.RUnlock()
  450. w.Header().Set("Content-Type", "application/json")
  451. _ = json.NewEncoder(w).Encode(drypkg.Generate(cfg))
  452. }
  453. func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
  454. switch r.Method {
  455. case http.MethodGet:
  456. s.mu.RLock()
  457. cfg := s.cfg
  458. s.mu.RUnlock()
  459. w.Header().Set("Content-Type", "application/json")
  460. _ = json.NewEncoder(w).Encode(cfg)
  461. case http.MethodPost:
  462. if !isJSONContentType(r) {
  463. s.recordAudit(auditUnsupportedMediaType)
  464. http.Error(w, "Content-Type must be application/json", http.StatusUnsupportedMediaType)
  465. return
  466. }
  467. r.Body = http.MaxBytesReader(w, r.Body, maxConfigBodyBytes)
  468. var patch ConfigPatch
  469. if err := json.NewDecoder(r.Body).Decode(&patch); err != nil {
  470. statusCode := http.StatusBadRequest
  471. if strings.Contains(err.Error(), "http: request body too large") {
  472. statusCode = http.StatusRequestEntityTooLarge
  473. s.recordAudit(auditBodyTooLarge)
  474. }
  475. http.Error(w, err.Error(), statusCode)
  476. return
  477. }
  478. // Update the server's config snapshot (for GET /config and /status)
  479. s.mu.Lock()
  480. next := s.cfg
  481. if patch.FrequencyMHz != nil {
  482. next.FM.FrequencyMHz = *patch.FrequencyMHz
  483. }
  484. if patch.OutputDrive != nil {
  485. next.FM.OutputDrive = *patch.OutputDrive
  486. }
  487. if patch.ToneLeftHz != nil {
  488. next.Audio.ToneLeftHz = *patch.ToneLeftHz
  489. }
  490. if patch.ToneRightHz != nil {
  491. next.Audio.ToneRightHz = *patch.ToneRightHz
  492. }
  493. if patch.ToneAmplitude != nil {
  494. next.Audio.ToneAmplitude = *patch.ToneAmplitude
  495. }
  496. if patch.AudioGain != nil {
  497. next.Audio.Gain = *patch.AudioGain
  498. }
  499. if patch.PS != nil {
  500. next.RDS.PS = *patch.PS
  501. }
  502. if patch.RadioText != nil {
  503. next.RDS.RadioText = *patch.RadioText
  504. }
  505. if patch.PI != nil {
  506. next.RDS.PI = *patch.PI
  507. }
  508. if patch.PTY != nil {
  509. next.RDS.PTY = *patch.PTY
  510. }
  511. if patch.PreEmphasisTauUS != nil {
  512. next.FM.PreEmphasisTauUS = *patch.PreEmphasisTauUS
  513. }
  514. if patch.StereoEnabled != nil {
  515. next.FM.StereoEnabled = *patch.StereoEnabled
  516. }
  517. if patch.LimiterEnabled != nil {
  518. next.FM.LimiterEnabled = *patch.LimiterEnabled
  519. }
  520. if patch.LimiterCeiling != nil {
  521. next.FM.LimiterCeiling = *patch.LimiterCeiling
  522. }
  523. if patch.RDSEnabled != nil {
  524. next.RDS.Enabled = *patch.RDSEnabled
  525. }
  526. if patch.PilotLevel != nil {
  527. next.FM.PilotLevel = *patch.PilotLevel
  528. }
  529. if patch.RDSInjection != nil {
  530. next.FM.RDSInjection = *patch.RDSInjection
  531. }
  532. if patch.BS412Enabled != nil {
  533. next.FM.BS412Enabled = *patch.BS412Enabled
  534. }
  535. if patch.BS412ThresholdDBr != nil {
  536. next.FM.BS412ThresholdDBr = *patch.BS412ThresholdDBr
  537. }
  538. if patch.MpxGain != nil {
  539. next.FM.MpxGain = *patch.MpxGain
  540. }
  541. if err := next.Validate(); err != nil {
  542. s.mu.Unlock()
  543. http.Error(w, err.Error(), http.StatusBadRequest)
  544. return
  545. }
  546. lp := LivePatch{
  547. FrequencyMHz: patch.FrequencyMHz,
  548. OutputDrive: patch.OutputDrive,
  549. StereoEnabled: patch.StereoEnabled,
  550. PilotLevel: patch.PilotLevel,
  551. RDSInjection: patch.RDSInjection,
  552. RDSEnabled: patch.RDSEnabled,
  553. LimiterEnabled: patch.LimiterEnabled,
  554. LimiterCeiling: patch.LimiterCeiling,
  555. PS: patch.PS,
  556. RadioText: patch.RadioText,
  557. ToneLeftHz: patch.ToneLeftHz,
  558. ToneRightHz: patch.ToneRightHz,
  559. ToneAmplitude: patch.ToneAmplitude,
  560. AudioGain: patch.AudioGain,
  561. }
  562. // NEU-02 fix: determine whether any live-patchable fields are present,
  563. // then release the lock before calling UpdateConfig to avoid holding
  564. // s.mu across a potentially blocking engine call.
  565. tx := s.tx
  566. hasLiveFields := patch.FrequencyMHz != nil || patch.OutputDrive != nil ||
  567. patch.StereoEnabled != nil || patch.PilotLevel != nil ||
  568. patch.RDSInjection != nil || patch.RDSEnabled != nil ||
  569. patch.LimiterEnabled != nil || patch.LimiterCeiling != nil ||
  570. patch.PS != nil || patch.RadioText != nil ||
  571. patch.ToneLeftHz != nil || patch.ToneRightHz != nil ||
  572. patch.ToneAmplitude != nil || patch.AudioGain != nil
  573. s.cfg = next
  574. s.mu.Unlock()
  575. // Apply live fields to running engine outside the lock.
  576. var updateErr error
  577. if tx != nil && hasLiveFields {
  578. if err := tx.UpdateConfig(lp); err != nil {
  579. updateErr = err
  580. }
  581. }
  582. if updateErr != nil {
  583. http.Error(w, updateErr.Error(), http.StatusBadRequest)
  584. return
  585. }
  586. // NEU-03 fix: report live=true only when live-patchable fields were applied.
  587. live := tx != nil && hasLiveFields
  588. w.Header().Set("Content-Type", "application/json")
  589. _ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "live": live})
  590. default:
  591. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  592. }
  593. }
  594. func (s *Server) handleIngestSave(w http.ResponseWriter, r *http.Request) {
  595. if r.Method != http.MethodPost {
  596. s.recordAudit(auditMethodNotAllowed)
  597. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  598. return
  599. }
  600. if !isJSONContentType(r) {
  601. s.recordAudit(auditUnsupportedMediaType)
  602. http.Error(w, "Content-Type must be application/json", http.StatusUnsupportedMediaType)
  603. return
  604. }
  605. r.Body = http.MaxBytesReader(w, r.Body, maxConfigBodyBytes)
  606. var req IngestSaveRequest
  607. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  608. statusCode := http.StatusBadRequest
  609. if strings.Contains(err.Error(), "http: request body too large") {
  610. statusCode = http.StatusRequestEntityTooLarge
  611. s.recordAudit(auditBodyTooLarge)
  612. }
  613. http.Error(w, err.Error(), statusCode)
  614. return
  615. }
  616. s.mu.Lock()
  617. next := s.cfg
  618. next.Ingest = req.Ingest
  619. if err := next.Validate(); err != nil {
  620. s.mu.Unlock()
  621. http.Error(w, err.Error(), http.StatusBadRequest)
  622. return
  623. }
  624. save := s.saveConfig
  625. reload := s.hardReload
  626. if save == nil {
  627. s.mu.Unlock()
  628. http.Error(w, "config save is not configured (start with --config <path>)", http.StatusServiceUnavailable)
  629. return
  630. }
  631. if err := save(next); err != nil {
  632. s.mu.Unlock()
  633. http.Error(w, err.Error(), http.StatusInternalServerError)
  634. return
  635. }
  636. s.cfg = next
  637. s.mu.Unlock()
  638. w.Header().Set("Content-Type", "application/json")
  639. reloadScheduled := reload != nil
  640. _ = json.NewEncoder(w).Encode(map[string]any{
  641. "ok": true,
  642. "saved": true,
  643. "reloadScheduled": reloadScheduled,
  644. })
  645. if reloadScheduled && s.reloadPending.CompareAndSwap(false, true) {
  646. go func(fn func()) {
  647. time.Sleep(250 * time.Millisecond)
  648. s.reloadPending.Store(false)
  649. fn()
  650. }(reload)
  651. }
  652. }