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 ( import (
"bytes" "bytes"
"encoding/json"
"html/template" "html/template"
"log"
"net/http" "net/http"
"sync"
) )
var geoJSONTmpl = template.Must(template.New("geojson").Parse( var geoJSONTmpl = template.Must(template.New("geojson").Parse(
@ -28,6 +31,7 @@ type players struct {
fifo string fifo string
wsf *websocketForwarder wsf *websocketForwarder
pls []*player pls []*player
mu sync.RWMutex
} }
func newPlayers(fifo string, wsf *websocketForwarder) *players { 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) { 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)
}
} }