More API response fixing

This commit is contained in:
Dane Everitt 2021-04-04 10:20:27 -07:00
parent 8dfd494eaf
commit f57c24002e
3 changed files with 24 additions and 17 deletions

View File

@ -18,11 +18,7 @@ import (
// Returns a single server from the collection of servers. // Returns a single server from the collection of servers.
func getServer(c *gin.Context) { func getServer(c *gin.Context) {
s := ExtractServer(c) c.JSON(http.StatusOK, ExtractServer(c).ToAPIResponse())
c.JSON(http.StatusOK, ServerJsonResponse{
Configuration: s.Config(),
Resources: s.Proc(),
})
} }
// Returns the logs for a given server instance. // Returns the logs for a given server instance.

View File

@ -14,13 +14,6 @@ import (
"github.com/pterodactyl/wings/system" "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. // Returns information about the system that wings is running on.
func getSystemInformation(c *gin.Context) { func getSystemInformation(c *gin.Context) {
i, err := system.GetSystemInformation() i, err := system.GetSystemInformation()
@ -37,12 +30,9 @@ func getSystemInformation(c *gin.Context) {
// this wings instance. // this wings instance.
func getAllServers(c *gin.Context) { func getAllServers(c *gin.Context) {
servers := middleware.ExtractManager(c).All() 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 { for i, v := range servers {
out[i] = ServerJsonResponse{ out[i] = v.ToAPIResponse()
Configuration: v.Config(),
Resources: v.Proc(),
}
} }
c.JSON(http.StatusOK, out) c.JSON(http.StatusOK, out)
} }

View File

@ -305,3 +305,24 @@ func (s *Server) IsRunning() bool {
return st == environment.ProcessRunningState || st == environment.ProcessStartingState 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(),
}
}