This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
proxy.go
124 lines (96 loc) · 2.89 KB
/
proxy.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
package main
import "C"
import (
"errors"
"io/ioutil"
"os"
"path"
"github.com/mitchellh/go-homedir"
"github.com/planetscale/planetscale-go/planetscale"
)
const accessTokenDir string = "~/.config/planetscale"
//export startfromenv
func startfromenv(org, database, branch, listenAddr *C.char) (*C.char, *C.char) {
client, err := newClientFromEnv()
if err != nil {
return nil, nilOrError(err)
}
opts := []controllerOpt{withClient(client)}
if a := C.GoString(listenAddr); a != "" {
opts = append(opts, withListen(a))
}
return startproxy(org, database, branch, opts...)
}
//export startfromtoken
func startfromtoken(tokenName, token, org, database, branch, listenAddr *C.char) (*C.char, *C.char) {
client, err := newClientFromServiceToken(C.GoString(tokenName), C.GoString(token))
if err != nil {
return nil, nilOrError(err)
}
opts := []controllerOpt{withClient(client)}
if a := C.GoString(listenAddr); a != "" {
opts = append(opts, withListen(a))
}
return startproxy(org, database, branch, opts...)
}
//export startfromstatic
func startfromstatic(org, database, branch, privKey, cert, addr, port, listenAddr *C.char) (*C.char, *C.char) {
certSource := &localCertSource{
privKey: C.GoString(privKey),
certificate: C.GoString(cert),
remoteAddr: C.GoString(addr),
port: C.GoString(port),
}
opts := []controllerOpt{withLocalCertSource(certSource)}
if a := C.GoString(listenAddr); a != "" {
opts = append(opts, withListen(a))
}
return startproxy(org, database, branch, opts...)
}
func startproxy(org, database, branch *C.char, opts ...controllerOpt) (*C.char, *C.char) {
cntrlr, err := newController(C.GoString(org), C.GoString(database), C.GoString(branch), opts...)
if err != nil {
return nil, nilOrError(err)
}
addr, err := cntrlr.start()
return C.CString(addr), nilOrError(err)
}
func newClientFromEnv() (*planetscale.Client, error) {
opts := []planetscale.ClientOption{
planetscale.WithBaseURL(planetscale.DefaultBaseURL),
}
token, err := accessToken()
if err != nil {
return nil, err
}
opts = append(opts, planetscale.WithAccessToken(token))
return planetscale.NewClient(opts...)
}
func newClientFromServiceToken(name, token string) (*planetscale.Client, error) {
opts := []planetscale.ClientOption{
planetscale.WithBaseURL(planetscale.DefaultBaseURL),
planetscale.WithServiceToken(name, token),
}
return planetscale.NewClient(opts...)
}
func accessToken() (string, error) {
cfgDir, err := homedir.Expand(accessTokenDir)
if err != nil {
return "", err
}
tFile := path.Join(cfgDir, "access-token")
if _, err := os.Stat(tFile); err != nil {
return "", errors.New("unable to find access token file")
}
t, err := ioutil.ReadFile(tFile)
if err != nil {
return "", errors.New("unable to read access token file")
}
return string(t), nil
}
func nilOrError(err error) *C.char {
if err == nil {
return nil
}
return C.CString(err.Error())
}