Add initial API support for listing configured servers

This commit is contained in:
Dane Everitt 2019-04-05 22:20:26 -07:00
parent ec2407481b
commit cedbee5080
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 67 additions and 20 deletions

1
go.mod
View File

@ -15,6 +15,7 @@ require (
github.com/gogo/protobuf v1.0.0 // indirect github.com/gogo/protobuf v1.0.0 // indirect
github.com/google/go-cmp v0.2.0 // indirect github.com/google/go-cmp v0.2.0 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/julienschmidt/httprouter v1.2.0
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect github.com/onsi/gomega v1.5.0 // indirect

2
go.sum
View File

@ -28,6 +28,8 @@ github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQ
github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

33
http.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/pterodactyl/wings/server"
"net/http"
)
type Router struct {
Servers []*server.Server
}
func (r *Router) routeIndex(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func (r *Router) routeAllServers(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
enc := json.NewEncoder(w)
enc.Encode(r.Servers)
}
func (r *Router) ConfigureRouter() *httprouter.Router {
router := httprouter.New()
router.GET("/", r.routeIndex)
router.GET("/api/servers", r.routeAllServers)
// router.GET("/api/servers/:server", r.routeServer)
return router
}

View File

@ -15,29 +15,29 @@ type Server struct {
// The unique identifier for the server that should be used when referencing // The unique identifier for the server that should be used when referencing
// it aganist the Panel API (and internally). This will be used when naming // it aganist the Panel API (and internally). This will be used when naming
// docker containers as well as in log output. // docker containers as well as in log output.
Uuid string Uuid string `json:"uuid"`
// Wether or not the server is in a suspended state. Suspended servers cannot // Wether or not the server is in a suspended state. Suspended servers cannot
// be started or modified except in certain scenarios by an admin user. // be started or modified except in certain scenarios by an admin user.
Suspended bool Suspended bool `json:"suspended"`
// The power state of the server. // The power state of the server.
State int State int `json:"state"`
// The command that should be used when booting up the server instance. // The command that should be used when booting up the server instance.
Invocation string Invocation string `json:"invocation"`
// An array of environment variables that should be passed along to the running // An array of environment variables that should be passed along to the running
// server process. // server process.
EnvVars map[string]string `yaml:"env"` EnvVars map[string]string `json:"environment" yaml:"env"`
Build *BuildSettings Build *BuildSettings `json:"build"`
Allocations *Allocations Allocations *Allocations `json:"allocations"`
Container struct { Container struct {
// Defines the Docker image that will be used for this server // Defines the Docker image that will be used for this server
Image string Image string `json:"image,omitempty"`
} } `json:"container,omitempty"`
environment Environment environment Environment
@ -49,22 +49,22 @@ type Server struct {
type BuildSettings struct { type BuildSettings struct {
// The total amount of memory in megabytes that this server is allowed to // The total amount of memory in megabytes that this server is allowed to
// use on the host system. // use on the host system.
MemoryLimit int64 `yaml:"memory"` MemoryLimit int64 `json:"memory_limit" yaml:"memory"`
// The amount of additional swap space to be provided to a container instance. // The amount of additional swap space to be provided to a container instance.
Swap int64 Swap int64 `json:"swap"`
// The relative weight for IO operations in a container. This is relative to other // The relative weight for IO operations in a container. This is relative to other
// containers on the system and should be a value between 10 and 1000. // containers on the system and should be a value between 10 and 1000.
IoWeight uint16 `yaml:"io"` IoWeight uint16 `json:"io_weight" yaml:"io"`
// The percentage of CPU that this instance is allowed to consume relative to // The percentage of CPU that this instance is allowed to consume relative to
// the host. A value of 200% represents complete utilization of two cores. This // the host. A value of 200% represents complete utilization of two cores. This
// should be a value between 1 and THREAD_COUNT * 100. // should be a value between 1 and THREAD_COUNT * 100.
CpuLimit int64 `yaml:"cpu"` CpuLimit int64 `json:"cpu_limit" yaml:"cpu"`
// The amount of disk space in megabytes that a server is allowed to use. // The amount of disk space in megabytes that a server is allowed to use.
DiskSpace int64 `yaml:"disk"` DiskSpace int64 `json:"disk_space" yaml:"disk"`
} }
// Converts the CPU limit for a server build into a number that can be better understood // Converts the CPU limit for a server build into a number that can be better understood
@ -96,13 +96,13 @@ type Allocations struct {
// what will be used for {SERVER_IP} and {SERVER_PORT} when modifying configuration // what will be used for {SERVER_IP} and {SERVER_PORT} when modifying configuration
// files or the startup arguments for a server. // files or the startup arguments for a server.
DefaultMapping struct { DefaultMapping struct {
Ip string Ip string `json:"ip"`
Port int Port int `json:"port"`
} `yaml:"default"` } `json:"default" yaml:"default"`
// Mappings contains all of the ports that should be assigned to a given server // Mappings contains all of the ports that should be assigned to a given server
// attached to the IP they correspond to. // attached to the IP they correspond to.
Mappings map[string][]int Mappings map[string][]int `json:"mappings"`
} }
// Iterates over a given directory and loads all of the servers listed before returning // Iterates over a given directory and loads all of the servers listed before returning
@ -184,7 +184,7 @@ func FromConfiguration(data []byte, cfg DockerConfiguration) (*Server, error) {
s.fs = &Filesystem{ s.fs = &Filesystem{
// @todo adjust this to be configuration provided! // @todo adjust this to be configuration provided!
Root: "/srv/daemon-data", Root: "/srv/daemon-data",
Server: s, Server: s,
} }
@ -205,4 +205,4 @@ func (s *Server) IsBootable() bool {
// for the server is setup, and that all of the necessary files are created. // for the server is setup, and that all of the necessary files are created.
func (s *Server) CreateEnvironment() error { func (s *Server) CreateEnvironment() error {
return s.environment.Create() return s.environment.Create()
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/pterodactyl/wings/server" "github.com/pterodactyl/wings/server"
"go.uber.org/zap" "go.uber.org/zap"
"net/http"
) )
// Entrypoint for the Wings application. Configures the logger and checks any // Entrypoint for the Wings application. Configures the logger and checks any
@ -50,6 +51,16 @@ func main() {
zap.S().Errorw("failed to create an environment for server", zap.String("server", s.Uuid), zap.Error(err)) zap.S().Errorw("failed to create an environment for server", zap.String("server", s.Uuid), zap.Error(err))
} }
} }
r := &Router{
Servers: servers,
}
router := r.ConfigureRouter()
zap.S().Infow("configuring webserver", zap.String("host", c.Api.Host), zap.Int("port", c.Api.Port))
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", c.Api.Host, c.Api.Port), router); err != nil {
zap.S().Fatalw("failed to configure HTTP server", zap.Error(err))
}
} }
// Configures the global logger for Zap so that we can call it from any location // Configures the global logger for Zap so that we can call it from any location