forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
offline message gateway (ortuman#84)
- Loading branch information
Showing
13 changed files
with
247 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package offline | ||
|
||
import "fmt" | ||
|
||
const ( | ||
httpGatewayType = "http" | ||
) | ||
|
||
// Config represents Offline Storage module configuration. | ||
type Config struct { | ||
QueueSize int | ||
Gateway gateway | ||
} | ||
|
||
type configProxy struct { | ||
QueueSize int `yaml:"queue_size"` | ||
Gateway *struct { | ||
Type string `yaml:"type"` | ||
Auth string `yaml:"auth"` | ||
Pass string `yaml:"pass"` | ||
} `yaml:"gateway"` | ||
} | ||
|
||
// UnmarshalYAML satisfies Unmarshaler interface. | ||
func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
p := configProxy{} | ||
if err := unmarshal(&p); err != nil { | ||
return err | ||
} | ||
cfg.QueueSize = p.QueueSize | ||
if p.Gateway != nil { | ||
switch p.Gateway.Type { | ||
case httpGatewayType: | ||
cfg.Gateway = newHTTPGateway(p.Gateway.Pass, p.Gateway.Auth) | ||
default: | ||
return fmt.Errorf("unrecognized offline gateway type: %s", p.Gateway.Type) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package offline | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestOfflineConfig(t *testing.T) { | ||
badCfg := `enabled [roster]` | ||
cfg := &Config{} | ||
err := yaml.Unmarshal([]byte(badCfg), &cfg) | ||
require.NotNil(t, err) | ||
|
||
goodCfg := `queue_size: 100` | ||
cfg = &Config{} | ||
err = yaml.Unmarshal([]byte(goodCfg), &cfg) | ||
require.Nil(t, err) | ||
|
||
wrongGatewayTypeCfg := ` | ||
queue_size: 100 | ||
gateway: | ||
type: foo | ||
url: http://127.0.0.1:6666 | ||
` | ||
cfg = &Config{} | ||
err = yaml.Unmarshal([]byte(wrongGatewayTypeCfg), &cfg) | ||
require.NotNil(t, err) | ||
|
||
goodGatewayTypeCfg := ` | ||
queue_size: 100 | ||
gateway: | ||
type: http | ||
url: http://127.0.0.1:6666 | ||
` | ||
cfg = &Config{} | ||
err = yaml.Unmarshal([]byte(goodGatewayTypeCfg), &cfg) | ||
require.NotNil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package offline | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/ortuman/jackal/xmpp" | ||
"github.com/sony/gobreaker" | ||
) | ||
|
||
type gateway interface { | ||
Route(msg *xmpp.Message) error | ||
} | ||
|
||
type httpClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
type httpGateway struct { | ||
url string | ||
authToken string | ||
reqBuf *bytes.Buffer | ||
cb *gobreaker.CircuitBreaker | ||
client httpClient | ||
} | ||
|
||
func newHTTPGateway(url string, authToken string) gateway { | ||
return &httpGateway{ | ||
url: url, | ||
authToken: authToken, | ||
reqBuf: bytes.NewBuffer(nil), | ||
cb: gobreaker.NewCircuitBreaker(gobreaker.Settings{}), | ||
client: &http.Client{}, | ||
} | ||
} | ||
|
||
func (g *httpGateway) Route(msg *xmpp.Message) error { | ||
msg.ToXML(g.reqBuf, true) | ||
defer g.reqBuf.Reset() | ||
|
||
req, err := http.NewRequest(http.MethodPost, g.url, g.reqBuf) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/xml") | ||
req.Header.Set("Authorization", g.authToken) | ||
|
||
_, err = g.cb.Execute(func() (i interface{}, e error) { | ||
resp, err := g.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("response status code: %d", resp.StatusCode) | ||
} | ||
return nil, nil | ||
}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package offline | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/ortuman/jackal/xmpp" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type fakeReadCloser struct{} | ||
|
||
func (rc *fakeReadCloser) Read(p []byte) (n int, err error) { return 0, nil } | ||
func (rc *fakeReadCloser) Close() error { return nil } | ||
|
||
type fakeHTTPClient struct { | ||
do func(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func (c *fakeHTTPClient) Do(req *http.Request) (*http.Response, error) { | ||
return c.do(req) | ||
} | ||
|
||
func TestHttpGateway_Route(t *testing.T) { | ||
g := newHTTPGateway("http://127.0.0.1:6666", "a-secret-key").(*httpGateway) | ||
fakeClient := &fakeHTTPClient{} | ||
g.client = fakeClient | ||
|
||
msg := xmpp.NewMessageType(uuid.New().String(), xmpp.ChatType) | ||
body := xmpp.NewElementName("body") | ||
body.SetText("This is an offline message!") | ||
msg.AppendElement(body) | ||
|
||
var reqBody string | ||
fakeClient.do = func(req *http.Request) (response *http.Response, e error) { | ||
require.Equal(t, http.MethodPost, req.Method) | ||
require.Equal(t, "a-secret-key", req.Header.Get("Authorization")) | ||
require.Equal(t, "application/xml", req.Header.Get("Content-Type")) | ||
|
||
b, _ := ioutil.ReadAll(req.Body) | ||
reqBody = string(b) | ||
return &http.Response{StatusCode: http.StatusOK, Body: &fakeReadCloser{}}, nil | ||
} | ||
|
||
err := g.Route(msg) | ||
require.Nil(t, err) | ||
require.Equal(t, msg.String(), reqBody) | ||
|
||
fakeClient.do = func(req *http.Request) (response *http.Response, e error) { | ||
return &http.Response{StatusCode: http.StatusInternalServerError, Body: &fakeReadCloser{}}, nil | ||
} | ||
require.NotNil(t, g.Route(msg)) | ||
|
||
fakeClient.do = func(req *http.Request) (response *http.Response, e error) { | ||
return nil, errors.New("foo error") | ||
} | ||
require.NotNil(t, g.Route(msg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters