Skip to content

Commit 5ea0d80

Browse files
authored
Revert "Glauber/region" (#979)
Reverts #970
2 parents 189f917 + 80feb01 commit 5ea0d80

File tree

6 files changed

+41
-55
lines changed

6 files changed

+41
-55
lines changed

internal/cmd/db.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ func closestLocation(client *turso.Client) (string, error) {
182182
return closest, nil
183183
}
184184

185-
settings, err := settings.ReadSettings()
186-
if err != nil {
187-
return "", fmt.Errorf("could not read settings file to probe for locations: %w", err)
188-
}
189-
190-
regionUrl := settings.GetRegionURL()
191-
closest, err := client.Locations.Closest(regionUrl)
185+
closest, err := client.Locations.Closest()
192186
if err != nil {
193187
// We fallback to ams if we are unable to probe the closest location.
194188
return "ams", err

internal/cmd/db_shell.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ import (
2020
"github.com/tursodatabase/turso-cli/internal/turso"
2121
)
2222

23+
var proxy string
24+
2325
func init() {
2426
dbCmd.AddCommand(shellCmd)
2527
addInstanceFlag(shellCmd, "Connect to the database at the specified instance.")
2628
addLocationFlag(shellCmd, "Connect to the database at the specified location.")
29+
shellCmd.Flags().StringVar(&proxy, "proxy", "", "Proxy to use for the connection.")
30+
shellCmd.RegisterFlagCompletionFunc("proxy", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
return []string{}, cobra.ShellCompDirectiveNoFileComp
32+
})
2733
flags.AddAttachClaims(shellCmd)
2834
}
2935

@@ -216,7 +222,7 @@ var shellCmd = &cobra.Command{
216222

217223
shellConfig := shell.ShellConfig{
218224
DbUri: dbUrl,
219-
Proxy: getTursoProxyUrl(),
225+
Proxy: proxy,
220226
AuthToken: authToken,
221227
InF: cmd.InOrStdin(),
222228
OutF: cmd.OutOrStdout(),

internal/cmd/utils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,6 @@ func getTursoUrl() string {
261261
return url
262262
}
263263

264-
func getTursoProxyUrl() string {
265-
config, _ := settings.ReadSettings() // ok to ignore, we'll fallback to default
266-
return config.GetProxyURL()
267-
}
268-
269264
func promptConfirmation(prompt string) (bool, error) {
270265
reader := bufio.NewReader(os.Stdin)
271266

internal/settings/settings.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package settings
22

33
import (
44
"fmt"
5-
"net/url"
65
"os"
76
"path"
87
"path/filepath"
@@ -35,7 +34,6 @@ func ReadSettings() (*Settings, error) {
3534
configPath := configdir.LocalConfig("turso")
3635
viper.BindEnv("config-path", "TURSO_CONFIG_FOLDER")
3736
viper.BindEnv("baseURL", "TURSO_API_BASEURL")
38-
viper.BindEnv("proxyURL", "TURSO_PROXY_URL")
3937

4038
configPathFlag := viper.GetString("config-path")
4139
if len(configPathFlag) > 0 {
@@ -145,21 +143,6 @@ func (s *Settings) GetBaseURL() string {
145143
return viper.GetString("baseURL")
146144
}
147145

148-
func (s *Settings) GetProxyURL() string {
149-
return viper.GetString("proxyURL")
150-
}
151-
152-
func (s *Settings) GetRegionURL() string {
153-
baseURL := s.GetProxyURL()
154-
parsedURL, err := url.Parse(baseURL)
155-
if err != nil {
156-
return "https://region.turso.io"
157-
}
158-
159-
parsedURL.Host = "region." + parsedURL.Host
160-
return parsedURL.String()
161-
}
162-
163146
func (s *Settings) SetAutoupdate(autoupdate string) {
164147
config := viper.GetStringMap("config")
165148
if config == nil {

internal/turso/locations.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"regexp"
7+
"time"
78
"unicode/utf8"
89

910
"github.com/rodaine/table"
@@ -52,8 +53,8 @@ type ClosestLocationResponse struct {
5253
Server string
5354
}
5455

55-
func (c *LocationsClient) Closest(regionUrl string) (string, error) {
56-
r, err := c.client.Get(regionUrl, nil)
56+
func (c *LocationsClient) Closest() (string, error) {
57+
r, err := c.client.Get("https://region.turso.io", nil)
5758
if err != nil {
5859
return "", fmt.Errorf("failed to request closest: %s", err)
5960
}
@@ -72,6 +73,35 @@ func (c *LocationsClient) Closest(regionUrl string) (string, error) {
7273
return data.Server, nil
7374
}
7475

76+
func ProbeLocation(location string) *time.Duration {
77+
client := &http.Client{Timeout: 2 * time.Second}
78+
req, err := http.NewRequest("GET", "http://region.turso.io:8080/", nil)
79+
if err != nil {
80+
return nil
81+
}
82+
req.Header.Add("fly-prefer-region", location)
83+
84+
start := time.Now()
85+
r, err := client.Do(req)
86+
if err != nil {
87+
return nil
88+
}
89+
defer r.Body.Close()
90+
91+
dur := time.Since(start)
92+
if r.StatusCode != http.StatusOK {
93+
return nil
94+
}
95+
data, err := unmarshal[ClosestLocationResponse](r)
96+
if err != nil {
97+
return nil
98+
}
99+
if data.Server != location {
100+
return nil
101+
}
102+
return &dur
103+
}
104+
75105
func LocationsTable(columns []interface{}) table.Table {
76106
regex := regexp.MustCompile(`\x1b\[[0-9;]*m`)
77107
return table.New(columns...).WithWidthFunc(func(s string) int {

internal/turso/turso.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package turso
22

33
import (
4-
"context"
54
"fmt"
65
"io"
76
"mime/multipart"
8-
"net"
97
"net/http"
108
"net/http/httputil"
119
"net/url"
1210
"os"
1311
"runtime"
14-
"strings"
1512

1613
"github.com/tursodatabase/turso-cli/internal/flags"
1714
)
@@ -104,26 +101,7 @@ func (t *Client) do(method, path string, body io.Reader, extraHeaders map[string
104101
if err != nil {
105102
return nil, err
106103
}
107-
108-
var client = &http.Client{
109-
// go seems to have issues resolving things like region.localhost:9090, while curl works fine.
110-
// Make sure we can get it to work in those cases by resolving it manually.
111-
Transport: &http.Transport{
112-
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
113-
host, port, err := net.SplitHostPort(addr)
114-
if err != nil {
115-
return nil, err
116-
}
117-
118-
if strings.HasSuffix(host, ".localhost") {
119-
return (&net.Dialer{}).DialContext(ctx, network, net.JoinHostPort("127.0.0.1", port))
120-
}
121-
122-
return (&net.Dialer{}).DialContext(ctx, network, addr)
123-
},
124-
},
125-
}
126-
resp, err := client.Do(req)
104+
resp, err := http.DefaultClient.Do(req)
127105
if err != nil {
128106
return nil, err
129107
}

0 commit comments

Comments
 (0)