server.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "github.com/gorilla/mux"
  7. "github.com/gravitl/netmaker/database"
  8. "github.com/gravitl/netmaker/logic"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/mq"
  11. "github.com/gravitl/netmaker/servercfg"
  12. )
  13. func serverHandlers(r *mux.Router) {
  14. // r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(true, http.HandlerFunc(addNetwork))).Methods(http.MethodPost)
  15. r.HandleFunc("/api/server/health", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
  16. resp.WriteHeader(http.StatusOK)
  17. resp.Write([]byte("Server is up and running!!"))
  18. }))
  19. r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).Methods(http.MethodGet)
  20. r.HandleFunc("/api/server/getserverinfo", Authorize(true, false, "node", http.HandlerFunc(getServerInfo))).Methods(http.MethodGet)
  21. r.HandleFunc("/api/server/status", http.HandlerFunc(getStatus)).Methods(http.MethodGet)
  22. r.HandleFunc("/api/server/usage", Authorize(true, false, "user", http.HandlerFunc(getUsage))).Methods(http.MethodGet)
  23. }
  24. func getUsage(w http.ResponseWriter, r *http.Request) {
  25. type usage struct {
  26. Hosts int `json:"hosts"`
  27. Clients int `json:"clients"`
  28. Networks int `json:"networks"`
  29. Users int `json:"users"`
  30. Ingresses int `json:"ingresses"`
  31. Egresses int `json:"egresses"`
  32. }
  33. var serverUsage usage
  34. hosts, err := logic.GetAllHosts()
  35. if err == nil {
  36. serverUsage.Hosts = len(hosts)
  37. }
  38. clients, err := logic.GetAllExtClients()
  39. if err == nil {
  40. serverUsage.Clients = len(clients)
  41. }
  42. users, err := logic.GetUsers()
  43. if err == nil {
  44. serverUsage.Users = len(users)
  45. }
  46. networks, err := logic.GetNetworks()
  47. if err == nil {
  48. serverUsage.Networks = len(networks)
  49. }
  50. ingresses, err := logic.GetAllIngresses()
  51. if err == nil {
  52. serverUsage.Ingresses = len(ingresses)
  53. }
  54. egresses, err := logic.GetAllEgresses()
  55. if err == nil {
  56. serverUsage.Egresses = len(egresses)
  57. }
  58. w.Header().Set("Content-Type", "application/json")
  59. json.NewEncoder(w).Encode(models.SuccessResponse{
  60. Code: http.StatusOK,
  61. Response: serverUsage,
  62. })
  63. }
  64. // swagger:route GET /api/server/status server getStatus
  65. //
  66. // Get the server configuration.
  67. //
  68. // Schemes: https
  69. //
  70. // Security:
  71. // oauth
  72. //
  73. // Responses:
  74. // 200: serverConfigResponse
  75. func getStatus(w http.ResponseWriter, r *http.Request) {
  76. type status struct {
  77. DB bool `json:"db_connected"`
  78. Broker bool `json:"broker_connected"`
  79. LicenseError string `json:"license_error"`
  80. IsPro bool `json:"is_pro"`
  81. }
  82. licenseErr := ""
  83. if servercfg.ErrLicenseValidation != nil {
  84. licenseErr = servercfg.ErrLicenseValidation.Error()
  85. }
  86. currentServerStatus := status{
  87. DB: database.IsConnected(),
  88. Broker: mq.IsConnected(),
  89. LicenseError: licenseErr,
  90. IsPro: servercfg.IsPro,
  91. }
  92. w.Header().Set("Content-Type", "application/json")
  93. json.NewEncoder(w).Encode(&currentServerStatus)
  94. }
  95. // allowUsers - allow all authenticated (valid) users - only used by getConfig, may be able to remove during refactor
  96. func allowUsers(next http.Handler) http.HandlerFunc {
  97. return func(w http.ResponseWriter, r *http.Request) {
  98. var errorResponse = models.ErrorResponse{
  99. Code: http.StatusUnauthorized, Message: logic.Unauthorized_Msg,
  100. }
  101. bearerToken := r.Header.Get("Authorization")
  102. var tokenSplit = strings.Split(bearerToken, " ")
  103. var authToken = ""
  104. if len(tokenSplit) < 2 {
  105. logic.ReturnErrorResponse(w, r, errorResponse)
  106. return
  107. } else {
  108. authToken = tokenSplit[1]
  109. }
  110. user, _, _, err := logic.VerifyUserToken(authToken)
  111. if err != nil || user == "" {
  112. logic.ReturnErrorResponse(w, r, errorResponse)
  113. return
  114. }
  115. next.ServeHTTP(w, r)
  116. }
  117. }
  118. // swagger:route GET /api/server/getserverinfo server getServerInfo
  119. //
  120. // Get the server configuration.
  121. //
  122. // Schemes: https
  123. //
  124. // Security:
  125. // oauth
  126. //
  127. // Responses:
  128. // 200: serverConfigResponse
  129. func getServerInfo(w http.ResponseWriter, r *http.Request) {
  130. // Set header
  131. w.Header().Set("Content-Type", "application/json")
  132. // get params
  133. json.NewEncoder(w).Encode(servercfg.GetServerInfo())
  134. //w.WriteHeader(http.StatusOK)
  135. }
  136. // swagger:route GET /api/server/getconfig server getConfig
  137. //
  138. // Get the server configuration.
  139. //
  140. // Schemes: https
  141. //
  142. // Security:
  143. // oauth
  144. //
  145. // Responses:
  146. // 200: serverConfigResponse
  147. func getConfig(w http.ResponseWriter, r *http.Request) {
  148. // Set header
  149. w.Header().Set("Content-Type", "application/json")
  150. // get params
  151. scfg := servercfg.GetServerConfig()
  152. scfg.IsPro = "no"
  153. if servercfg.IsPro {
  154. scfg.IsPro = "yes"
  155. }
  156. json.NewEncoder(w).Encode(scfg)
  157. //w.WriteHeader(http.StatusOK)
  158. }