-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathsdk.go
143 lines (112 loc) · 2.58 KB
/
sdk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sdk
import (
"context"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/convox/rack/pkg/structs"
"github.com/convox/stdsdk"
)
const (
sortableTime = "20060102.150405.000000000"
statusCodePrefix = "F1E49A85-0AD7-4AEF-A618-C249C6E6568D:"
)
var (
Version = "dev"
)
type AuthenticationError interface {
AuthenticationError() error
}
type Client struct {
*stdsdk.Client
Debug bool
Rack string
Session SessionFunc
}
type SessionFunc func(c *Client) string
// ensure interface parity
var _ structs.Provider = &Client{}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func New(endpoint string) (*Client, error) {
s, err := stdsdk.New(coalesce(endpoint, "https://rack.convox"))
if err != nil {
return nil, err
}
c := &Client{
Client: s,
Debug: os.Getenv("CONVOX_DEBUG") == "true",
}
c.Client.Headers = c.Headers
return c, nil
}
func NewFromEnv() (*Client, error) {
return New(os.Getenv("RACK_URL"))
}
func (c *Client) Headers() http.Header {
h := http.Header{}
h.Set("User-Agent", fmt.Sprintf("convox.go/%s", Version))
h.Set("Version", Version)
if c.Endpoint.User != nil {
h.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s", c.Endpoint.User)))))
}
if c.Rack != "" {
h.Set("Rack", c.Rack)
}
if c.Session != nil {
h.Set("Session", c.Session(c))
}
return h
}
func (c *Client) Websocket(path string, opts stdsdk.RequestOptions) (io.ReadCloser, error) {
// trigger session authentication
if err := c.Get("/racks", stdsdk.RequestOptions{}, nil); err != nil {
if _, ok := err.(AuthenticationError); ok {
return nil, err
}
}
return c.Client.Websocket(path, opts)
}
func (c *Client) WebsocketExit(path string, ro stdsdk.RequestOptions, rw io.ReadWriter) (int, error) {
ws, err := c.Websocket(path, ro)
if err != nil {
return 0, err
}
buf := make([]byte, 10*1024)
code := 0
for {
n, err := ws.Read(buf)
if err == io.EOF {
return code, nil
}
if err != nil {
return code, err
}
if i := strings.Index(string(buf[0:n]), statusCodePrefix); i > -1 {
if _, err := rw.Write(buf[0:i]); err != nil {
return 0, err
}
m := i + len(statusCodePrefix)
code, err = strconv.Atoi(strings.TrimSpace(string(buf[m:n])))
if err != nil {
return 0, fmt.Errorf("unable to read exit code")
}
continue
}
if _, err := rw.Write(buf[0:n]); err != nil {
return 0, err
}
}
}
func (c *Client) WithContext(ctx context.Context) structs.Provider {
cc := *c
cc.Client = cc.Client.WithContext(ctx)
return &cc
}