blob: 94923d2f84e5bda861766e20f64304a683a755f2 [file] [log] [blame]
Serge Bazanski7ea8e472020-12-04 10:48:37 +01001package main
2
3import (
4 "encoding/json"
5 "net/http"
6)
7
8type PeopleNowPresent struct {
9 Names []string `json:"names"`
10 Value int `json:"value"`
11}
12
13type SpaceAPI struct {
14 Sensors struct {
15 PeopleNowPresent []PeopleNowPresent `json:"people_now_present"`
16 } `json:"sensors"`
17}
18
19func (s *server) viewSpaceAPI(w http.ResponseWriter, r *http.Request) {
20 res := &SpaceAPI{}
21
22 names := []string{}
23 for _, p := range s.online(r.Context()) {
24 names = append(names, p.Character)
25 }
26 res.Sensors.PeopleNowPresent = []PeopleNowPresent{
27 {
28 Names: names,
29 Value: len(names),
30 },
31 }
32
33 w.Header().Set("Content-Type", "application/json")
34 json.NewEncoder(w).Encode(res)
35}