Browse Source

Made info url more useful (start and uptime)

Stephen McQuay 4 years ago
parent
commit
5cac75b638
2 changed files with 34 additions and 18 deletions
  1. 2
    18
      control.go
  2. 32
    0
      info.go

+ 2
- 18
control.go View File

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

+ 32
- 0
info.go View File

@@ -0,0 +1,32 @@
1
+package server
2
+
3
+import (
4
+	"encoding/json"
5
+	"log"
6
+	"net/http"
7
+	"time"
8
+)
9
+
10
+const Version = "0.0.0"
11
+
12
+var start time.Time
13
+
14
+func init() {
15
+	start = time.Now()
16
+}
17
+
18
+// Info provides server version and uptime stats
19
+func (c *Controller) Info(w http.ResponseWriter, req *http.Request) {
20
+	log.Println("version requested")
21
+	output := struct {
22
+		Version string `json:"version"`
23
+		Start   string `json:"start"`
24
+		Uptime  string `json:"uptime"`
25
+	}{
26
+		Version: Version,
27
+		Start:   start.Format("2006-01-02 15:04:05"),
28
+		Uptime:  time.Since(start).String(),
29
+	}
30
+	w.Header().Set("Content-Type", "application/json")
31
+	json.NewEncoder(w).Encode(output)
32
+}

Loading…
Cancel
Save