Guest User

Untitled

a guest
Dec 7th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "net/http"
  5.  
  6. "github.com/gorilla/mux"
  7. )
  8.  
  9. type Route struct {
  10. Name string
  11. Method string
  12. Pattern string
  13. HandlerFunc http.HandlerFunc
  14. }
  15.  
  16. type Routes []Route
  17.  
  18. func NewRouter() *mux.Router {
  19. router := mux.NewRouter().StrictSlash(true)
  20. for _, route := range routes {
  21. router.
  22. Methods(route.Method).
  23. Path(route.Pattern).
  24. Name(route.Name).
  25. Handler(route.HandlerFunc)
  26. }
  27.  
  28. return router
  29. }
  30.  
  31. var routes = Routes{
  32. Route{
  33. "CustomerLocationCreate",
  34. "POST",
  35. "/tracking/customer",
  36. CustomerLocationCreate,
  37. },
  38. }
  39.  
  40. package main
  41.  
  42. import (
  43. "encoding/json"
  44. "net/http"
  45. "io"
  46. "io/ioutil"
  47. )
  48.  
  49. //curl -H "Content-Type: application/json" -d '{"userId":"1234"}' http://localhost:8181/tracking/customer
  50. func CustomerLocationCreate(w http.ResponseWriter, r *http.Request) {
  51. var location CustomerLocation
  52. body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  53. if err != nil {
  54. panic(err)
  55. }
  56. if err := r.Body.Close(); err != nil {
  57. panic(err)
  58. }
  59. if err := json.Unmarshal(body, &location); err != nil {
  60. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  61. w.WriteHeader(422) // unprocessable entity
  62. if err := json.NewEncoder(w).Encode(err); err != nil {
  63. panic(err)
  64. }
  65. }
  66.  
  67. c := RepoCreateCustomerLocation(location)
  68. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  69. w.WriteHeader(http.StatusCreated)
  70. if err := json.NewEncoder(w).Encode(c); err != nil {
  71. panic(err)
  72. }
  73.  
  74. HandleCustomerLocationChange(c);
  75. }
  76.  
  77. func HandleCustomerLocationChange(custLoc CustomerLocation) {
  78.  
  79. endpoint := og.Getenv("RABBIT_ENDPOINT")
  80. conn, err := amqp.Dial("amqp://guest:guest@" + endpoint)
  81. failOnError(err, "Failed to connect to RabbitMQ")
  82. defer conn.Close()
  83.  
  84. ch, err := conn.Channel()
  85. failOnError(err, "Failed to open a channel")
  86. defer ch.Close()
  87.  
  88. topic := "locationChange"
  89. err = ch.ExchangeDeclare(
  90. topic, // name
  91. "topic", // type
  92. true, // durable
  93. false, // auto-deleted
  94. false, // internal
  95. false, // no-wait
  96. nil, // arguments
  97. )
  98. failOnError(err, "Failed to declare an exchange")
  99.  
  100. // Create JSON from the instance data.
  101. body, _ := json.Marshal(custLoc)
  102. // Convert bytes to string.
  103.  
  104. err = ch.Publish(
  105. topic, // exchange
  106. "", // routing key
  107. false, // mandatory
  108. false, // immediate
  109. amqp.Publishing{
  110. ContentType: "text/plain",
  111. Body: body,
  112. })
  113. failOnError(err, "Failed to publish a message")
  114.  
  115. log.Printf(" [x] Sent %s", body)
  116.  
  117. }
  118.  
  119. body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  120. if err != nil {
  121. panic(err)
  122. }
  123.  
  124. body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  125. if err != nil {
  126. http.Error(w, err.Error(), http.StatusBadRequest)
  127. return
  128. }
  129.  
  130. func HandleCustomerLocationChange(custLoc CustomerLocation)
  131. ...
  132. conn, err := amqp.Dial(...)
  133. failOnError(err, "Failed to connect to RabbitMQ")
  134.  
  135. func HandleCustomerLocationChange(custLoc CustomerLocation) error
  136. ...
  137. conn, err := amqp.Dial(...)
  138. if err != nil {
  139. return fmt.Errorf("failed to connect to RabbitMQ: %s", err)
  140. }
Add Comment
Please, Sign In to add comment