Serve GeoJSON encoded players via /players.

This commit is contained in:
Sascha L. Teichmann 2015-03-09 14:08:52 +01:00
parent cbfb594c83
commit ff08f2af94

View File

@ -2,8 +2,11 @@ package main
import (
"bytes"
"encoding/json"
"html/template"
"log"
"net/http"
"sync"
)
var geoJSONTmpl = template.Must(template.New("geojson").Parse(
@ -28,6 +31,7 @@ type players struct {
fifo string
wsf *websocketForwarder
pls []*player
mu sync.RWMutex
}
func newPlayers(fifo string, wsf *websocketForwarder) *players {
@ -49,5 +53,11 @@ func (ps *players) run() {
}
func (ps *players) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// TODO: Implement me!
rw.Header().Set("Content-Type", "application/json")
ps.mu.RLock()
defer ps.mu.RUnlock()
encoder := json.NewEncoder(rw)
if err := encoder.Encode(ps.pls); err != nil {
log.Printf("error: sending JSON failed: %s\n", err)
}
}