MCPcopy
hub / github.com/nats-io/nats.go / lookupGeo

Function lookupGeo

examples/nats-echo/main.go:198–217  ·  view source on GitHub ↗

lookup our current region and country..

()

Source from the content-addressed store, hash-verified

196
197// lookup our current region and country..
198func lookupGeo() string {
199 c := &http.Client{Timeout: 2 * time.Second}
200
201 url := os.Getenv("ECHO_SVC_GEO_URL")
202 if len(url) == 0 {
203 url = "https://ipapi.co/json"
204 }
205
206 resp, err := c.Get(url)
207 if err != nil || resp == nil {
208 log.Fatalf("Could not retrieve geo location data: %v", err)
209 }
210 defer resp.Body.Close()
211 body, _ := io.ReadAll(resp.Body)
212 g := geo{}
213 if err := json.Unmarshal(body, &g); err != nil {
214 log.Fatalf("Error unmarshalling geo: %v", err)
215 }
216 return g.Region + ", " + g.Country
217}

Callers 1

mainFunction · 0.85

Calls 3

FatalfMethod · 0.80
GetMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected