Skip to content

Commit 016fac5

Browse files
committed
chore(controller-sdk-go): remove gorilla websocket
1 parent dce01c6 commit 016fac5

10 files changed

Lines changed: 107 additions & 141 deletions

File tree

api/apps.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ type AppRunResponse struct {
3737
Output string `json:"output"`
3838
ReturnCode int `json:"exit_code"`
3939
}
40+
41+
// AppLogsRequest is the definition of websocket /v2/apps/<app id>/logs
42+
type AppLogsRequest struct {
43+
Lines int `json:"lines"`
44+
Follow bool `json:"follow"`
45+
Timeout int `json:"timeout"`
46+
}

api/ps.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func (p PodsList) Len() int { return len(p) }
2424
func (p PodsList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
2525
func (p PodsList) Less(i, j int) bool { return p[i].Name < p[j].Name }
2626

27+
// Command defines a command of app exec.
28+
type Command struct {
29+
Tty bool `json:"tty"`
30+
Stdin bool `json:"stdin"`
31+
Command []string `json:"command"`
32+
}
33+
2734
// PodType holds pods of the same type.
2835
type PodType struct {
2936
Type string

apps/apps.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
drycc "github.com/drycc/controller-sdk-go"
1212
"github.com/drycc/controller-sdk-go/api"
13-
"github.com/gorilla/websocket"
13+
"golang.org/x/net/websocket"
1414
)
1515

1616
// ErrNoLogs is returned when logs are missing from an app.
@@ -89,29 +89,31 @@ func Get(c *drycc.Client, appID string) (api.App, error) {
8989

9090
// Logs retrieves logs from an app. The number of log lines fetched can be set by the lines
9191
// argument. Setting lines = -1 will retrieve all app logs.
92-
func Logs(c *drycc.Client, appID string, lines int, follow bool, timeout int) (*websocket.Conn, error) {
92+
func Logs(c *drycc.Client, appID string, request api.AppLogsRequest) (*websocket.Conn, error) {
9393
scheme := "ws"
9494
if c.ControllerURL.Scheme == "https" {
9595
scheme = "wss"
9696
}
9797
path := fmt.Sprintf("v2/apps/%s/logs", appID)
9898
endpoint := url.URL{Scheme: scheme, Host: c.ControllerURL.Host, Path: path}
99-
conn, _, err := websocket.DefaultDialer.Dial(
100-
endpoint.String(),
101-
http.Header{
102-
"User-Agent": {c.UserAgent},
103-
"Authorization": {"token " + c.Token},
104-
"X-Drycc-Builder-Auth": {c.HooksToken},
105-
},
106-
)
99+
100+
config, err := websocket.NewConfig(endpoint.String(), c.ControllerURL.String())
101+
if err != nil {
102+
return nil, err
103+
}
104+
config.Header = http.Header{
105+
"User-Agent": {c.UserAgent},
106+
"Authorization": {"token " + c.Token},
107+
"X-Drycc-Builder-Auth": {c.HooksToken},
108+
}
109+
conn, err := websocket.DialConfig(config)
110+
if err != nil {
111+
return nil, err
112+
}
113+
err = websocket.JSON.Send(conn, request)
107114
if err != nil {
108115
return nil, err
109116
}
110-
111-
conn.WriteMessage(
112-
websocket.TextMessage,
113-
[]byte(fmt.Sprintf(`{"lines": %d, "timeout": %d, "follow": %v}`, lines, timeout, follow)),
114-
)
115117
return conn, nil
116118
}
117119

apps/apps_test.go

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package apps
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io"
76
"log"
87
"net/http"
98
"net/http/httptest"
109
"reflect"
10+
"sync"
1111
"testing"
1212

1313
drycc "github.com/drycc/controller-sdk-go"
1414
"github.com/drycc/controller-sdk-go/api"
15-
"github.com/gorilla/websocket"
15+
"golang.org/x/net/websocket"
1616
)
1717

1818
const appFixture string = `
@@ -46,19 +46,11 @@ const appCreateExpected string = `{"id":"example-go"}`
4646
const appRunExpected string = `{"command":"echo hi"}`
4747
const appTransferExpected string = `{"owner":"test"}`
4848

49-
var upgrader = websocket.Upgrader{} // use default options
50-
5149
type fakeHTTPServer struct {
5250
createID bool
5351
createWithoutID bool
5452
}
5553

56-
type fakeLogReqMessage struct {
57-
Lines int `json:"lines"`
58-
Timeout int `json:"timeout"`
59-
Follow bool `json:"follow"`
60-
}
61-
6254
func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6355
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
6456

@@ -105,28 +97,6 @@ func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
10597
return
10698
}
10799

108-
// The entire log message is prefixed and suffixed with a few characters (not entirely sure why)
109-
// We mimic those here
110-
if req.URL.Path == "/v2/apps/example-go/logs" {
111-
c, err := upgrader.Upgrade(res, req, nil)
112-
if err != nil {
113-
log.Print("upgrade:", err)
114-
return
115-
}
116-
defer c.Close()
117-
_, message, err := c.ReadMessage()
118-
if err != nil {
119-
log.Print("upgrade:", err)
120-
return
121-
}
122-
reqJSON := fakeLogReqMessage{}
123-
json.Unmarshal([]byte(message), &reqJSON)
124-
for i := 0; i < reqJSON.Lines; i++ {
125-
c.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("test %d", i)))
126-
}
127-
return
128-
}
129-
130100
if req.URL.Path == "/v2/apps/example-go/run" && req.Method == "POST" {
131101
body, err := io.ReadAll(req.Body)
132102

@@ -317,26 +287,39 @@ func TestAppsList(t *testing.T) {
317287

318288
func TestAppsLogs(t *testing.T) {
319289
t.Parallel()
320-
321-
handler := fakeHTTPServer{}
322-
server := httptest.NewServer(&handler)
323-
defer server.Close()
324-
325-
drycc, err := drycc.New(false, server.URL, "abc")
290+
var once sync.Once
291+
var addr string
292+
once.Do(func() {
293+
http.Handle(
294+
"/v2/apps/example-go/logs",
295+
websocket.Handler(func(conn *websocket.Conn) {
296+
io.Copy(conn, conn)
297+
}),
298+
)
299+
server := httptest.NewServer(nil)
300+
addr = server.Listener.Addr().String()
301+
log.Print("Test WebSocket server listening on ", addr)
302+
})
303+
drycc, err := drycc.New(false, addr, "abc")
326304
if err != nil {
327305
t.Fatal(err)
328306
}
329-
330-
conn, err := Logs(drycc, "example-go", 1, false, 300)
307+
expected := api.AppLogsRequest{
308+
Lines: 1,
309+
Follow: false,
310+
Timeout: 300,
311+
}
312+
conn, err := Logs(drycc, "example-go", expected)
331313
if err != nil {
332314
t.Fatal(err)
333315
}
334-
_, message, err := conn.ReadMessage()
316+
var actual api.AppLogsRequest
317+
err = websocket.JSON.Receive(conn, &actual)
335318
if err != nil {
336319
t.Fatal(err)
337320
}
338-
if string(message) != "test 0" {
339-
t.Errorf("Expected %s, Got %s", "test 0", message)
321+
if !reflect.DeepEqual(actual, expected) {
322+
t.Errorf("Expected: %v, Got %v", expected, actual)
340323
}
341324
}
342325

drycc.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ import (
5555
"fmt"
5656
"net/http"
5757
"net/url"
58-
59-
"github.com/goware/urlx"
58+
"strings"
6059
)
6160

6261
// Client oversees the interaction between the drycc and controller
@@ -117,9 +116,11 @@ func IsErrAPIMismatch(err error) bool {
117116
// verifySSL determines whether or not to verify SSL connections.
118117
// This should be true unless you know the controller is using untrusted SSL keys.
119118
func New(verifySSL bool, controllerURL string, token string) (*Client, error) {
120-
// urlx, unlike the native url library, uses sane defaults when URL parsing,
121119
// preventing issues like missing schemes.
122-
u, err := urlx.Parse(controllerURL)
120+
if !strings.HasPrefix(controllerURL, "http://") && !strings.HasPrefix(controllerURL, "https://") {
121+
controllerURL = "http://" + controllerURL
122+
}
123+
u, err := url.Parse(controllerURL)
123124
if err != nil {
124125
return nil, err
125126
}

errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func (m *mockReadCloser) Read(msg []byte) (int, error) {
3131

3232
copy(msg, []byte(m.msg))
3333

34-
len := len(m.msg)
34+
size := len(m.msg)
3535
m.msg = ""
36-
return len, nil
36+
return size, nil
3737
}
3838

3939
func readCloser(msg string) *mockReadCloser {

go.mod

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,4 @@ module github.com/drycc/controller-sdk-go
22

33
go 1.20
44

5-
require (
6-
github.com/gorilla/websocket v1.5.0
7-
github.com/goware/urlx v0.3.2
8-
)
9-
10-
require (
11-
github.com/PuerkitoBio/purell v1.1.1 // indirect
12-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
13-
golang.org/x/net v0.7.0 // indirect
14-
golang.org/x/text v0.7.0 // indirect
15-
)
5+
require golang.org/x/net v0.12.0

go.sum

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
2-
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
3-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
4-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
5-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
6-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
7-
github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo=
8-
github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw=
9-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
10-
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
11-
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
12-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
13-
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
14-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
1+
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
2+
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=

ps/ps.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
drycc "github.com/drycc/controller-sdk-go"
1212
"github.com/drycc/controller-sdk-go/api"
13-
"github.com/gorilla/websocket"
13+
"golang.org/x/net/websocket"
1414
)
1515

1616
// List lists an app's processes.
@@ -30,32 +30,27 @@ func List(c *drycc.Client, appID string, results int) (api.PodsList, int, error)
3030
}
3131

3232
// Exec a command in a container.
33-
func Exec(c *drycc.Client, app, pod string, tty, stdin bool, command []string) (*websocket.Conn, error) {
33+
func Exec(c *drycc.Client, app, pod string, command api.Command) (*websocket.Conn, error) {
3434
scheme := "ws"
3535
if c.ControllerURL.Scheme == "https" {
3636
scheme = "wss"
3737
}
3838
path := fmt.Sprintf("v2/apps/%s/pods/%s/exec/", app, pod)
39-
endpoint := url.URL{Scheme: scheme, Host: c.ControllerURL.Host, Path: path}
40-
conn, _, err := websocket.DefaultDialer.Dial(
41-
endpoint.String(),
42-
http.Header{
43-
"User-Agent": {c.UserAgent},
44-
"Authorization": {"token " + c.Token},
45-
"X-Drycc-Builder-Auth": {c.HooksToken},
46-
},
47-
)
39+
u := url.URL{Scheme: scheme, Host: c.ControllerURL.Host, Path: path}
40+
config, err := websocket.NewConfig(u.String(), c.ControllerURL.String())
4841
if err != nil {
4942
return nil, err
5043
}
51-
data, err := json.Marshal(&command)
44+
config.Header = http.Header{
45+
"User-Agent": {c.UserAgent},
46+
"Authorization": {"token " + c.Token},
47+
"X-Drycc-Builder-Auth": {c.HooksToken},
48+
}
49+
conn, err := websocket.DialConfig(config)
5250
if err != nil {
5351
return nil, err
5452
}
55-
conn.WriteMessage(
56-
websocket.TextMessage,
57-
[]byte(fmt.Sprintf(`{"tty": %t, "stdin": %t, "command": %s}`, tty, stdin, data)),
58-
)
53+
websocket.JSON.Send(conn, command)
5954
return conn, nil
6055
}
6156

0 commit comments

Comments
 (0)