Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync server to status -v #176 so that self-hosted users can easily confirm they're using the self-hosted server #178

Merged
merged 1 commit into from
Feb 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions client/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func printOnlineStatus(config *hctx.ClientConfig) {
fmt.Println("Sync Mode: Disabled")
} else {
fmt.Println("Sync Mode: Enabled")
if lib.GetServerHostname() != lib.DefaultServerHostname {
fmt.Println("Sync Server: " + lib.GetServerHostname())
}
if config.HaveMissedUploads || len(config.PendingDeletionRequests) > 0 {
fmt.Println("Sync Status: Unsynced (device is offline?)")
fmt.Printf(" HaveMissedUploads=%v MissedUploadTimestamp=%v len(PendingDeletionRequests)=%v\n", config.HaveMissedUploads, config.MissedUploadTimestamp, len(config.PendingDeletionRequests))
Expand Down
24 changes: 13 additions & 11 deletions client/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,13 @@ func readFileToIterator(path string) Seq2[string, error] {
}
}

func getServerHostname() string {
const DefaultServerHostname = "https://api.hishtory.dev"

func GetServerHostname() string {
if server := os.Getenv("HISHTORY_SERVER"); server != "" {
return server
}
return "https://api.hishtory.dev"
return DefaultServerHostname
}

func httpClient() *http.Client {
Expand All @@ -459,7 +461,7 @@ func ApiGet(ctx context.Context, path string) ([]byte, error) {
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
}
start := time.Now()
req, err := http.NewRequest("GET", getServerHostname()+path, nil)
req, err := http.NewRequest("GET", GetServerHostname()+path, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GET: %w", err)
}
Expand All @@ -468,15 +470,15 @@ func ApiGet(ctx context.Context, path string) ([]byte, error) {
req.Header.Set("X-Hishtory-User-Id", data.UserId(hctx.GetConf(ctx).UserSecret))
resp, err := httpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("failed to GET %s%s: %w", getServerHostname(), path, err)
return nil, fmt.Errorf("failed to GET %s%s: %w", GetServerHostname(), path, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to GET %s%s: status_code=%d", getServerHostname(), path, resp.StatusCode)
return nil, fmt.Errorf("failed to GET %s%s: status_code=%d", GetServerHostname(), path, resp.StatusCode)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body from GET %s%s: %w", getServerHostname(), path, err)
return nil, fmt.Errorf("failed to read response body from GET %s%s: %w", GetServerHostname(), path, err)
}
duration := time.Since(start)
hctx.GetLogger().Infof("ApiGet(%#v): %s\n", path, duration.String())
Expand All @@ -488,7 +490,7 @@ func ApiPost(ctx context.Context, path, contentType string, reqBody []byte) ([]b
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
}
start := time.Now()
req, err := http.NewRequest("POST", getServerHostname()+path, bytes.NewBuffer(reqBody))
req, err := http.NewRequest("POST", GetServerHostname()+path, bytes.NewBuffer(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create POST: %w", err)
}
Expand All @@ -498,18 +500,18 @@ func ApiPost(ctx context.Context, path, contentType string, reqBody []byte) ([]b
req.Header.Set("X-Hishtory-User-Id", data.UserId(hctx.GetConf(ctx).UserSecret))
resp, err := httpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("failed to POST %s: %w", getServerHostname()+path, err)
return nil, fmt.Errorf("failed to POST %s: %w", GetServerHostname()+path, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to POST %s: status_code=%d", getServerHostname()+path, resp.StatusCode)
return nil, fmt.Errorf("failed to POST %s: status_code=%d", GetServerHostname()+path, resp.StatusCode)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body from POST %s: %w", getServerHostname()+path, err)
return nil, fmt.Errorf("failed to read response body from POST %s: %w", GetServerHostname()+path, err)
}
duration := time.Since(start)
hctx.GetLogger().Infof("ApiPost(%#v): %s\n", getServerHostname()+path, duration.String())
hctx.GetLogger().Infof("ApiPost(%#v): %s\n", GetServerHostname()+path, duration.String())
return respBody, nil
}

Expand Down
Loading