-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
handler.go
39 lines (34 loc) · 996 Bytes
/
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
package server
import (
"github.com/Clymene-project/Clymene/cmd/agent/app/config"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap"
"net/http"
)
type APIHandler struct {
l *zap.Logger
Reloader []func(cfg *config.Config) error
ConfigFile string
}
// NewAPIHandler returns a new APIHandler
func NewAPIHandler(p *HttpServerParams) *APIHandler {
return &APIHandler{
l: p.Logger,
Reloader: p.Reloader,
ConfigFile: p.ConfigFile,
}
}
func (aH *APIHandler) RegisterRoutes(router *mux.Router) {
router.HandleFunc("/api/reload", aH.Reload).Methods(http.MethodGet)
}
func (aH *APIHandler) Reload(w http.ResponseWriter, r *http.Request) {
if err := config.ReloadConfig(aH.ConfigFile, aH.l, aH.Reloader...); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errors.Wrapf(err, "error loading config from %q", aH.ConfigFile).Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("config reload, success"))
return
}