48 lines
1.0 KiB
Go
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)
|
|
}
|