blob: eee5d09b4ac32857ffe7c9a24e445cddb72bdf8a (
plain)
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
|
package api
import (
"net/http"
"github.com/thepeterstone/claudomator/internal/budget"
)
// budgetReporter exposes per-provider spend headroom. Satisfied by
// *budget.Accountant; an interface keeps the handler testable.
type budgetReporter interface {
All() ([]budget.Headroom, error)
}
// SetBudget wires the budget accountant so GET /api/budget can report headroom.
func (s *Server) SetBudget(b budgetReporter) {
s.budget = b
}
// handleGetBudget returns per-provider rolling-window spend headroom. When no
// budget is configured it returns an empty list so the UI simply shows nothing.
func (s *Server) handleGetBudget(w http.ResponseWriter, _ *http.Request) {
if s.budget == nil {
writeJSON(w, http.StatusOK, []budget.Headroom{})
return
}
hs, err := s.budget.All()
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if hs == nil {
hs = []budget.Headroom{}
}
writeJSON(w, http.StatusOK, hs)
}
|