arnaucube
arnaucube/cellMapVisualizerPublic Self-hosted repository
cellMapVisualizer/serverRoutes.go
serverRoutes.goRaw
 1package main
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6	"net/http"
 7	"strconv"
 8
 9	"github.com/gorilla/mux"
10
11	"gopkg.in/mgo.v2/bson"
12)
13
14type Routes []Route
15
16var routes = Routes{
17	Route{
18		"Index",
19		"GET",
20		"/",
21		Index,
22	},
23	Route{
24		"GetAllCells",
25		"Get",
26		"/allcells",
27		GetAllCells,
28	},
29	Route{
30		"GetCellsInQuad",
31		"Get",
32		"/cells/{lat1}/{lon1}/{lat2}/{lon2}",
33		GetCellsInQuad,
34	},
35}
36
37//ROUTES
38
39func Index(w http.ResponseWriter, r *http.Request) {
40	fmt.Fprintln(w, "ask for cells in /r")
41	//http.FileServer(http.Dir("./web"))
42}
43func GetAllCells(w http.ResponseWriter, r *http.Request) {
44	ipFilter(w, r)
45
46	cells := []CellModel{}
47	iter := cellCollection.Find(bson.M{}).Limit(10000).Iter()
48	err := iter.All(&cells)
49
50	//convert []cells struct to json
51	jsonCells, err := json.Marshal(cells)
52	check(err)
53
54	fmt.Fprintln(w, string(jsonCells))
55}
56func GetCellsInQuad(w http.ResponseWriter, r *http.Request) {
57	ipFilter(w, r)
58
59	vars := mux.Vars(r)
60	lat1, err := strconv.ParseFloat(vars["lat1"], 64)
61	check(err)
62	lon1, err := strconv.ParseFloat(vars["lon1"], 64)
63	check(err)
64	lat2, err := strconv.ParseFloat(vars["lat2"], 64)
65	check(err)
66	lon2, err := strconv.ParseFloat(vars["lon2"], 64)
67	check(err)
68	fmt.Println(vars)
69
70	cells := []CellModel{}
71	iter := cellCollection.Find(bson.M{"lat": bson.M{"$gte": lat2, "$lt": lat1}, "lon": bson.M{"$gte": lon1, "$lt": lon2}}).Limit(100).Iter()
72	err = iter.All(&cells)
73
74	//convert []cells struct to json
75	jsonCells, err := json.Marshal(cells)
76	check(err)
77
78	fmt.Fprintln(w, string(jsonCells))
79}