mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-07-03 00:33:38 +00:00
14857206cb
Co-authored-by: James Mills <1290234+prologic@users.noreply.github.com> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/180
42 lines
980 B
Go
42 lines
980 B
Go
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
|
|
}
|