on main help sign in
refactor(server): derive per-host cache dir from parsed host URL

Co-Authored-By: Charm Crush <crush@charm.land>

commitdd1be0e0c12b39f774eae783ce0ca35cfd4d5917 authorChristian Rocha <christian@rocha.is> dateMon 11 May 2026, 19:27 (5 months ago) parent2dba83a
2 files changed+24−16
internal/cmd/root.go+18−10 internal/cmd/server.go+6−6
internal/cmd/root.go+18−10
@@ -426,7 +426,7 @@ func ensureServer(cmd *cobra.Command, hostURL *url.URL) error {
426426// duration of "spawn + readiness probe" and released before the caller
427427// resumes its normal lifetime.
428428func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
429− chDir, err := perHostServerDir()
429+ chDir, err := perHostServerDir(hostURL)
430430 if err != nil {
431431 return err
432432 }
@@ -435,7 +435,7 @@ func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
435435 // If the lock itself is unavailable, fall back to the
436436 // unsynchronized path rather than blocking the user.
437437 slog.Warn("Failed to acquire spawn lock, proceeding without single-flight", "error", err)
438− if err := startDetachedServer(cmd); err != nil {
438+ if err := startDetachedServer(cmd, hostURL); err != nil {
439439 return err
440440 }
441441 return waitForServerReady(cmd.Context(), hostURL)
@@ -452,7 +452,7 @@ func spawnAndWaitReady(cmd *cobra.Command, hostURL *url.URL) error {
452452 return nil
453453 }
454454
455− if err := startDetachedServer(cmd); err != nil {
455+ if err := startDetachedServer(cmd, hostURL); err != nil {
456456 return err
457457 }
458458 return waitForServerReady(cmd.Context(), hostURL)
@@ -469,17 +469,25 @@ func quickHealthProbe(ctx context.Context, hostURL *url.URL) error {
469469}
470470
471471// perHostServerDir returns (and creates) the cache directory used for
472−// per-host server state (logs, start.lock, etc.). It mirrors the path
473−// computed in startDetachedServer so both code paths stay in sync.
474−func perHostServerDir() (string, error) {
475− safeClientHost := safeNameRegexp.ReplaceAllString(clientHost, "_")
476− chDir := filepath.Join(config.GlobalCacheDir(), "server-"+safeClientHost)
472+// per-host server state (logs, start.lock, etc.). The path is derived
473+// from the parsed host URL rather than the global flag so the same key
474+// is computed regardless of where the host came from.
475+func perHostServerDir(hostURL *url.URL) (string, error) {
476+ chDir := filepath.Join(config.GlobalCacheDir(), "server-"+safeHostName(hostURL))
477477 if err := os.MkdirAll(chDir, 0o700); err != nil {
478478 return "", fmt.Errorf("failed to create server working directory: %v", err)
479479 }
480480 return chDir, nil
481481}
482482
483+// safeHostName returns a filesystem-safe identifier for hostURL,
484+// suitable for use as a directory name. It mirrors the input shape of
485+// the --host flag so client and server compute the same key.
486+func safeHostName(hostURL *url.URL) string {
487+ return safeNameRegexp.ReplaceAllString(
488+ hostURL.Scheme+"://"+hostURL.Host+hostURL.Path, "_")
489+}
490+
483491// serverReadyTimeout returns the total budget for the readiness probe.
484492// Overridable via CRUSH_SERVER_READY_TIMEOUT (parsed as a Go duration).
485493func serverReadyTimeout() time.Duration {
@@ -631,13 +639,13 @@ func restartIfStale(cmd *cobra.Command, hostURL *url.URL) (restarted bool, err e
631639
632640var safeNameRegexp = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
633641
634−func startDetachedServer(cmd *cobra.Command) error {
642+func startDetachedServer(cmd *cobra.Command, hostURL *url.URL) error {
635643 exe, err := os.Executable()
636644 if err != nil {
637645 return fmt.Errorf("failed to get executable path: %v", err)
638646 }
639647
640− chDir, err := perHostServerDir()
648+ chDir, err := perHostServerDir(hostURL)
641649 if err != nil {
642650 return err
643651 }
internal/cmd/server.go+6−6
@@ -42,7 +42,12 @@ var serverCmd = &cobra.Command{
4242 return fmt.Errorf("failed to load configuration: %v", err)
4343 }
4444
45− logFile := filepath.Join(config.GlobalCacheDir(), "server-"+safeNameRegexp.ReplaceAllString(serverHost, "_"), "crush.log")
45+ hostURL, err := server.ParseHostURL(serverHost)
46+ if err != nil {
47+ return fmt.Errorf("invalid server host: %v", err)
48+ }
49+
50+ logFile := filepath.Join(config.GlobalCacheDir(), "server-"+safeHostName(hostURL), "crush.log")
4651
4752 if term.IsTerminal(os.Stderr.Fd()) {
4853 crushlog.Setup(logFile, debug, os.Stderr)
@@ -50,11 +55,6 @@ var serverCmd = &cobra.Command{
5055 crushlog.Setup(logFile, debug)
5156 }
5257
53− hostURL, err := server.ParseHostURL(serverHost)
54− if err != nil {
55− return fmt.Errorf("invalid server host: %v", err)
56− }
57−
5858 srv := server.NewServer(cfg, hostURL.Scheme, hostURL.Host)
5959 srv.SetLogger(slog.Default())
6060 slog.Info("Starting Crush server...", "addr", serverHost)