Add basic API endpoints to get server data
This commit is contained in:
parent
cedbee5080
commit
134debd529
57
http.go
57
http.go
|
@ -8,26 +8,61 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
Servers []*server.Server
|
||||
type ServerCollection []*server.Server
|
||||
|
||||
// Retrieves a server out of the collection by UUID.
|
||||
func (sc *ServerCollection) Get(uuid string) *server.Server {
|
||||
for _, s := range *sc {
|
||||
if s.Uuid == uuid {
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) routeIndex(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
Servers ServerCollection
|
||||
}
|
||||
|
||||
// Middleware to protect server specific routes. This will ensure that the server exists and
|
||||
// is in a state that allows it to be exposed to the API.
|
||||
func (rt *Router) AuthenticateServer(h httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if rt.Servers.Get(ps.ByName("server")) != nil {
|
||||
h(w, r, ps)
|
||||
return
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the basic Wings index page without anything else.
|
||||
func (rt *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)
|
||||
// Returns all of the servers that exist on the Daemon. This route is only accessible to
|
||||
// requests that include an administrative control key, otherwise a 404 is returned. This
|
||||
// authentication is handled by a middleware.
|
||||
func (rt *Router) routeAllServers(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
||||
json.NewEncoder(w).Encode(rt.Servers)
|
||||
}
|
||||
|
||||
func (r *Router) ConfigureRouter() *httprouter.Router {
|
||||
// Returns basic information about a single server found on the Daemon.
|
||||
func (rt *Router) routeServer(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
|
||||
s := rt.Servers.Get(ps.ByName("server"))
|
||||
|
||||
json.NewEncoder(w).Encode(s)
|
||||
}
|
||||
|
||||
func (rt *Router) ConfigureRouter() *httprouter.Router {
|
||||
router := httprouter.New()
|
||||
|
||||
router.GET("/", r.routeIndex)
|
||||
|
||||
router.GET("/api/servers", r.routeAllServers)
|
||||
// router.GET("/api/servers/:server", r.routeServer)
|
||||
router.GET("/", rt.routeIndex)
|
||||
router.GET("/api/servers", rt.routeAllServers)
|
||||
router.GET("/api/servers/:server", rt.AuthenticateServer(rt.routeServer))
|
||||
|
||||
return router
|
||||
}
|
Loading…
Reference in New Issue
Block a user