-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
43 lines (37 loc) · 1.08 KB
/
config.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
package handlers
import (
"encoding/json"
"os"
"strconv"
"github.com/labstack/echo/v4"
"github.com/m50/wygoming-satellite/services/config"
"github.com/m50/wygoming-satellite/views"
)
type ConfigHandler struct {
conf *config.Config
}
func NewConfigHandler(conf *config.Config) ConfigHandler {
return ConfigHandler{
conf,
}
}
func (h *ConfigHandler) HandleGet(c echo.Context) error {
return views.RenderView(c, 200, views.Config(h.conf.Values))
}
func (h *ConfigHandler) HandleUpdate(c echo.Context) error {
p, err := strconv.Atoi(c.FormValue("WebUIPort"))
if err != nil {
return err
}
h.conf.Values.WebUIPort = uint16(p)
h.conf.Values.LogLevel = c.FormValue("LogLevel")
h.conf.Values.HomeAssistant.Address = c.FormValue("HomeAssistantAddress")
h.conf.Values.HomeAssistant.AccessToken = c.FormValue("HomeAssistantAccessToken")
h.conf.Values.MQTTBroker = c.FormValue("MQTTBroker")
cnf, err := json.MarshalIndent(h.conf.Values, "", " ")
if err != nil {
return err
}
os.WriteFile(h.conf.ConfigPath, cnf, 664)
return views.RenderView(c, 200, views.Config(h.conf.Values))
}