From f57c24002ef546021f4a5ff2a0a9d81158512299 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 4 Apr 2021 10:20:27 -0700 Subject: [PATCH] More API response fixing --- router/router_server.go | 6 +----- router/router_system.go | 14 ++------------ server/server.go | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/router/router_server.go b/router/router_server.go index ec01483..d2924f1 100644 --- a/router/router_server.go +++ b/router/router_server.go @@ -18,11 +18,7 @@ import ( // Returns a single server from the collection of servers. func getServer(c *gin.Context) { - s := ExtractServer(c) - c.JSON(http.StatusOK, ServerJsonResponse{ - Configuration: s.Config(), - Resources: s.Proc(), - }) + c.JSON(http.StatusOK, ExtractServer(c).ToAPIResponse()) } // Returns the logs for a given server instance. diff --git a/router/router_system.go b/router/router_system.go index 28ee144..60cf617 100644 --- a/router/router_system.go +++ b/router/router_system.go @@ -14,13 +14,6 @@ import ( "github.com/pterodactyl/wings/system" ) -// ServerJsonResponse is a type returned when requesting details about a single -// server instance on Wings. -type ServerJsonResponse struct { - *server.Configuration - Resources server.ResourceUsage `json:"resources"` -} - // Returns information about the system that wings is running on. func getSystemInformation(c *gin.Context) { i, err := system.GetSystemInformation() @@ -37,12 +30,9 @@ func getSystemInformation(c *gin.Context) { // this wings instance. func getAllServers(c *gin.Context) { servers := middleware.ExtractManager(c).All() - out := make([]ServerJsonResponse, len(servers), len(servers)) + out := make([]server.APIResponse, len(servers), len(servers)) for i, v := range servers { - out[i] = ServerJsonResponse{ - Configuration: v.Config(), - Resources: v.Proc(), - } + out[i] = v.ToAPIResponse() } c.JSON(http.StatusOK, out) } diff --git a/server/server.go b/server/server.go index e485ed9..a4bb24e 100644 --- a/server/server.go +++ b/server/server.go @@ -305,3 +305,24 @@ func (s *Server) IsRunning() bool { return st == environment.ProcessRunningState || st == environment.ProcessStartingState } + +// APIResponse is a type returned when requesting details about a single server +// instance on Wings. This includes the information needed by the Panel in order +// to show resource utilization and the current state on this system. +type APIResponse struct { + State string `json:"state"` + IsSuspended bool `json:"is_suspended"` + Utilization ResourceUsage `json:"utilization"` + Configuration Configuration `json:"configuration"` +} + +// ToAPIResponse returns the server struct as an API object that can be consumed +// by callers. +func (s *Server) ToAPIResponse() APIResponse { + return APIResponse{ + State: s.Environment.State(), + IsSuspended: s.IsSuspended(), + Utilization: s.Proc(), + Configuration: *s.Config(), + } +}