+4
-8
internal/pubsub.go
+4
-8
internal/pubsub.go
···
100
100
}
101
101
}
102
102
103
-
type Input struct {
104
-
Value uint `json:"value"`
105
-
Name string `json:"name"`
106
-
}
107
-
108
103
func (p *PubSub) HandlePublish(w http.ResponseWriter, r *http.Request) {
109
104
topicName := r.URL.Query().Get("topic")
110
105
topic, exists := p.topics[topicName]
···
113
108
return
114
109
}
115
110
116
-
var input Input
111
+
var input interface{}
117
112
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
118
113
http.Error(w, err.Error(), http.StatusBadRequest)
119
114
return
120
115
}
121
116
117
+
b, _ := json.Marshal(input)
122
118
for _, client := range topic {
123
-
client.channel <- fmt.Sprintf("%+v", input)
119
+
client.channel <- string(b)
124
120
}
125
121
126
122
w.Header().Set("Content-Type", "application/json")
127
123
w.WriteHeader(http.StatusOK)
128
-
w.Write([]byte("Message received"))
124
+
w.Write([]byte(`{"msg": "Message received"}`))
129
125
}