mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
)
|
|
|
|
var geoJSONTmpl = template.Must(template.New("geojson").Parse(
|
|
`{ "type": "Feature",
|
|
"geometry": {
|
|
"type": "Point",
|
|
"coordinates": [{{.X}}, {{.Z}}]
|
|
},
|
|
"properties": {
|
|
"name": {{.Name || html }}
|
|
}
|
|
}`))
|
|
|
|
type player struct {
|
|
X float64
|
|
Y float64
|
|
Z float64
|
|
Name string
|
|
}
|
|
|
|
type players struct {
|
|
fifo string
|
|
wsf *websocketForwarder
|
|
pls []*player
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func newPlayers(fifo string, wsf *websocketForwarder) *players {
|
|
return &players{fifo: fifo, wsf: wsf}
|
|
}
|
|
|
|
func (p *player) MarshalJSON() ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
if err := geoJSONTmpl.Execute(&buf, p); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func (ps *players) run() {
|
|
for {
|
|
// TODO: Implement me!
|
|
}
|
|
}
|
|
|
|
func (ps *players) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
}
|
|
}
|