Skip to content

Commit

Permalink
feat: websocket support in webhooks
Browse files Browse the repository at this point in the history
- websocket URLs are now supported in addition to existing HTTP webhook
URLs.
- Part of hyperledger-archives#1323

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Feb 26, 2020
1 parent 1aef12b commit 591c758
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 86 deletions.
5 changes: 5 additions & 0 deletions cmd/aries-agent-rest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/tink v1.3.0-rc4 h1:zALbM4opXSOEf/ykXWMXgRwDPCtJz/KB4lVXHqPeFsA=
Expand All @@ -86,6 +87,7 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.10.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -215,13 +217,15 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down Expand Up @@ -250,5 +254,6 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
nhooyr.io/websocket v1.7.4 h1:w/LGB2sZT0RV8lZYR7nfyaYz4PUbYZ5oF7NBon2M0NY=
nhooyr.io/websocket v1.7.4/go.mod h1:PxYxCwFdFYQ0yRvtQz3s/dC+VEm7CSuC/4b9t8MQQxw=
nhooyr.io/websocket v1.8.3/go.mod h1:LiqdCg1Cu7TPWxEvPjPa0TGYxCsy4pHNTN9gGluwBpQ=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
github.com/btcsuite/btcutil v1.0.1
github.com/golang/mock v1.4.0
github.com/golang/protobuf v1.3.3 // indirect
github.com/google/tink v1.3.0-rc4
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.3
Expand All @@ -27,8 +26,9 @@ require (
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 // indirect
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
gopkg.in/yaml.v2 v2.2.8 // indirect
nhooyr.io/websocket v1.7.4
nhooyr.io/websocket v1.8.3
)

