Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

123 řádky
3.3 KiB

  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  9. "github.com/AFASystems/presence/internal/pkg/model"
  10. "github.com/gorilla/mux"
  11. "github.com/segmentio/kafka-go"
  12. )
  13. func BeaconsListSingleController(appstate *appcontext.AppState) http.HandlerFunc {
  14. return func(w http.ResponseWriter, r *http.Request) {
  15. vars := mux.Vars(r)
  16. id := vars["beacon_id"]
  17. beacon, ok := appstate.GetBeacon(id)
  18. if !ok {
  19. w.Header().Set("Content-Type", "application/json")
  20. w.WriteHeader(http.StatusNotFound)
  21. json.NewEncoder(w).Encode(map[string]string{"error": "Beacon not found"})
  22. return
  23. }
  24. w.Header().Set("Content-Type", "application/json")
  25. w.WriteHeader(http.StatusOK)
  26. json.NewEncoder(w).Encode(beacon)
  27. }
  28. }
  29. func BeaconsListController(appstate *appcontext.AppState) http.HandlerFunc {
  30. return func(w http.ResponseWriter, r *http.Request) {
  31. beacons := appstate.GetAllBeacons()
  32. w.Header().Set("Content-Type", "application/json")
  33. w.WriteHeader(http.StatusOK)
  34. json.NewEncoder(w).Encode(beacons)
  35. }
  36. }
  37. // Probably define value as interface and then reuse this writer in all of the functions
  38. func sendKafkaMessage(writer *kafka.Writer, value *model.ApiUpdate, ctx context.Context) error {
  39. valueStr, err := json.Marshal(&value)
  40. if err != nil {
  41. fmt.Println("error in encoding: ", err)
  42. return err
  43. }
  44. msg := kafka.Message{
  45. Value: valueStr,
  46. }
  47. if err := writer.WriteMessages(ctx, msg); err != nil {
  48. fmt.Println("Error in sending kafka message: ", err)
  49. return err
  50. }
  51. return nil
  52. }
  53. func BeaconsDeleteController(writer *kafka.Writer, ctx context.Context, appstate *appcontext.AppState) http.HandlerFunc {
  54. return func(w http.ResponseWriter, r *http.Request) {
  55. vars := mux.Vars(r)
  56. beaconId := vars["beacon_id"]
  57. apiUpdate := model.ApiUpdate{
  58. Method: "DELETE",
  59. ID: beaconId,
  60. }
  61. fmt.Printf("Sending DELETE beacon id: %s message\n", beaconId)
  62. if err := sendKafkaMessage(writer, &apiUpdate, ctx); err != nil {
  63. fmt.Println("error in sending Kafka DELETE message")
  64. http.Error(w, "Error in sending kafka message", 500)
  65. return
  66. }
  67. // If message is succesfully sent delete the beacon from the list
  68. appstate.RemoveBeacon(beaconId)
  69. w.Write([]byte("ok"))
  70. }
  71. }
  72. func BeaconsAddController(writer *kafka.Writer, ctx context.Context) http.HandlerFunc {
  73. return func(w http.ResponseWriter, r *http.Request) {
  74. decoder := json.NewDecoder(r.Body)
  75. var inBeacon model.Beacon
  76. err := decoder.Decode(&inBeacon)
  77. fmt.Printf("hello world\n")
  78. if err != nil {
  79. http.Error(w, err.Error(), 400)
  80. return
  81. }
  82. fmt.Printf("hello world\n")
  83. fmt.Printf("in beacon: %+v\n", inBeacon)
  84. if (len(strings.TrimSpace(inBeacon.Name)) == 0) || (len(strings.TrimSpace(inBeacon.ID)) == 0) {
  85. http.Error(w, "name and beacon_id cannot be blank", 400)
  86. return
  87. }
  88. fmt.Printf("Adding new print here also\n")
  89. // fmt.Printf("sending POST beacon id: %s message\n", inBeacon.ID)
  90. apiUpdate := model.ApiUpdate{
  91. Method: "POST",
  92. Beacon: inBeacon,
  93. }
  94. fmt.Printf("message: %+v\n", apiUpdate)
  95. if err := sendKafkaMessage(writer, &apiUpdate, ctx); err != nil {
  96. fmt.Println("error in sending Kafka POST message")
  97. http.Error(w, "Error in sending kafka message", 500)
  98. return
  99. }
  100. w.Write([]byte("ok"))
  101. }
  102. }