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.

157 lines
2.7 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "gopkg.in/mgo.v2/bson"
  7. "github.com/gorilla/mux"
  8. )
  9. type Routes []Route
  10. var routes = Routes{
  11. Route{
  12. "Index",
  13. "GET",
  14. "/",
  15. Index,
  16. },
  17. Route{
  18. "AllAddresses",
  19. "Get",
  20. "/alladdresses",
  21. AllAddresses,
  22. },
  23. Route{
  24. "AddressNetwork",
  25. "GET",
  26. "/address/network/{address}",
  27. AddressNetwork,
  28. },
  29. Route{
  30. "NetworkMap",
  31. "Get",
  32. "/map",
  33. NetworkMap,
  34. },
  35. /*
  36. Route{
  37. "SelectItem",
  38. "GET",
  39. "/selectItem/{userid}/{itemid}",
  40. SelectItem,
  41. },
  42. */
  43. }
  44. //ROUTES
  45. func Index(w http.ResponseWriter, r *http.Request) {
  46. fmt.Fprintln(w, "ask for recommendations in /r")
  47. //http.FileServer(http.Dir("./web"))
  48. }
  49. /*
  50. func NewUser(w http.ResponseWriter, r *http.Request) {
  51. ipFilter(w, r)
  52. decoder := json.NewDecoder(r.Body)
  53. var newUser UserModel
  54. err := decoder.Decode(&newUser)
  55. check(err)
  56. defer r.Body.Close()
  57. saveUser(userCollection, newUser)
  58. fmt.Println(newUser)
  59. fmt.Fprintln(w, "new user added: ", newUser.ID)
  60. }
  61. */
  62. func AllAddresses(w http.ResponseWriter, r *http.Request) {
  63. ipFilter(w, r)
  64. nodes := []NodeModel{}
  65. iter := nodeCollection.Find(bson.M{}).Limit(10000).Iter()
  66. err := iter.All(&nodes)
  67. //convert []resp struct to json
  68. jsonNodes, err := json.Marshal(nodes)
  69. check(err)
  70. fmt.Fprintln(w, string(jsonNodes))
  71. }
  72. func AddressNetwork(w http.ResponseWriter, r *http.Request) {
  73. ipFilter(w, r)
  74. vars := mux.Vars(r)
  75. address := vars["address"]
  76. if address == "undefined" {
  77. fmt.Fprintln(w, "not valid address")
  78. } else {
  79. network := addressTree(address)
  80. //convert []resp struct to json
  81. jNetwork, err := json.Marshal(network)
  82. check(err)
  83. fmt.Fprintln(w, string(jNetwork))
  84. }
  85. }
  86. func NetworkMap(w http.ResponseWriter, r *http.Request) {
  87. ipFilter(w, r)
  88. nodes, err := getAllNodes()
  89. check(err)
  90. edges, err := getAllEdges()
  91. check(err)
  92. var network NetworkModel
  93. network.Nodes = nodes
  94. network.Edges = edges
  95. //convert []resp struct to json
  96. jNetwork, err := json.Marshal(network)
  97. check(err)
  98. fmt.Fprintln(w, string(jNetwork))
  99. }
  100. /*
  101. func SelectItem(w http.ResponseWriter, r *http.Request) {
  102. ipFilter(w, r)
  103. vars := mux.Vars(r)
  104. userid := vars["userid"]
  105. itemid := vars["itemid"]
  106. //find item
  107. item, err := getItemById(itemid)
  108. if err != nil {
  109. fmt.Fprintln(w, "item "+itemid+" not found")
  110. }
  111. //find user
  112. user, err := getUserById(userid)
  113. if err != nil {
  114. fmt.Fprintln(w, "user "+userid+" not found")
  115. }
  116. //increase TActed in item
  117. item.TActed = item.TActed + 1
  118. //save item
  119. item, err = updateItem(item)
  120. check(err)
  121. fmt.Println(item)
  122. //add item to []Actions of user
  123. user.Actions = append(user.Actions, itemid)
  124. //save user
  125. user, err = updateUser(user)
  126. check(err)
  127. fmt.Println(user)
  128. fmt.Fprintln(w, "user: "+userid+", selects item: "+itemid)
  129. }
  130. */