6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-07-03 00:33:38 +00:00
prologic-saltyim/internal/pwa/utils/lookup.go
James Mills 14857206cb Add client e2e integration tests (#180)
Co-authored-by: James Mills <1290234+prologic@users.noreply.github.com>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/180
2023-01-26 22:30:16 +00:00

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
}