netmaker/controllers/server.go

94 lines
2.5 KiB
Go
Raw Normal View History

package controller
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
2021-10-27 00:27:29 +08:00
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
)
func serverHandlers(r *mux.Router) {
2022-01-13 08:45:42 +08:00
// r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(true, http.HandlerFunc(addNetwork))).Methods("POST")
2022-09-26 19:25:33 +08:00
r.HandleFunc("/api/server/health", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusOK)
resp.Write([]byte("Server is up and running!!"))
}))
2022-09-15 01:26:31 +08:00
r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).Methods("GET")
2022-05-31 20:42:12 +08:00
r.HandleFunc("/api/server/getserverinfo", authorize(true, false, "node", http.HandlerFunc(getServerInfo))).Methods("GET")
}
2022-09-15 01:26:31 +08:00
// allowUsers - allow all authenticated (valid) users - only used by getConfig, may be able to remove during refactor
func allowUsers(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var errorResponse = models.ErrorResponse{
2022-09-15 01:26:31 +08:00
Code: http.StatusInternalServerError, Message: logic.Unauthorized_Msg,
}
bearerToken := r.Header.Get("Authorization")
var tokenSplit = strings.Split(bearerToken, " ")
var authToken = ""
if len(tokenSplit) < 2 {
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
return
} else {
authToken = tokenSplit[1]
}
2022-09-15 01:26:31 +08:00
user, _, _, err := logic.VerifyUserToken(authToken)
if err != nil || user == "" {
logic.ReturnErrorResponse(w, r, errorResponse)
2021-08-20 07:00:23 +08:00
return
}
2021-06-01 23:14:34 +08:00
next.ServeHTTP(w, r)
}
}
// swagger:route GET /api/server/getserverinfo server getServerInfo
2022-09-06 20:20:24 +08:00
//
// Get the server configuration.
//
2022-09-25 19:11:26 +08:00
// Schemes: https
2022-09-06 20:20:24 +08:00
//
2022-09-25 19:11:26 +08:00
// Security:
// oauth
2022-09-11 12:51:59 +08:00
//
2022-09-25 19:11:26 +08:00
// Responses:
// 200: serverConfigResponse
2022-05-31 20:42:12 +08:00
func getServerInfo(w http.ResponseWriter, r *http.Request) {
2021-06-01 23:14:34 +08:00
// Set header
w.Header().Set("Content-Type", "application/json")
// get params
2022-06-01 00:07:56 +08:00
json.NewEncoder(w).Encode(servercfg.GetServerInfo())
2021-08-20 07:00:23 +08:00
//w.WriteHeader(http.StatusOK)
}
// swagger:route GET /api/server/getconfig server getConfig
2022-09-06 20:20:24 +08:00
//
// Get the server configuration.
//
2022-09-25 19:11:26 +08:00
// Schemes: https
2022-09-06 20:20:24 +08:00
//
2022-09-25 19:11:26 +08:00
// Security:
// oauth
2022-09-11 12:51:59 +08:00
//
2022-09-25 19:11:26 +08:00
// Responses:
// 200: serverConfigResponse
2022-05-31 20:42:12 +08:00
func getConfig(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")
2022-05-31 20:42:12 +08:00
// get params
2022-05-31 20:42:12 +08:00
scfg := servercfg.GetServerConfig()
2022-09-14 03:44:45 +08:00
scfg.IsEE = "no"
2022-09-25 19:11:26 +08:00
if servercfg.Is_EE {
2022-09-14 03:44:45 +08:00
scfg.IsEE = "yes"
}
2022-05-31 20:42:12 +08:00
json.NewEncoder(w).Encode(scfg)
//w.WriteHeader(http.StatusOK)
}