Files
glance/internal/glance/finance.go
Tanmay Karande 492e0ec47c feat: add finance widget and CSS/template updates
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-17 00:36:43 -04:00

48 lines
1.0 KiB
Go

package glance
import (
"encoding/json"
"io"
"net/http"
"os"
)
func handleFinanceGet(w http.ResponseWriter, r *http.Request) {
f, err := os.Open("finance.json")
if err != nil {
w.Header().Set("Content-Type", "application/json")
if os.IsNotExist(err) {
w.Write([]byte(`{"accounts":[],"monthly":{"labels":[],"income":[],"expenses":[],"spend":[]}}`))
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
w.Header().Set("Content-Type", "application/json")
io.Copy(w, f)
}
func handleFinancePost(w http.ResponseWriter, r *http.Request) {
var data interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
f, err := os.Create("finance.json")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
if err := json.NewEncoder(f).Encode(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}