2021-04-07 07:13:34 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2021-08-17 04:30:55 +08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-10-27 00:27:29 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2021-08-17 04:30:55 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-04-07 07:13:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2021-04-07 07:13:34 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-07 07:13:34 +08:00
|
|
|
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,
|
2021-04-07 07:13:34 +08:00
|
|
|
}
|
|
|
|
bearerToken := r.Header.Get("Authorization")
|
|
|
|
var tokenSplit = strings.Split(bearerToken, " ")
|
2021-08-17 04:30:55 +08:00
|
|
|
var authToken = ""
|
2021-04-07 07:13:34 +08:00
|
|
|
if len(tokenSplit) < 2 {
|
2022-09-15 01:26:31 +08:00
|
|
|
logic.ReturnErrorResponse(w, r, errorResponse)
|
2021-08-17 04:30:55 +08:00
|
|
|
return
|
|
|
|
} else {
|
2021-04-07 07:13:34 +08:00
|
|
|
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-08-17 04:30:55 +08:00
|
|
|
}
|
2021-06-01 23:14:34 +08:00
|
|
|
next.ServeHTTP(w, r)
|
2021-04-07 07:13:34 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-17 04:30:55 +08:00
|
|
|
|
2022-09-15 20:47:48 +08:00
|
|
|
// 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
|
2021-08-17 04:30:55 +08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-05-06 06:03:37 +08:00
|
|
|
|
2021-08-17 04:30:55 +08:00
|
|
|
// get params
|
2021-05-06 06:03:37 +08:00
|
|
|
|
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)
|
2021-05-06 06:03:37 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 20:47:48 +08:00
|
|
|
// 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")
|
2021-04-07 07:13:34 +08:00
|
|
|
|
2022-05-31 20:42:12 +08:00
|
|
|
// get params
|
2021-04-07 07:13:34 +08:00
|
|
|
|
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)
|
|
|
|
}
|