From eebefb0ef40304dd103e200303dc695abe54700a Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Sun, 10 Feb 2019 20:35:19 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + glide.lock | 40 ++++++++++++++++++++ glide.yaml | 9 +++++ main.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100644 glide.lock create mode 100644 glide.yaml create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ecdf2d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +.idea diff --git a/glide.lock b/glide.lock new file mode 100644 index 0000000..92891af --- /dev/null +++ b/glide.lock @@ -0,0 +1,40 @@ +hash: 9942efaff02d4094b23213cd68b23c3a8c3eea8fc6d5b4a9060869a8a305677f +updated: 2019-02-10T19:52:18.799824+01:00 +imports: +- name: github.com/caarlos0/env + version: 518fcfb943252f77846f92ce5794086d9cb4a1b5 +- name: github.com/gogo/protobuf + version: 8e4a75f11384d0fb2188d872d456407e41f33fc0 + subpackages: + - gogoproto + - proto + - protoc-gen-gogo/descriptor +- name: github.com/google/uuid + version: 9b3b1e0f5f99ae461456d768e7d301a7acdaa2d8 +- name: github.com/nats-io/go-nats + version: 13c7fc7590db68b18f2015600f8765a02d705b5d + subpackages: + - encoders/builtin + - util +- name: github.com/nats-io/go-nats-streaming + version: e15a53f85e4932540600a16b56f6c4f65f58176f + subpackages: + - pb +- name: github.com/nats-io/nkeys + version: 1546a3320a8f195a5b5c84aef8309377c2e411d5 +- name: github.com/nats-io/nuid + version: 3024a71c3cbe30667286099921591e6fcc328230 +- name: golang.org/x/crypto + version: 193df9c0f06f8bb35fba505183eaf0acc0136505 + subpackages: + - ed25519 + - ed25519/internal/edwards25519 +- name: golang.org/x/time + version: 85acf8d2951cb2a3bde7632f9ff273ef0379bcbd + subpackages: + - rate +- name: googlemaps.github.io/maps + version: be134e760d707b2057d6a68668e19c509147d805 + subpackages: + - internal +testImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..56a99a6 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,9 @@ +package: gitlab.com/unboundsoftware/geo-service +import: +- package: github.com/nats-io/go-nats + version: ^1.7.0 +- package: googlemaps.github.io/maps +- package: github.com/nats-io/go-nats-streaming + version: ^0.4.0 +- package: github.com/caarlos0/env + version: ^3.5.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..21f0107 --- /dev/null +++ b/main.go @@ -0,0 +1,109 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "github.com/caarlos0/env" + "github.com/nats-io/go-nats-streaming" + "googlemaps.github.io/maps" + "log" + "os" + "os/signal" +) + +type config struct { + MapsApiKey string `env:"MAPS_API_KEY"` + NATSUrl string `env:"NATS_URL" envDefault:"nats://nats:4222"` +} + +type event struct { + Name string `json:"name"` + City string `json:"city"` + Municipality string `json:"municiplity"` + State string `json:"state"` + Created string `json:"created"` +} + +type location struct { + Name string `json:"name"` + City string `json:"city"` + Municipality string `json:"municiplity"` + State string `json:"state"` + Lat float64 `json:"lat"` + Long float64 `json:"long"` +} + +func main() { + cfg := config{} + err := env.Parse(&cfg) + if err != nil { + fmt.Printf("%+v\n", err) + } + fmt.Printf("%+v\n", cfg) + + clusterID := "stan" + clientID := "geo-service" + + c, err := maps.NewClient(maps.WithAPIKey(cfg.MapsApiKey)) + if err != nil { + log.Fatalf("fatal error: %s", err) + } + + sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(cfg.NATSUrl), + stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) { + log.Fatalf("Connection lost, reason: %v", reason) + })) + if err != nil { + log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, cfg.NATSUrl) + } + log.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", cfg.NATSUrl, clusterID, clientID) + + mcb := func(msg *stan.Msg) { + e := event{} + if err := json.Unmarshal(msg.Data, &e); err != nil { + panic(err) + } + + r := &maps.GeocodingRequest{ + Address: fmt.Sprintf("%s,%s,%s,%s", e.Name, e.City, e.Municipality, e.State), + } + if result, err := c.Geocode(context.Background(), r); err != nil { + log.Fatalf("fatal error: %s", err) + } else { + l := location{ + Name: e.Name, + City: e.City, + Municipality: e.Municipality, + State: e.State, + Lat: result[0].Geometry.Location.Lat, + Long: result[0].Geometry.Location.Lng, + } + if response, err := json.Marshal(l); err != nil { + log.Fatalf("fatal error: %s", err) + } else { + if err := sc.Publish("DanceHall.Location", response); err != nil { + log.Fatalf("fatal error: %s", err) + } + } + } + } + + if _, err := sc.QueueSubscribe("DanceHall.Created", "geo-service", mcb, stan.StartWithLastReceived(), stan.DurableName("geo-service")); err != nil { + _ = sc.Close() + log.Fatal(err) + } + + signalChan := make(chan os.Signal, 1) + cleanupDone := make(chan bool) + signal.Notify(signalChan, os.Interrupt) + go func() { + for range signalChan { + fmt.Printf("\nReceived an interrupt, unsubscribing and closing connection...\n\n") + // Do not unsubscribe a durable on exit, except if asked to. + _ = sc.Close() + cleanupDone <- true + } + }() + <-cleanupDone +}