Skip to content

Commit eefc9c8

Browse files
committed
get region URL from Proxy variable
We already use the proxy to connect to databases if set: now also use it to resolve the region endpoint. This will be useful when running the proxy locally
1 parent b2cca6e commit eefc9c8

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

internal/cmd/db.go

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

185-
closest, err := client.Locations.Closest()
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)
186192
if err != nil {
187193
// We fallback to ams if we are unable to probe the closest location.
188194
return "ams", err

internal/settings/settings.go

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

33
import (
44
"fmt"
5+
"net/url"
56
"os"
67
"path"
78
"path/filepath"
@@ -148,6 +149,17 @@ func (s *Settings) GetProxyURL() string {
148149
return viper.GetString("proxyURL")
149150
}
150151

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+
151163
func (s *Settings) SetAutoupdate(autoupdate string) {
152164
config := viper.GetStringMap("config")
153165
if config == nil {

internal/turso/locations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ type ClosestLocationResponse struct {
5252
Server string
5353
}
5454

55-
func (c *LocationsClient) Closest() (string, error) {
56-
r, err := c.client.Get("https://region.turso.io", nil)
55+
func (c *LocationsClient) Closest(regionUrl string) (string, error) {
56+
r, err := c.client.Get(regionUrl, nil)
5757
if err != nil {
5858
return "", fmt.Errorf("failed to request closest: %s", err)
5959
}

0 commit comments

Comments
 (0)