forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_global_settings_handler.go
113 lines (91 loc) · 2.59 KB
/
http_global_settings_handler.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package server
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util"
"github.com/gorilla/mux"
"gopkg.in/yaml.v3"
)
func settingsGetStringHandler(key string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, _ := settings.String(key)
jsonResult(w, res)
}
}
func settingsDeleteHandler(key string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
settings.SetString(key, "")
jsonResult(w, true)
}
}
func settingsSetDurationHandler(key string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
val, err := strconv.Atoi(vars["value"])
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
settings.SetInt(key, int64(time.Second*time.Duration(val)))
setConfigDirty()
jsonResult(w, val)
}
}
func settingsSetYamlHandler(key string, other, struc any) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
if err := yaml.NewDecoder(bytes.NewBuffer(b)).Decode(&other); err != nil && err != io.EOF {
jsonError(w, http.StatusBadRequest, err)
return
}
if err := settings.DecodeOtherSliceOrMap(other, &struc); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
val := strings.TrimSpace(string(b))
settings.SetString(key, val)
setConfigDirty()
jsonResult(w, val)
}
}
// func settingsGetJsonHandler(key string, struc any) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// if err := settings.Json(key, &struc); err != nil && err != settings.ErrNotFound {
// jsonError(w, http.StatusInternalServerError, err)
// return
// }
// jsonResult(w, struc)
// }
// }
func settingsSetJsonHandler(key string, valueChan chan<- util.Param, struc any) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(&struc); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
settings.SetJson(key, struc)
setConfigDirty()
valueChan <- util.Param{Key: key, Val: struc}
jsonResult(w, true)
}
}
func settingsDeleteJsonHandler(key string, valueChan chan<- util.Param, struc any) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
settings.SetString(key, "")
setConfigDirty()
valueChan <- util.Param{Key: key, Val: struc}
jsonResult(w, true)
}
}