From a1fa876734161bb33d05a96d3037c46df69c685c Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 9 Dec 2019 21:05:55 -0800 Subject: [PATCH] [#3c9g25] Add endpoint for returning basic system information --- http.go | 16 ++++++++++++++++ system.go | 31 +++++++++++++++++++++++++++++++ wings.go | 3 +-- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 system.go diff --git a/http.go b/http.go index 1669cfc..e251e4a 100644 --- a/http.go +++ b/http.go @@ -8,6 +8,7 @@ import ( "github.com/buger/jsonparser" "github.com/gorilla/websocket" "github.com/julienschmidt/httprouter" + "github.com/pkg/errors" "github.com/pterodactyl/wings/installer" "github.com/pterodactyl/wings/server" "go.uber.org/zap" @@ -449,6 +450,20 @@ func (rt *Router) routeCreateServer(w http.ResponseWriter, r *http.Request, ps h w.WriteHeader(http.StatusAccepted) } +func (rt *Router) routeSystemInformation(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + defer r.Body.Close() + + s, err := GetSystemInformation() + if err != nil { + zap.S().Errorw("failed to retrieve system information", zap.Error(errors.WithStack(err))) + + http.Error(w, "failed to retrieve information", http.StatusInternalServerError) + return + } + + json.NewEncoder(w).Encode(s) +} + func (rt *Router) ReaderToBytes(r io.Reader) []byte { buf := bytes.Buffer{} buf.ReadFrom(r) @@ -461,6 +476,7 @@ func (rt *Router) ConfigureRouter() *httprouter.Router { router := httprouter.New() router.GET("/", rt.routeIndex) + router.GET("/api/system", rt.AuthenticateToken(rt.routeSystemInformation)) router.GET("/api/servers", rt.AuthenticateToken(rt.routeAllServers)) router.GET("/api/servers/:server", rt.AuthenticateRequest(rt.routeServer)) router.GET("/api/servers/:server/ws", rt.AuthenticateServer(rt.routeWebsocket)) diff --git a/system.go b/system.go new file mode 100644 index 0000000..b201069 --- /dev/null +++ b/system.go @@ -0,0 +1,31 @@ +package main + +import ( + "github.com/docker/docker/pkg/parsers/kernel" + "runtime" +) + +type SystemInformation struct { + Version string `json:"version"` + KernelVersion string `json:"kernel_version"` + Architecture string `json:"architecture"` + OS string `json:"os"` + CpuCount int `json:"cpu_count"` +} + +func GetSystemInformation() (*SystemInformation, error) { + k, err := kernel.GetKernelVersion() + if err != nil { + return nil, err + } + + s := &SystemInformation{ + Version: Version, + KernelVersion: k.String(), + Architecture: runtime.GOARCH, + OS: runtime.GOOS, + CpuCount: runtime.NumCPU(), + } + + return s, nil +} diff --git a/wings.go b/wings.go index deda418..44b8357 100644 --- a/wings.go +++ b/wings.go @@ -23,8 +23,6 @@ func main() { flag.Parse() - zap.S().Infof("using configuration file: %s", configPath) - c, err := config.ReadConfiguration(configPath) if err != nil { panic(err) @@ -40,6 +38,7 @@ func main() { panic(err) } + zap.S().Infof("using configuration from path: %s", configPath) if c.Debug { zap.S().Debugw("running in debug mode") zap.S().Infow("certificate checking is disabled")