func lookupGeo() string {
	c := &http.Client{Timeout: 2 * time.Second}
	resp, err := c.Get("https://ipinfo.io")
	if err != nil || resp == nil {
		log.Fatalf("Could not retrive geo location data: %v", err)
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	g := geo{}
	if err := json.Unmarshal(body, &g); err != nil {
		log.Fatalf("Error unmarshalling geo: %v", err)
	}
	return g.Region + ", " + g.Country
}
lookup our current region and country..
package main
import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/signal"
	"runtime"
	"syscall"
	"time"
	"github.com/nats-io/go-nats"
)
func usage() {
	log.Printf("Usage: nats-echo [-s server] [-creds file] [-t] <subject>\n")
	flag.PrintDefaults()
}
func printMsg(m *nats.Msg, i int) {
	log.Printf("[#%d] Echoing to [%s]: %q", i, m.Reply, m.Data)
}
func main() {
	var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
	var userCreds = flag.String("creds", "", "User Credentials File")
	var showTime = flag.Bool("t", false, "Display timestamps")
	var geoloc = flag.Bool("geo", false, "Display geo location of echo service")
	var geo string
	log.SetFlags(0)
	flag.Usage = usage
	flag.Parse()
	args := flag.Args()
	if len(args) != 1 {
		usage()
	}
	if *geoloc {
		geo = lookupGeo()
	}
	opts := []nats.Option{nats.Name("NATS Echo Service")}
	opts = setupConnOptions(opts)
	if *userCreds != "" {
		opts = append(opts, nats.UserCredentials(*userCreds))
	}
	nc, err := nats.Connect(*urls, opts...)
	if err != nil {
		log.Fatal(err)
	}
	subj, i := args[0], 0
	nc.QueueSubscribe(subj, "echo", func(msg *nats.Msg) {
		i++
		if msg.Reply != "" {
			printMsg(msg, i)
			if geo != "" {
				m := fmt.Sprintf("[%s]: %q", geo, msg.Data)
				nc.Publish(msg.Reply, []byte(m))
			} else {
				nc.Publish(msg.Reply, msg.Data)
			}
		}
	})
	nc.Flush()
	if err := nc.LastError(); err != nil {
		log.Fatal(err)
	}
	log.Printf("Echo Service listening on [%s]\n", subj)
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT)
	go func() {
		<-c
		log.Printf("<caught signal - draining>")
		nc.Drain()
	}()
	if *showTime {
		log.SetFlags(log.LstdFlags)
	}
	runtime.Goexit()
}
func setupConnOptions(opts []nats.Option) []nats.Option {
	totalWait := 10 * time.Minute
	reconnectDelay := time.Second
	opts = append(opts, nats.ReconnectWait(reconnectDelay))
	opts = append(opts, nats.MaxReconnects(int(totalWait/reconnectDelay)))
	opts = append(opts, nats.DisconnectHandler(func(nc *nats.Conn) {
		if !nc.IsClosed() {
			log.Printf("Disconnected: will attempt reconnects for %.0fm", totalWait.Minutes())
		}
	}))
	opts = append(opts, nats.ReconnectHandler(func(nc *nats.Conn) {
		log.Printf("Reconnected [%s]", nc.ConnectedUrl())
	}))
	opts = append(opts, nats.ClosedHandler(func(nc *nats.Conn) {
		if !nc.IsClosed() {
			log.Fatal("Exiting: no servers available")
		} else {
			log.Fatal("Exiting")
		}
	}))
	return opts
}
type geo struct {
	Region  string
	Country string
}