package utils import ( "encoding/json" "io/ioutil" "net/http" "github.com/maxence-charriere/go-app/v9/pkg/app" "go.mills.io/saltyim" ) // LookupAddr performs a lookup of a Salty Addr directly and if the request // fails for whatever reason (usuaully due to Cross-Orogin-Resource-Sharing // policies / CORS) it uses the Salty Broker the PWA was served from // initially to perform the lookup by proxying the lookup through the broker. // Why? CORS sucks. func LookupAddr(user string) (saltyim.Addr, error) { addr, err := saltyim.LookupAddr(user) if err == nil { return addr, nil } // Fallback to proxying the lookup through the broker... res, err := saltyim.Request(http.MethodGet, app.Getenv("LookupEndpoint")+user, nil, nil) if err != nil { return nil, err } defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } if err := json.Unmarshal(data, &addr); err != nil { return nil, err } return addr, nil }