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.

202 lines
3.8 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. "AddressSankey",
  31. "GET",
  32. "/address/sankey/{address}",
  33. AddressSankey,
  34. },
  35. Route{
  36. "NetworkMap",
  37. "Get",
  38. "/map",
  39. NetworkMap,
  40. },
  41. /*
  42. Route{
  43. "SelectItem",
  44. "GET",
  45. "/selectItem/{userid}/{itemid}",
  46. SelectItem,
  47. },
  48. */
  49. }
  50. //ROUTES
  51. func Index(w http.ResponseWriter, r *http.Request) {
  52. fmt.Fprintln(w, "ask for recommendations in /r")
  53. //http.FileServer(http.Dir("./web"))
  54. }
  55. /*
  56. func NewUser(w http.ResponseWriter, r *http.Request) {
  57. ipFilter(w, r)
  58. decoder := json.NewDecoder(r.Body)
  59. var newUser UserModel
  60. err := decoder.Decode(&newUser)
  61. check(err)
  62. defer r.Body.Close()
  63. saveUser(userCollection, newUser)
  64. fmt.Println(newUser)
  65. fmt.Fprintln(w, "new user added: ", newUser.ID)
  66. }
  67. */
  68. func AllAddresses(w http.ResponseWriter, r *http.Request) {
  69. ipFilter(w, r)
  70. nodes := []NodeModel{}
  71. iter := nodeCollection.Find(bson.M{}).Limit(10000).Iter()
  72. err := iter.All(&nodes)
  73. //convert []resp struct to json
  74. jsonNodes, err := json.Marshal(nodes)
  75. check(err)
  76. fmt.Fprintln(w, string(jsonNodes))
  77. }
  78. func AddressNetwork(w http.ResponseWriter, r *http.Request) {
  79. ipFilter(w, r)
  80. vars := mux.Vars(r)
  81. address := vars["address"]
  82. if address == "undefined" {
  83. fmt.Fprintln(w, "not valid address")
  84. } else {
  85. network := addressTree(address)
  86. //convert []resp struct to json
  87. jNetwork, err := json.Marshal(network)
  88. check(err)
  89. fmt.Fprintln(w, string(jNetwork))
  90. }
  91. }
  92. func AddressSankey(w http.ResponseWriter, r *http.Request) {
  93. ipFilter(w, r)
  94. vars := mux.Vars(r)
  95. address := vars["address"]
  96. if address == "undefined" {
  97. fmt.Fprintln(w, "not valid address")
  98. } else {
  99. network := addressTree(address)
  100. var sankey SankeyModel
  101. fmt.Println("network generated")
  102. mapNodeK := make(map[string]int)
  103. for k, n := range network.Nodes {
  104. var sankeyNode SankeyNodeModel
  105. //sankeyNode.StringNode = n.Id
  106. sankeyNode.Node = k
  107. sankeyNode.Name = n.Id
  108. sankey.Nodes = append(sankey.Nodes, sankeyNode)
  109. mapNodeK[n.Id] = k
  110. }
  111. for _, e := range network.Edges {
  112. var sankeyLink SankeyLinkModel
  113. //sankeyLink.StringSource = e.From
  114. sankeyLink.Source = mapNodeK[e.From]
  115. //sankeyLink.StringTarget = e.To
  116. sankeyLink.Target = mapNodeK[e.To]
  117. sankeyLink.Value = e.Label
  118. sankey.Links = append(sankey.Links, sankeyLink)
  119. }
  120. fmt.Println("Sankey generated")
  121. //convert []resp struct to json
  122. jsonSankey, err := json.Marshal(sankey)
  123. check(err)
  124. fmt.Fprintln(w, string(jsonSankey))
  125. }
  126. }
  127. func NetworkMap(w http.ResponseWriter, r *http.Request) {
  128. ipFilter(w, r)
  129. nodes, err := getAllNodes()
  130. check(err)
  131. edges, err := getAllEdges()
  132. check(err)
  133. var network NetworkModel
  134. network.Nodes = nodes
  135. network.Edges = edges
  136. //convert []resp struct to json
  137. jNetwork, err := json.Marshal(network)
  138. check(err)
  139. fmt.Fprintln(w, string(jNetwork))
  140. }
  141. /*
  142. func SelectItem(w http.ResponseWriter, r *http.Request) {
  143. ipFilter(w, r)
  144. vars := mux.Vars(r)
  145. userid := vars["userid"]
  146. itemid := vars["itemid"]
  147. //find item
  148. item, err := getItemById(itemid)
  149. if err != nil {
  150. fmt.Fprintln(w, "item "+itemid+" not found")
  151. }
  152. //find user
  153. user, err := getUserById(userid)
  154. if err != nil {
  155. fmt.Fprintln(w, "user "+userid+" not found")
  156. }
  157. //increase TActed in item
  158. item.TActed = item.TActed + 1
  159. //save item
  160. item, err = updateItem(item)
  161. check(err)
  162. fmt.Println(item)
  163. //add item to []Actions of user
  164. user.Actions = append(user.Actions, itemid)
  165. //save user
  166. user, err = updateUser(user)
  167. check(err)
  168. fmt.Println(user)
  169. fmt.Fprintln(w, "user: "+userid+", selects item: "+itemid)
  170. }
  171. */