Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

176 wiersze
4.6 KiB

  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  8. "github.com/AFASystems/presence/internal/pkg/model"
  9. "github.com/gorilla/mux"
  10. "github.com/segmentio/kafka-go"
  11. )
  12. func GetBeaconIds(appstate *appcontext.AppState) http.HandlerFunc {
  13. return func(w http.ResponseWriter, r *http.Request) {
  14. beacons := appstate.GetAllHttpResults()
  15. bIds := make([]string, len(beacons))
  16. i := 0
  17. for k := range beacons {
  18. bIds[i] = k
  19. i++
  20. }
  21. w.Header().Set("Content-Type", "application/json")
  22. w.WriteHeader(http.StatusOK)
  23. json.NewEncoder(w).Encode(bIds)
  24. }
  25. }
  26. func BeaconsListSingleController(appstate *appcontext.AppState) http.HandlerFunc {
  27. return func(w http.ResponseWriter, r *http.Request) {
  28. vars := mux.Vars(r)
  29. id := vars["beacon_id"]
  30. beacon, ok := appstate.GetHTTPResult(id)
  31. if !ok {
  32. w.Header().Set("Content-Type", "application/json")
  33. w.WriteHeader(http.StatusNotFound)
  34. json.NewEncoder(w).Encode(map[string]string{"error": "Beacon not found"})
  35. return
  36. }
  37. w.Header().Set("Content-Type", "application/json")
  38. w.WriteHeader(http.StatusOK)
  39. json.NewEncoder(w).Encode(beacon)
  40. }
  41. }
  42. func BeaconsListController(appstate *appcontext.AppState) http.HandlerFunc {
  43. return func(w http.ResponseWriter, r *http.Request) {
  44. beacons := appstate.GetAllHttpResults()
  45. values := make([]model.HTTPResult, len(beacons))
  46. i := 0
  47. for k, v := range beacons {
  48. if k == "" {
  49. continue
  50. }
  51. values[i] = v
  52. i++
  53. }
  54. w.Header().Set("Content-Type", "application/json")
  55. w.WriteHeader(http.StatusOK)
  56. json.NewEncoder(w).Encode(values)
  57. }
  58. }
  59. // Probably define value as interface and then reuse this writer in all of the functions
  60. func sendKafkaMessage(writer *kafka.Writer, value *model.ApiUpdate, ctx context.Context) error {
  61. valueStr, err := json.Marshal(&value)
  62. if err != nil {
  63. fmt.Println("error in encoding: ", err)
  64. return err
  65. }
  66. msg := kafka.Message{
  67. Value: valueStr,
  68. }
  69. if err := writer.WriteMessages(ctx, msg); err != nil {
  70. fmt.Println("Error in sending kafka message: ", err)
  71. return err
  72. }
  73. return nil
  74. }
  75. func BeaconsDeleteController(writer *kafka.Writer, ctx context.Context, appstate *appcontext.AppState) http.HandlerFunc {
  76. return func(w http.ResponseWriter, r *http.Request) {
  77. vars := mux.Vars(r)
  78. beaconId := vars["beacon_id"]
  79. apiUpdate := model.ApiUpdate{
  80. Method: "DELETE",
  81. ID: beaconId,
  82. }
  83. fmt.Printf("Sending DELETE beacon id: %s message\n", beaconId)
  84. if err := sendKafkaMessage(writer, &apiUpdate, ctx); err != nil {
  85. fmt.Println("error in sending Kafka DELETE message")
  86. http.Error(w, "Error in sending kafka message", 500)
  87. return
  88. }
  89. // If message is succesfully sent delete the beacon from the list
  90. appstate.RemoveHTTPResult(beaconId)
  91. w.Write([]byte("ok"))
  92. }
  93. }
  94. func BeaconAddService(writer *kafka.Writer, ctx context.Context, appstate *appcontext.AppState, beacon model.HTTPResult) error {
  95. id := beacon.ID
  96. appstate.UpdateHTTPResult(id, beacon)
  97. apiUpdate := model.ApiUpdate{
  98. Method: "POST",
  99. Beacon: beacon,
  100. }
  101. if err := sendKafkaMessage(writer, &apiUpdate, ctx); err != nil {
  102. return err
  103. }
  104. return nil
  105. }
  106. func BeaconsAddController(writer *kafka.Writer, ctx context.Context, appstate *appcontext.AppState) http.HandlerFunc {
  107. return func(w http.ResponseWriter, r *http.Request) {
  108. decoder := json.NewDecoder(r.Body)
  109. var inBeacon model.HTTPResult
  110. err := decoder.Decode(&inBeacon)
  111. if err != nil {
  112. http.Error(w, err.Error(), 400)
  113. return
  114. }
  115. if inBeacon.ID == "" {
  116. http.Error(w, "Beacon needs to include MAC addr", 400)
  117. return
  118. }
  119. if err := BeaconAddService(writer, ctx, appstate, inBeacon); err != nil {
  120. fmt.Println("error in sending Kafka POST message")
  121. http.Error(w, "Error in sending kafka message", 500)
  122. return
  123. }
  124. w.Write([]byte("ok"))
  125. }
  126. }
  127. func AddListOfBeaconsController(writer *kafka.Writer, ctx context.Context, appstate *appcontext.AppState) http.HandlerFunc {
  128. return func(w http.ResponseWriter, r *http.Request) {
  129. decoder := json.NewDecoder(r.Body)
  130. var inBeacons []model.HTTPResult
  131. if err := decoder.Decode(&inBeacons); err != nil {
  132. http.Error(w, err.Error(), 400)
  133. return
  134. }
  135. for _, v := range inBeacons {
  136. if v.ID == "" {
  137. fmt.Println("One of the beacons is missing MAC address")
  138. http.Error(w, "One of the beacons is missing MAC address", 400)
  139. return
  140. }
  141. if err := BeaconAddService(writer, ctx, appstate, v); err != nil {
  142. fmt.Println("error in sending Kafka POST message")
  143. http.Error(w, "Error in sending kafka message", 500)
  144. return
  145. }
  146. }
  147. w.Write([]byte("ok"))
  148. }
  149. }