arnaucube
arnaucube/cellMapVisualizerPublic Self-hosted repository
cellMapVisualizer/serverRoutes.go
serverRoutes.goRaw
 1package main
 2
 3import (
 4	"encoding/json"
 5	"fmt"
 6	"net/http"
 7
 8	"gopkg.in/mgo.v2/bson"
 9)
10
11type Routes []Route
12
13var routes = Routes{
14	Route{
15		"Index",
16		"GET",
17		"/",
18		Index,
19	},
20	Route{
21		"GetAllCells",
22		"Get",
23		"/allcells",
24		GetAllCells,
25	},
26}
27
28//ROUTES
29
30func Index(w http.ResponseWriter, r *http.Request) {
31	fmt.Fprintln(w, "ask for cells in /r")
32	//http.FileServer(http.Dir("./web"))
33}
34func GetAllCells(w http.ResponseWriter, r *http.Request) {
35	ipFilter(w, r)
36
37	cells := []CellModel{}
38	iter := cellCollection.Find(bson.M{}).Limit(10000).Iter()
39	err := iter.All(&cells)
40
41	//convert []cells struct to json
42	jsonCells, err := json.Marshal(cells)
43	check(err)
44
45	fmt.Fprintln(w, string(jsonCells))
46}