moonmq icon indicating copy to clipboard operation
moonmq copied to clipboard

the way to publish message via http channel

Open ghiewa opened this issue 10 years ago • 2 comments
trafficstars

does message cover queue/routing_key/pub_type already? fail to publish messag there.

    v := url.Values{}
    v.Set("queue", "SIGNAL")
    body := ioutil.NopCloser(strings.NewReader(v.Encode())) //把form数据编下码
    client := &http.Client{}
    req, _ := http.NewRequest("POST", "http://httpbin.org/post", body)
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") 
    resp, err := client.Do(req) //发送
    defer resp.Body.Close()     //一定要关闭resp.Body

http_msg.go

func (h *MsgHandler) publishMsg(w http.ResponseWriter, r *http.Request) {
    message, err := ioutil.ReadAll(r.Body)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    queue := r.FormValue("queue")
    routingKey := r.FormValue("routing_key")
    tp := r.FormValue("pub_type")

    if err := checkPublish(queue, routingKey, tp, message); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    var m *msg
    m, err = h.app.saveMsg(queue, routingKey, tp, message)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    q := h.app.qs.Get(queue)
    q.Push(m)

    w.Write([]byte(strconv.FormatInt(m.id, 10)))
}

ghiewa avatar May 06 '15 04:05 ghiewa

sorry @ghiewa , I have not maintained this project any longer. So I don't recommend you spending time on it.

siddontang avatar May 06 '15 04:05 siddontang

well, @siddontang , still thanks for your code, I am along with it in my experiment project.

thank you.

ghiewa avatar May 06 '15 06:05 ghiewa