Serge Bazanski | 7ea8e47 | 2020-12-04 10:48:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | type PeopleNowPresent struct { |
| 9 | Names []string `json:"names"` |
| 10 | Value int `json:"value"` |
| 11 | } |
| 12 | |
| 13 | type SpaceAPI struct { |
| 14 | Sensors struct { |
| 15 | PeopleNowPresent []PeopleNowPresent `json:"people_now_present"` |
| 16 | } `json:"sensors"` |
| 17 | } |
| 18 | |
| 19 | func (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 | } |