Made info url more useful (start and uptime)

This commit is contained in:
Stephen McQuay 2015-08-26 22:25:38 -07:00
parent b5cac7c02a
commit 5cac75b638
2 changed files with 34 additions and 18 deletions

View File

@ -54,7 +54,7 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux {
go c.Run() go c.Run()
sm := http.NewServeMux() sm := http.NewServeMux()
sm.Handle("/", JsonHandler(c.Index)) sm.Handle("/", JsonHandler(c.Info))
sm.Handle("/ws/", websocket.Handler(c.AddPlayer)) sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame)) sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame))
sm.Handle("/api/v0/game/list/", JsonHandler(c.ListGames)) sm.Handle("/api/v0/game/list/", JsonHandler(c.ListGames))
@ -62,6 +62,7 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux {
sm.Handle("/api/v0/game/bw/", JsonHandler(c.BW)) sm.Handle("/api/v0/game/bw/", JsonHandler(c.BW))
sm.Handle("/api/v0/game/stop/", JsonHandler(c.StopGame)) sm.Handle("/api/v0/game/stop/", JsonHandler(c.StopGame))
sm.HandleFunc("/api/v0/fsu/", c.KillServer) sm.HandleFunc("/api/v0/fsu/", c.KillServer)
sm.HandleFunc("/api/v0/info/", c.Info)
return sm return sm
} }
@ -307,23 +308,6 @@ func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) {
log.Fatal("shit got fucked up") log.Fatal("shit got fucked up")
} }
// Index is the function for handling all traffic not officially in the API. It
// just lets people know that this is a hackerbots server running at
// a particular version.
func (c *Controller) Index(w http.ResponseWriter, req *http.Request) {
log.Println("version requested")
version := struct {
Version string `json:"version"`
Name string `json:"name"`
}{
Version: "0.1.2",
Name: "Hackerbots",
}
if err := json.NewEncoder(w).Encode(version); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// getGameId trims the gameid off of the url. This is hokey, and makes me miss // getGameId trims the gameid off of the url. This is hokey, and makes me miss
// django regex-specified routes. // django regex-specified routes.
func (c *Controller) getGameId(path string) (string, error) { func (c *Controller) getGameId(path string) (string, error) {

32
info.go Normal file
View File

@ -0,0 +1,32 @@
package server
import (
"encoding/json"
"log"
"net/http"
"time"
)
const Version = "0.0.0"
var start time.Time
func init() {
start = time.Now()
}
// Info provides server version and uptime stats
func (c *Controller) Info(w http.ResponseWriter, req *http.Request) {
log.Println("version requested")
output := struct {
Version string `json:"version"`
Start string `json:"start"`
Uptime string `json:"uptime"`
}{
Version: Version,
Start: start.Format("2006-01-02 15:04:05"),
Uptime: time.Since(start).String(),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}