[#3c9g25] Add endpoint for returning basic system information

This commit is contained in:
Dane Everitt 2019-12-09 21:05:55 -08:00
parent 89806427f9
commit a1fa876734
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 48 additions and 2 deletions

16
http.go
View File

@ -8,6 +8,7 @@ import (
"github.com/buger/jsonparser" "github.com/buger/jsonparser"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/installer" "github.com/pterodactyl/wings/installer"
"github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server"
"go.uber.org/zap" "go.uber.org/zap"
@ -449,6 +450,20 @@ func (rt *Router) routeCreateServer(w http.ResponseWriter, r *http.Request, ps h
w.WriteHeader(http.StatusAccepted) 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 { func (rt *Router) ReaderToBytes(r io.Reader) []byte {
buf := bytes.Buffer{} buf := bytes.Buffer{}
buf.ReadFrom(r) buf.ReadFrom(r)
@ -461,6 +476,7 @@ func (rt *Router) ConfigureRouter() *httprouter.Router {
router := httprouter.New() router := httprouter.New()
router.GET("/", rt.routeIndex) router.GET("/", rt.routeIndex)
router.GET("/api/system", rt.AuthenticateToken(rt.routeSystemInformation))
router.GET("/api/servers", rt.AuthenticateToken(rt.routeAllServers)) router.GET("/api/servers", rt.AuthenticateToken(rt.routeAllServers))
router.GET("/api/servers/:server", rt.AuthenticateRequest(rt.routeServer)) router.GET("/api/servers/:server", rt.AuthenticateRequest(rt.routeServer))
router.GET("/api/servers/:server/ws", rt.AuthenticateServer(rt.routeWebsocket)) router.GET("/api/servers/:server/ws", rt.AuthenticateServer(rt.routeWebsocket))

31
system.go Normal file
View File

@ -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
}

View File

@ -23,8 +23,6 @@ func main() {
flag.Parse() flag.Parse()
zap.S().Infof("using configuration file: %s", configPath)
c, err := config.ReadConfiguration(configPath) c, err := config.ReadConfiguration(configPath)
if err != nil { if err != nil {
panic(err) panic(err)
@ -40,6 +38,7 @@ func main() {
panic(err) panic(err)
} }
zap.S().Infof("using configuration from path: %s", configPath)
if c.Debug { if c.Debug {
zap.S().Debugw("running in debug mode") zap.S().Debugw("running in debug mode")
zap.S().Infow("certificate checking is disabled") zap.S().Infow("certificate checking is disabled")