-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (49 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net/http"
"os"
"github.com/paulmach/go.geojson"
)
func main() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(".")))
mux.HandleFunc("/schools", SchoolsHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
fmt.Println("Server started on", port)
err := http.ListenAndServe(":"+port, mux)
if err != nil {
fmt.Println(err)
return
}
}
func SchoolsHandler(w http.ResponseWriter, r *http.Request) {
schools, err := getRegisteredSchools()
if err != nil {
w.Write([]byte("Could not get registered schools"))
return
}
// sorting schools so that leaflet draws the registered schools on top of
// the not registered schools.
schools = sortSchools(schools)
fc := geojson.NewFeatureCollection()
for _, school := range schools {
geom := geojson.NewPointGeometry([]float64{school.Point.Long, school.Point.Lat})
f := geojson.NewFeature(geom)
f.SetProperty("name", school.Name)
f.SetProperty("email", school.Owner.Email)
f.SetProperty("phonenumber", school.Owner.PhoneNumber)
f.SetProperty("registered", school.Registered)
fc = fc.AddFeature(f)
}
b, err := fc.MarshalJSON()
if err != nil {
fmt.Println(err)
w.Write([]byte("Could not marshal geojson"))
return
}
w.Write(b)
}