diff --git a/control.go b/control.go index 1b8ec76..c07e444 100644 --- a/control.go +++ b/control.go @@ -54,7 +54,7 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux { go c.Run() sm := http.NewServeMux() - sm.Handle("/", JsonHandler(c.Index)) + sm.Handle("/", JsonHandler(c.Info)) sm.Handle("/ws/", websocket.Handler(c.AddPlayer)) sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame)) 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/stop/", JsonHandler(c.StopGame)) sm.HandleFunc("/api/v0/fsu/", c.KillServer) + sm.HandleFunc("/api/v0/info/", c.Info) return sm } @@ -307,23 +308,6 @@ func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) { 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 // django regex-specified routes. func (c *Controller) getGameId(path string) (string, error) { diff --git a/info.go b/info.go new file mode 100644 index 0000000..3f7b7a3 --- /dev/null +++ b/info.go @@ -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) +}