You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.5 KiB

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