Switch to http-protocol
This commit is contained in:
Generated
+2
-26
@@ -1,34 +1,10 @@
|
|||||||
hash: 9942efaff02d4094b23213cd68b23c3a8c3eea8fc6d5b4a9060869a8a305677f
|
hash: 970b38f08b875634ffd52ae0c930abe90a206ae26803a5c66462ea0358178d58
|
||||||
updated: 2019-02-10T19:52:18.799824+01:00
|
updated: 2019-02-12T16:59:28.793243+01:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/caarlos0/env
|
- name: github.com/caarlos0/env
|
||||||
version: 518fcfb943252f77846f92ce5794086d9cb4a1b5
|
version: 518fcfb943252f77846f92ce5794086d9cb4a1b5
|
||||||
- name: github.com/gogo/protobuf
|
|
||||||
version: 8e4a75f11384d0fb2188d872d456407e41f33fc0
|
|
||||||
subpackages:
|
|
||||||
- gogoproto
|
|
||||||
- proto
|
|
||||||
- protoc-gen-gogo/descriptor
|
|
||||||
- name: github.com/google/uuid
|
- name: github.com/google/uuid
|
||||||
version: 9b3b1e0f5f99ae461456d768e7d301a7acdaa2d8
|
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
|
- name: golang.org/x/time
|
||||||
version: 85acf8d2951cb2a3bde7632f9ff273ef0379bcbd
|
version: 85acf8d2951cb2a3bde7632f9ff273ef0379bcbd
|
||||||
subpackages:
|
subpackages:
|
||||||
|
|||||||
+2
-4
@@ -1,9 +1,7 @@
|
|||||||
package: gitlab.com/unboundsoftware/geo-service
|
package: gitlab.com/unboundsoftware/geo-service
|
||||||
import:
|
import:
|
||||||
- package: github.com/nats-io/go-nats
|
|
||||||
version: ^1.7.0
|
|
||||||
- package: googlemaps.github.io/maps
|
- package: googlemaps.github.io/maps
|
||||||
- package: github.com/nats-io/go-nats-streaming
|
|
||||||
version: ^0.4.0
|
|
||||||
- package: github.com/caarlos0/env
|
- package: github.com/caarlos0/env
|
||||||
version: ^3.5.0
|
version: ^3.5.0
|
||||||
|
- package: github.com/google/uuid
|
||||||
|
version: ^1.1.0
|
||||||
|
|||||||
@@ -5,31 +5,16 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/caarlos0/env"
|
"github.com/caarlos0/env"
|
||||||
"github.com/nats-io/go-nats-streaming"
|
|
||||||
"googlemaps.github.io/maps"
|
"googlemaps.github.io/maps"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"net/http"
|
||||||
"os/signal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
MapsApiKey string `env:"MAPS_API_KEY"`
|
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:"municipality"`
|
|
||||||
State string `json:"state"`
|
|
||||||
Created string `json:"created"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type location struct {
|
type location struct {
|
||||||
Name string `json:"name"`
|
|
||||||
City string `json:"city"`
|
|
||||||
Municipality string `json:"municipality"`
|
|
||||||
State string `json:"state"`
|
|
||||||
Lat float64 `json:"lat"`
|
Lat float64 `json:"lat"`
|
||||||
Long float64 `json:"long"`
|
Long float64 `json:"long"`
|
||||||
}
|
}
|
||||||
@@ -42,70 +27,39 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", cfg)
|
fmt.Printf("%+v\n", cfg)
|
||||||
|
|
||||||
clusterID := "stan"
|
client, err := maps.NewClient(maps.WithAPIKey(cfg.MapsApiKey))
|
||||||
clientID := "geo-service"
|
|
||||||
|
|
||||||
c, err := maps.NewClient(maps.WithAPIKey(cfg.MapsApiKey))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("fatal error: %s", err)
|
log.Fatalf("fatal error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sc, err := stan.Connect(clusterID, clientID, stan.NatsURL(cfg.NATSUrl),
|
http.HandleFunc("/latlong/", makeHandler(handleLatLongRequest, client))
|
||||||
stan.SetConnectionLostHandler(func(_ stan.Conn, reason error) {
|
log.Fatal(http.ListenAndServe(":8000", nil))
|
||||||
log.Fatalf("Connection lost, reason: %v", reason)
|
}
|
||||||
}))
|
|
||||||
if err != nil {
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), client *maps.Client) http.HandlerFunc {
|
||||||
log.Fatalf("Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s", err, cfg.NATSUrl)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fn(w, r, client)
|
||||||
}
|
}
|
||||||
log.Printf("Connected to %s clusterID: [%s] clientID: [%s]\n", cfg.NATSUrl, clusterID, clientID)
|
}
|
||||||
|
func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
||||||
mcb := func(msg *stan.Msg) {
|
req := &maps.GeocodingRequest{
|
||||||
e := event{}
|
Address: r.URL.Path[len("/latlong/"):],
|
||||||
if err := json.Unmarshal(msg.Data, &e); err != nil {
|
}
|
||||||
panic(err)
|
if result, err := client.Geocode(context.Background(), req); err != nil {
|
||||||
}
|
log.Fatalf("fatal error: %s", err)
|
||||||
|
w.WriteHeader(400)
|
||||||
r := &maps.GeocodingRequest{
|
} else {
|
||||||
Address: fmt.Sprintf("%s,%s,%s,%s", e.Name, e.City, e.Municipality, e.State),
|
if len(result) > 0 {
|
||||||
}
|
l := location{
|
||||||
if result, err := c.Geocode(context.Background(), r); err != nil {
|
Lat: result[0].Geometry.Location.Lat,
|
||||||
log.Fatalf("fatal error: %s", err)
|
Long: result[0].Geometry.Location.Lng,
|
||||||
} else {
|
}
|
||||||
if len(result) > 0 {
|
if response, err := json.Marshal(l); err != nil {
|
||||||
l := location{
|
log.Fatalf("fatal error: %s", err)
|
||||||
Name: e.Name,
|
w.WriteHeader(404)
|
||||||
City: e.City,
|
} else {
|
||||||
Municipality: e.Municipality,
|
_, _ = w.Write(response)
|
||||||
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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user