-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
94 lines (79 loc) · 2.29 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
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
package http
import (
"encoding/json"
"github.com/SyncYomi/SyncYomi/internal/config"
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"net/http"
)
type configJson struct {
Host string `json:"host"`
Port int `json:"port"`
LogLevel string `json:"log_level"`
LogPath string `json:"log_path"`
LogMaxSize int `json:"log_max_size"`
LogMaxBackups int `json:"log_max_backups"`
BaseURL string `json:"base_url"`
CheckForUpdates bool `json:"check_for_updates"`
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}
type configHandler struct {
encoder encoder
cfg *config.AppConfig
server Server
}
func newConfigHandler(encoder encoder, server Server, cfg *config.AppConfig) *configHandler {
return &configHandler{
encoder: encoder,
cfg: cfg,
server: server,
}
}
func (h configHandler) Routes(r chi.Router) {
r.Get("/", h.getConfig)
r.Patch("/", h.updateConfig)
}
func (h configHandler) getConfig(w http.ResponseWriter, r *http.Request) {
conf := configJson{
Host: h.cfg.Config.Host,
Port: h.cfg.Config.Port,
LogLevel: h.cfg.Config.LogLevel,
LogPath: h.cfg.Config.LogPath,
LogMaxSize: h.cfg.Config.LogMaxSize,
LogMaxBackups: h.cfg.Config.LogMaxBackups,
BaseURL: h.cfg.Config.BaseURL,
CheckForUpdates: h.cfg.Config.CheckForUpdates,
Version: h.server.version,
Commit: h.server.commit,
Date: h.server.date,
}
render.JSON(w, r, conf)
}
func (h configHandler) updateConfig(w http.ResponseWriter, r *http.Request) {
var data domain.ConfigUpdate
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
if data.CheckForUpdates != nil {
h.cfg.Config.CheckForUpdates = *data.CheckForUpdates
}
if data.LogLevel != nil {
h.cfg.Config.LogLevel = *data.LogLevel
}
if data.LogPath != nil {
h.cfg.Config.LogPath = *data.LogPath
}
if err := h.cfg.UpdateConfig(); err != nil {
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, errorResponse{
Message: err.Error(),
Status: http.StatusInternalServerError,
})
return
}
render.NoContent(w, r)
}