Switch to http-protocol

This commit is contained in:
2019-02-12 17:01:02 +01:00
parent 3d4d212e08
commit c8082554ec
3 changed files with 33 additions and 105 deletions
Generated
+2 -26
View File
@@ -1,34 +1,10 @@
hash: 9942efaff02d4094b23213cd68b23c3a8c3eea8fc6d5b4a9060869a8a305677f
updated: 2019-02-10T19:52:18.799824+01:00
hash: 970b38f08b875634ffd52ae0c930abe90a206ae26803a5c66462ea0358178d58
updated: 2019-02-12T16:59:28.793243+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:
+2 -4
View File
@@ -1,9 +1,7 @@
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
- package: github.com/google/uuid
version: ^1.1.0
+29 -75
View File
@@ -5,31 +5,16 @@ import (
"encoding/json"
"fmt"
"github.com/caarlos0/env"
"github.com/nats-io/go-nats-streaming"
"googlemaps.github.io/maps"
"log"
"os"
"os/signal"
"net/http"
)
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:"municipality"`
State string `json:"state"`
Created string `json:"created"`
}
type location struct {
Name string `json:"name"`
City string `json:"city"`
Municipality string `json:"municipality"`
State string `json:"state"`
Lat float64 `json:"lat"`
Long float64 `json:"long"`
}
@@ -42,70 +27,39 @@ func main() {
}
fmt.Printf("%+v\n", cfg)
clusterID := "stan"
clientID := "geo-service"
c, err := maps.NewClient(maps.WithAPIKey(cfg.MapsApiKey))
client, 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)
http.HandleFunc("/latlong/", makeHandler(handleLatLongRequest, client))
log.Fatal(http.ListenAndServe(":8000", nil))
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), client *maps.Client) http.HandlerFunc {
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)
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 {
if len(result) > 0 {
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)
}
}
}
func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
req := &maps.GeocodingRequest{
Address: r.URL.Path[len("/latlong/"):],
}
if result, err := client.Geocode(context.Background(), req); err != nil {
log.Fatalf("fatal error: %s", err)
w.WriteHeader(400)
} else {
if len(result) > 0 {
l := location{
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)
w.WriteHeader(404)
} else {
_, _ = w.Write(response)
}
}
}
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
}
}