go 1.13
27 changes: 10 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/tink v1.3.0-rc4 h1:zALbM4opXSOEf/ykXWMXgRwDPCtJz/KB4lVXHqPeFsA=
Expand All @@ -82,11 +82,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/klauspost/compress v1.10.0 h1:92XGj1AcYzA6UrVdd4qIIBrT8OroryvRvdmg/IfmC7Y=
github.com/klauspost/compress v1.10.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
Expand Down Expand Up @@ -141,10 +138,6 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -203,8 +196,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand All @@ -213,6 +206,8 @@ golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.14.0 h1:uMf5uLi4eQMRrMKhCplNik4U4H8Z6C1br3zOtAa/aDE=
google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI=
Expand All @@ -229,8 +224,6 @@ google.golang.org/grpc v1.20.1 h1:Hz2g2wirWK7H0qIIhGIqRGTuMwTE8HEKFnDZZ7lm9NU=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
Expand All @@ -244,7 +237,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
nhooyr.io/websocket v1.7.4 h1:w/LGB2sZT0RV8lZYR7nfyaYz4PUbYZ5oF7NBon2M0NY=
nhooyr.io/websocket v1.7.4/go.mod h1:PxYxCwFdFYQ0yRvtQz3s/dC+VEm7CSuC/4b9t8MQQxw=
nhooyr.io/websocket v1.8.3 h1:5UCql+eGVUYcBdr+IvngX2w1xq7g7snC9lSjbfi9qMY=
nhooyr.io/websocket v1.8.3/go.mod h1:LiqdCg1Cu7TPWxEvPjPa0TGYxCsy4pHNTN9gGluwBpQ=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
12 changes: 6 additions & 6 deletions pkg/controller/command/didexchange/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (

func TestNew(t *testing.T) {
t.Run("Successfully create new DID exchange command", func(t *testing.T) {
cmd, err := New(mockProvider(), webhook.NewHTTPNotifier(nil), "", false)
cmd, err := New(mockProvider(), webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
require.NotNil(t, cmd)

Expand All @@ -53,14 +53,14 @@ func TestNew(t *testing.T) {
})

t.Run("Successfully create new DID exchange command with auto accept", func(t *testing.T) {
cmd, err := New(mockProvider(), webhook.NewHTTPNotifier(nil), "", true)
cmd, err := New(mockProvider(), webhook.NewWebNotifier(nil), "", true)
require.NoError(t, err)
require.NotNil(t, cmd)
})

t.Run("Test create new DID exchange command failure", func(t *testing.T) {
cmd, err := New(&mockprovider.Provider{ServiceErr: errors.New("test-error")},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.Error(t, err)
require.Nil(t, cmd)
})
Expand Down Expand Up @@ -848,7 +848,7 @@ func TestHandleMessageEvent(t *testing.T) {
didexsvc.DIDExchange: &mockdidexchange.MockDIDExchangeSvc{},
route.Coordination: &mockroute.MockRouteSvc{},
}},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
require.NotNil(t, op)

Expand Down Expand Up @@ -900,7 +900,7 @@ func TestSendConnectionNotification(t *testing.T) {
didexsvc.DIDExchange: &mockdidexchange.MockDIDExchangeSvc{},
route.Coordination: &mockroute.MockRouteSvc{},
}},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
err = op.sendConnectionNotification(connID, "completed")
require.NoError(t, err)
Expand All @@ -914,7 +914,7 @@ func TestSendConnectionNotification(t *testing.T) {
didexsvc.DIDExchange: &mockdidexchange.MockDIDExchangeSvc{},
route.Coordination: &mockroute.MockRouteSvc{},
}},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
err = op.sendConnectionNotification("id2", "")
require.Error(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func GetRESTHandlers(ctx *context.Provider, opts ...Opt) ([]rest.Handler, error)

notifier := restAPIOpts.notifier
if notifier == nil {
notifier = webhook.NewHTTPNotifier(restAPIOpts.webhookURLs)
notifier = webhook.NewWebNotifier(restAPIOpts.webhookURLs)
}

// DID Exchange REST operation
Expand Down Expand Up @@ -133,7 +133,7 @@ func GetCommandHandlers(ctx *context.Provider, opts ...Opt) ([]command.Handler,

notifier := cmdOpts.notifier
if notifier == nil {
notifier = webhook.NewHTTPNotifier(cmdOpts.webhookURLs)
notifier = webhook.NewWebNotifier(cmdOpts.webhookURLs)
}

// did exchange command operation
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/rest/didexchange/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestOperation_GetAPIHandlers(t *testing.T) {
didexsvc.DIDExchange: &mockdidexchange.MockDIDExchangeSvc{},
route.Coordination: &mockroute.MockRouteSvc{},
}},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
require.NotNil(t, svc)

Expand All @@ -54,7 +54,7 @@ func TestOperation_GetAPIHandlers(t *testing.T) {

func TestNew_Fail(t *testing.T) {
svc, err := New(&mockprovider.Provider{ServiceErr: errors.New("test-error")},
webhook.NewHTTPNotifier(nil), "", false)
webhook.NewWebNotifier(nil), "", false)
require.Error(t, err)
require.Nil(t, svc)
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestEmptyID(t *testing.T) {
ServiceEndpointValue: "endppint",
}

op, err := New(prov, webhook.NewHTTPNotifier(nil), "", false)
op, err := New(prov, webhook.NewWebNotifier(nil), "", false)
require.NoError(t, err)
require.NotNil(t, op)

Expand Down Expand Up @@ -480,7 +480,7 @@ func getHandlerWithError(t *testing.T, lookup string, f *fails) rest.Handler {
ServiceEndpointValue: "endpoint",
TransientStorageProviderValue: &mockstore.MockStoreProvider{Store: &transientStore},
StorageProviderValue: &mockstore.MockStoreProvider{Store: &store}},
webhook.NewHTTPNotifier(nil),
webhook.NewWebNotifier(nil),
"", true,
)
require.NoError(t, err)
Expand Down
76 changes: 65 additions & 11 deletions pkg/controller/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"nhooyr.io/websocket"

"github.com/hyperledger/aries-framework-go/pkg/common/log"
)

Expand All @@ -30,20 +33,31 @@ type Notifier interface {
Notify(topic string, message []byte) error
}

// HTTPNotifier is a webhook dispatcher capable of notifying multiple subscribers via HTTP.
type HTTPNotifier struct {
WebhookURLs []string
// WebNotifier is a webhook dispatcher capable of notifying multiple subscribers via HTTP.
type WebNotifier struct {
httpURLs []string
wsURLS []string
}

// NewHTTPNotifier returns a new instance of an HTTPNotifier.
func NewHTTPNotifier(webhookURLs []string) HTTPNotifier {
return HTTPNotifier{WebhookURLs: webhookURLs}
// NewWebNotifier returns a new instance of an WebNotifier.
func NewWebNotifier(webhookURLs []string) WebNotifier {
var httpURLS, wsURLS []string

for _, url := range webhookURLs {
if strings.HasPrefix(url, "ws://") || strings.HasPrefix(url, "wss://") {
wsURLS = append(wsURLS, url)
} else {
httpURLS = append(httpURLS, url)
}
}

return WebNotifier{httpURLs: httpURLS, wsURLS: wsURLS}
}

// Notify sends the given message to all of the WebhookURLs.
// Notify sends the given message to all of the URLs.
// Topic is appended to the end of the webhook (subscriber) URL. E.g. localhost:8080/topic
// If multiple errors are encountered, then the first one is returned.
func (n HTTPNotifier) Notify(topic string, message []byte) error {
func (n WebNotifier) Notify(topic string, message []byte) error {
if topic == "" {
return fmt.Errorf(emptyTopicErrMsg)
}
Expand All @@ -54,15 +68,20 @@ func (n HTTPNotifier) Notify(topic string, message []byte) error {

var allErrs error

for _, webhookURL := range n.WebhookURLs {
err := notify(fmt.Sprintf("%s%s%s", webhookURL, "/", topic), message)
for _, url := range n.httpURLs {
err := notifyHTTP(fmt.Sprintf("%s/%s", url, topic), message)
allErrs = appendError(allErrs, err)
}

for _, url := range n.wsURLS {
err := notifyWS(fmt.Sprintf("%s/%s", url, topic), message)
allErrs = appendError(allErrs, err)
}

return allErrs
}

func notify(destination string, message []byte) error {
func notifyHTTP(destination string, message []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), notificationSendTimeout)
defer cancel()

Expand All @@ -89,6 +108,41 @@ func notify(destination string, message []byte) error {
destination, resp.Status)
}

func notifyWS(destination string, message []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), notificationSendTimeout)
defer cancel()

c, rs, err := websocket.Dial(ctx, destination, nil)
if err != nil {
return err
}

defer func() {
err = c.Close(websocket.StatusNormalClosure, "closing connection")
if err != nil {
logger.Errorf("failed close websocket connection : %s", err)
}

if rs != nil && rs.Body != nil {
closeResponse(rs.Body)
}
}()

w, err := c.Writer(ctx, websocket.MessageText)
if err != nil {
return err
}

_, err = w.Write(message)
if err != nil {
return err
}

defer closeResponse(w)

return nil
}

func closeResponse(c io.Closer) {
err := c.Close()
if err != nil {
Expand Down

0 comments on commit 591c758

Please sign in to comment.