63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"forge-mesh/internal/auth"
|
|
"forge-mesh/internal/forge"
|
|
)
|
|
|
|
// DropperHandler serves the dropper URL and one-liner info to the operator.
|
|
type DropperHandler struct {
|
|
DB *sql.DB
|
|
PublicKeyHex string
|
|
FleetSecret string
|
|
Version string
|
|
}
|
|
|
|
type DropperInfo struct {
|
|
DropperURL string `json:"dropper_url"`
|
|
InstallURL string `json:"install_url"`
|
|
OneLiner string `json:"one_liner"`
|
|
FleetSecret string `json:"fleet_secret"`
|
|
PublicKey string `json:"public_key"`
|
|
HasBuild bool `json:"has_build"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
func deckURL(r *http.Request) string {
|
|
scheme := "http"
|
|
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
|
|
scheme = "https"
|
|
}
|
|
return fmt.Sprintf("%s://%s", scheme, r.Host)
|
|
}
|
|
|
|
// Info handles GET /api/v1/dropper — returns dropper link, one-liner, and build status.
|
|
func (h *DropperHandler) Info(w http.ResponseWriter, r *http.Request) {
|
|
base := deckURL(r)
|
|
installURL := base + "/install.sh"
|
|
dropperURL := base + "/get"
|
|
|
|
oneLiner := fmt.Sprintf(
|
|
"FORGE_MESH_FLEET_SECRET=%s bash <(curl -fsSL %s)",
|
|
h.FleetSecret, installURL,
|
|
)
|
|
|
|
// Check whether a public build exists so the UI can warn if not.
|
|
_, err := forge.LatestPublic(h.DB, "linux", "amd64")
|
|
hasBuild := err == nil
|
|
|
|
auth.JSON(w, http.StatusOK, DropperInfo{
|
|
DropperURL: dropperURL,
|
|
InstallURL: installURL,
|
|
OneLiner: oneLiner,
|
|
FleetSecret: h.FleetSecret,
|
|
PublicKey: h.PublicKeyHex,
|
|
HasBuild: hasBuild,
|
|
Version: h.Version,
|
|
})
|
|
}
|