package handler import ( "net/http" "goflare/internal/database/queries" "goflare/internal/ipfetcher" "github.com/labstack/echo/v4" ) type IPHandler struct { q *queries.Queries } func NewIPHandler(q *queries.Queries) *IPHandler { return &IPHandler{q: q} } const settingKeyCurrentIP = "current_ip" func (h *IPHandler) Get(c echo.Context) error { ctx := c.Request().Context() cached, err := h.q.GetSetting(ctx, settingKeyCurrentIP) if err == nil && cached != "" { return c.JSON(http.StatusOK, map[string]string{"ip": cached}) } urls, err := h.q.ListEnabledIpProviderUrls(ctx) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to load IP providers"}) } var urlsOrNil []string if len(urls) > 0 { urlsOrNil = urls } ip, err := ipfetcher.FetchPublicIPFrom(ctx, urlsOrNil) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to fetch public IP"}) } return c.JSON(http.StatusOK, map[string]string{"ip": ip}) }