mirror of
https://git.mills.io/prologic/msgbus.git
synced 2024-06-16 11:59:03 +00:00
Improve API to be more RESTful
This commit is contained in:
parent
9d131ac06f
commit
c640130f11
@ -38,13 +38,6 @@ const (
|
|||||||
pingPeriod = (pongWait * 9) / 10
|
pingPeriod = (pongWait * 9) / 10
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// PublishedRegexp ...
|
|
||||||
PublishedRegexp = regexp.MustCompile(
|
|
||||||
"message successfully published to \\w+ with sequence \\d",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client ...
|
// Client ...
|
||||||
type Client struct {
|
type Client struct {
|
||||||
url string
|
url string
|
||||||
@ -162,17 +155,8 @@ func (c *Client) Publish(topic, message string) error {
|
|||||||
return fmt.Errorf("error publishing message: %s", err)
|
return fmt.Errorf("error publishing message: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 201 {
|
||||||
return fmt.Errorf("unexpected non-200 response: %s", res.Status)
|
return fmt.Errorf("unexpected response: %s", res.Status)
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading response: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !PublishedRegexp.Match(body) {
|
|
||||||
return fmt.Errorf("unexpected non-matching response: %s", body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
11
msgbus.go
11
msgbus.go
@ -497,6 +497,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(out)
|
w.Write(out)
|
||||||
return
|
return
|
||||||
@ -527,14 +528,9 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
message := mb.NewMessage(t, body)
|
mb.Put(mb.NewMessage(t, body))
|
||||||
mb.Put(message)
|
|
||||||
|
|
||||||
msg := fmt.Sprintf(
|
w.WriteHeader(http.StatusAccepted)
|
||||||
"message successfully published to %s with sequence %d",
|
|
||||||
t.Name, message.ID,
|
|
||||||
)
|
|
||||||
w.Write([]byte(msg))
|
|
||||||
case "GET":
|
case "GET":
|
||||||
if r.Header.Get("Upgrade") == "websocket" {
|
if r.Header.Get("Upgrade") == "websocket" {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
@ -562,6 +558,7 @@ func (mb *MessageBus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(out)
|
w.Write(out)
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@ -74,8 +73,7 @@ func TestServeHTTPPOST(t *testing.T) {
|
|||||||
r, _ := http.NewRequest("POST", "/hello", b)
|
r, _ := http.NewRequest("POST", "/hello", b)
|
||||||
|
|
||||||
mb.ServeHTTP(w, r)
|
mb.ServeHTTP(w, r)
|
||||||
assert.Equal(w.Code, http.StatusOK)
|
assert.Equal(w.Code, http.StatusAccepted)
|
||||||
assert.Regexp(`message successfully published to hello with sequence \d+`, w.Body.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServeHTTPMaxPayloadSize(t *testing.T) {
|
func TestServeHTTPMaxPayloadSize(t *testing.T) {
|
||||||
@ -101,8 +99,7 @@ func TestServeHTTPSimple(t *testing.T) {
|
|||||||
r, _ := http.NewRequest("POST", "/hello", b)
|
r, _ := http.NewRequest("POST", "/hello", b)
|
||||||
|
|
||||||
mb.ServeHTTP(w, r)
|
mb.ServeHTTP(w, r)
|
||||||
assert.Equal(w.Code, http.StatusOK)
|
assert.Equal(w.Code, http.StatusAccepted)
|
||||||
assert.Regexp(`message successfully published to hello with sequence \d+`, w.Body.String())
|
|
||||||
|
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
r, _ = http.NewRequest("GET", "/hello", nil)
|
r, _ = http.NewRequest("GET", "/hello", nil)
|
||||||
@ -167,9 +164,6 @@ func TestServeHTTPSubscriber(t *testing.T) {
|
|||||||
r, err := c.Post(s.URL+"/hello", "text/plain", b)
|
r, err := c.Post(s.URL+"/hello", "text/plain", b)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Regexp(`message successfully published to hello with sequence \d+`, string(body))
|
|
||||||
|
|
||||||
msg := <-msgs
|
msg := <-msgs
|
||||||
assert.Equal(msg.ID, uint64(0))
|
assert.Equal(msg.ID, uint64(0))
|
||||||
|
Loading…
Reference in New Issue
Block a user