package handlers import ( "encoding/json" "net/http" "net/url" "github.com/forgeo/forgebucket/internal/config" ) type InstanceHandler struct { cfg *config.Config } func NewInstanceHandler(cfg *config.Config) *InstanceHandler { return &InstanceHandler{cfg: cfg} } // Get returns the public instance configuration needed by the frontend to // construct clone URLs (SSH host, SSH port, instance name). func (h *InstanceHandler) Get(w http.ResponseWriter, r *http.Request) { sshHost := h.sshHost(r) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]string{ //nolint:errcheck "sshHost": sshHost, "sshPort": h.cfg.SSHPort, "instanceName": h.cfg.InstanceName, }) } // sshHost extracts the hostname from InstanceURL. Falls back to the request // host when InstanceURL is unset (common in local development). func (h *InstanceHandler) sshHost(r *http.Request) string { if h.cfg.InstanceURL != "" { if u, err := url.Parse(h.cfg.InstanceURL); err == nil && u.Hostname() != "" { return u.Hostname() } } // Strip port from Host header if present. host := r.Host if u, err := url.Parse("http://" + host); err == nil { return u.Hostname() } return host }