|
@@ -2,6 +2,7 @@ package controller
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "errors"
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
"strings"
|
|
"strings"
|
|
@@ -12,6 +13,7 @@ import (
|
|
"golang.org/x/exp/slog"
|
|
"golang.org/x/exp/slog"
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
+ "github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/logic"
|
|
"github.com/gravitl/netmaker/logic"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/mq"
|
|
"github.com/gravitl/netmaker/mq"
|
|
@@ -41,6 +43,10 @@ func serverHandlers(r *mux.Router) {
|
|
).Methods(http.MethodPost)
|
|
).Methods(http.MethodPost)
|
|
r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).
|
|
r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).
|
|
Methods(http.MethodGet)
|
|
Methods(http.MethodGet)
|
|
|
|
+ r.HandleFunc("/api/server/settings", allowUsers(http.HandlerFunc(getSettings))).
|
|
|
|
+ Methods(http.MethodGet)
|
|
|
|
+ r.HandleFunc("/api/server/settings", allowUsers(http.HandlerFunc(updateSettings))).
|
|
|
|
+ Methods(http.MethodPut)
|
|
r.HandleFunc("/api/server/getserverinfo", logic.SecurityCheck(true, http.HandlerFunc(getServerInfo))).
|
|
r.HandleFunc("/api/server/getserverinfo", logic.SecurityCheck(true, http.HandlerFunc(getServerInfo))).
|
|
Methods(http.MethodGet)
|
|
Methods(http.MethodGet)
|
|
r.HandleFunc("/api/server/status", getStatus).Methods(http.MethodGet)
|
|
r.HandleFunc("/api/server/status", getStatus).Methods(http.MethodGet)
|
|
@@ -207,7 +213,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
// get params
|
|
// get params
|
|
|
|
|
|
- json.NewEncoder(w).Encode(servercfg.GetServerInfo())
|
|
|
|
|
|
+ json.NewEncoder(w).Encode(logic.GetServerInfo())
|
|
// w.WriteHeader(http.StatusOK)
|
|
// w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -230,3 +236,38 @@ func getConfig(w http.ResponseWriter, r *http.Request) {
|
|
json.NewEncoder(w).Encode(scfg)
|
|
json.NewEncoder(w).Encode(scfg)
|
|
// w.WriteHeader(http.StatusOK)
|
|
// w.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// @Summary Get the server settings
|
|
|
|
+// @Router /api/server/settings [get]
|
|
|
|
+// @Tags Server
|
|
|
|
+// @Security oauth2
|
|
|
|
+// @Success 200 {object} config.ServerSettings
|
|
|
|
+func getSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ scfg := logic.GetServerSettings()
|
|
|
|
+ logic.ReturnSuccessResponseWithJson(w, r, scfg, "fetched server settings successfully")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// @Summary Update the server settings
|
|
|
|
+// @Router /api/server/settings [put]
|
|
|
|
+// @Tags Server
|
|
|
|
+// @Security oauth2
|
|
|
|
+// @Success 200 {object} config.ServerSettings
|
|
|
|
+func updateSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ var req models.ServerSettings
|
|
|
|
+
|
|
|
|
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
+ logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
|
|
|
|
+ logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if !logic.ValidateNewSettings(req) {
|
|
|
|
+ logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("invalid settings"), "badrequest"))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ err := logic.UpsertServerSettings(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failed to udpate server settings "+err.Error()), "internal"))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ logic.ReturnSuccessResponseWithJson(w, r, req, "updated server settings successfully")
|
|
|
|
+}
|