Skip to content

Commit

Permalink
Merge pull request #23 from xmidt-org/add-ws
Browse files Browse the repository at this point in the history
Add websocket support library.
  • Loading branch information
schmidtw authored Dec 30, 2023
2 parents 89412f4 + 7570113 commit 696379a
Show file tree
Hide file tree
Showing 14 changed files with 1,682 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ require (
github.com/ugorji/go/codec v1.2.12
github.com/xmidt-org/arrange v0.5.0
github.com/xmidt-org/eventor v0.0.0-20230910205925-8ff168bd12ed
github.com/xmidt-org/retry v0.0.3
github.com/xmidt-org/sallust v0.2.2
github.com/xmidt-org/wrp-go/v3 v3.2.3
go.uber.org/fx v1.20.1
go.uber.org/zap v1.26.0
gopkg.in/dealancer/validate.v2 v2.1.0
nhooyr.io/websocket v1.8.10
)

require (
Expand All @@ -28,6 +30,7 @@ require (
github.com/miekg/dns v1.1.56 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/xmidt-org/httpaux v0.4.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/xmidt-org/eventor v0.0.0-20230910205925-8ff168bd12ed h1:KpcgFuumKrt/8
github.com/xmidt-org/eventor v0.0.0-20230910205925-8ff168bd12ed/go.mod h1:X9Og+8y1Llz7N8F20UmjZUNgrxHubMVfBcroJ5SPtIY=
github.com/xmidt-org/httpaux v0.4.0 h1:cAL/MzIBpSsv4xZZeq/Eu1J5M3vfNe49xr41mP3COKU=
github.com/xmidt-org/httpaux v0.4.0/go.mod h1:UypqZwuZV1nn8D6+K1JDb+im9IZrLNg/2oO/Bgiybxc=
github.com/xmidt-org/retry v0.0.3 h1:wvmBnEEn1OKwSZaQtr1RZ2Vey8JIvP72mGTgR+3wPiM=
github.com/xmidt-org/retry v0.0.3/go.mod h1:I7FO3VVrxPckNuotwGYZIxfBnmjMSyOTitTKNL0VkIA=
github.com/xmidt-org/sallust v0.2.2 h1:MrINLEr7cMj6ENx/O76fvpfd5LNGYnk7OipZAGXPYA0=
github.com/xmidt-org/sallust v0.2.2/go.mod h1:ytBoypcPw10OmjM6b92Jx3eoqWX4J5zVXOQozGwz4qs=
github.com/xmidt-org/wrp-go/v3 v3.2.3 h1:Qmcsnz8QQZ2ZYVLvhyuTH11ixySes1Lm30XNdGDTpn8=
Expand Down Expand Up @@ -115,3 +117,5 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYs
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
12 changes: 6 additions & 6 deletions internal/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,21 @@ func (c *Credentials) Credentials() (string, time.Time, error) {
return c.token.Token, c.token.ExpiresAt, nil
}

// Decorate decorates the request with the credentials. If the credentials
// Decorate decorates the headers with the credentials. If the credentials
// are not valid, an error is returned.
func (c *Credentials) Decorate(req *http.Request) error {
err := c.decorate(req)
func (c *Credentials) Decorate(headers http.Header) error {
err := c.decorate(headers)
if c.required && err != nil {
return err
}

return nil
}

func (c *Credentials) decorate(req *http.Request) error {
func (c *Credentials) decorate(headers http.Header) error {
var e event.Decorate

if req == nil {
if headers == nil {
e.Err = ErrNilRequest
return c.dispatch(e)
}
Expand All @@ -238,7 +238,7 @@ func (c *Credentials) decorate(req *http.Request) error {
return c.dispatch(e)
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
headers.Set("Authorization", fmt.Sprintf("Bearer %s", token))

return c.dispatch(e)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ func TestEndToEnd(t *testing.T) {
assert.NoError(err)
assert.NotNil(req)

err = c.Decorate(req)
err = c.Decorate(req.Header)
assert.NoError(err)
assert.Equal("Bearer token", strings.TrimSpace(req.Header.Get("Authorization")))

// Decorate the a second time.
_ = c.Decorate(req)
_ = c.Decorate(req.Header)

c.Stop()

Expand Down Expand Up @@ -554,7 +554,7 @@ func TestDecorate(t *testing.T) {
assert.ErrorIs(err, ErrNilRequest)

req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
err = c.Decorate(req)
err = c.Decorate(req.Header)
assert.ErrorIs(err, ErrNoToken)

assert.Equal(2, count)
Expand Down
85 changes: 85 additions & 0 deletions internal/websocket/cmd/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"os"
"time"

"github.com/alecthomas/kong"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/xmidt-agent/internal/websocket"
"github.com/xmidt-org/xmidt-agent/internal/websocket/event"
)

// CLI is the structure that is used to capture the command line arguments.
type CLI struct {
Id string `optional:"" default:"mac:112233445566" help:"The id of the device."`
URL string `optional:"" default:"https://fabric.example.com/api/v2/device" help:"The URL for the WS connection."`
V4 bool `optional:"" short:"4" name:"4" xor:"ipmode" help:"Only use IPv4"`
V6 bool `optional:"" short:"6" name:"6" xor:"ipmode" help:"Only use IPv6"`
Once bool `optional:"" help:"Only attempt to connect once."`
}

func main() {
var cli CLI

parser, err := kong.New(&cli,
kong.Name("example"),
kong.Description("The test agent for websocket service.\n"),
kong.UsageOnError(),
)
if err != nil {
panic(err)
}

_, err = parser.Parse(os.Args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}

id, err := wrp.ParseDeviceID(cli.Id)
if err != nil {
panic(err)
}

opts := []websocket.Option{
websocket.DeviceID(id),
websocket.URL(cli.URL),
websocket.AddConnectListener(
event.ConnectListenerFunc(
func(e event.Connect) {
fmt.Println(e)
})),
websocket.AddDisconnectListener(
event.DisconnectListenerFunc(
func(e event.Disconnect) {
fmt.Println(e)
})),
}

if cli.V4 {
opts = append(opts, websocket.WithIPv6(false))
}

if cli.V6 {
opts = append(opts, websocket.WithIPv4(false))
}

if cli.Once {
opts = append(opts, websocket.Once())
}

ws, err := websocket.New(opts...)
if err != nil {
panic(err)
}

ws.Start()
defer ws.Stop()

time.Sleep(time.Minute)
}
Loading

0 comments on commit 696379a

Please sign in to comment.