Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ func closestLocation(client *turso.Client) (string, error) {
return closest, nil
}

closest, err := client.Locations.Closest()
settings, err := settings.ReadSettings()
if err != nil {
return "", fmt.Errorf("could not read settings file to probe for locations: %w", err)
}

regionUrl := settings.GetRegionURL()
closest, err := client.Locations.Closest(regionUrl)
if err != nil {
// We fallback to ams if we are unable to probe the closest location.
return "ams", err
Expand Down
12 changes: 12 additions & 0 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package settings

import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -148,6 +149,17 @@ func (s *Settings) GetProxyURL() string {
return viper.GetString("proxyURL")
}

func (s *Settings) GetRegionURL() string {
baseURL := s.GetProxyURL()
parsedURL, err := url.Parse(baseURL)
if err != nil || baseURL == "" {
return "https://region.turso.io"
}

parsedURL.Host = "region." + parsedURL.Host
return parsedURL.String()
}

func (s *Settings) SetAutoupdate(autoupdate string) {
config := viper.GetStringMap("config")
if config == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/turso/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type ClosestLocationResponse struct {
Server string
}

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