rewrite Python API to Go

This commit is contained in:
Steffen Vogel 2021-01-19 13:35:10 +01:00
parent e2dd0451d2
commit ae15cff014
6 changed files with 357 additions and 29 deletions

21
api/Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM golang:1.14
# Set the Current Working Directory inside the container
WORKDIR $GOPATH/src/app
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
# Download all the dependencies
RUN go get -d -v ./...
# Install the package
RUN go install -v ./...
# This container exposes port 8080 to the outside world
EXPOSE 8080
ENV GIN_MODE=release
# Run the executable
CMD ["ura"]

9
api/Makefile Normal file
View File

@ -0,0 +1,9 @@
IMAGE = stv0g/connectiq-ura
all: push
image:
docker build . -t $(IMAGE)
push: image
docker push $(IMAGE)

13
api/go.mod Normal file
View File

@ -0,0 +1,13 @@
module ura
go 1.15
require (
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/kellydunn/golang-geo v0.7.0 // indirect
github.com/kylelemons/go-gypsy v1.0.0 // indirect
github.com/lib/pq v1.9.0 // indirect
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 // indirect
github.com/ziutek/mymysql v1.5.4 // indirect
)

51
api/go.sum Normal file
View File

@ -0,0 +1,51 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kellydunn/golang-geo v0.7.0 h1:A5j0/BvNgGwY6Yb6inXQxzYwlPHc6WVZR+MrarZYNNg=
github.com/kellydunn/golang-geo v0.7.0/go.mod h1:YYlQPJ+DPEzrHx8kT3oPHC/NjyvCCXE+IuKGKdrjrcU=
github.com/kylelemons/go-gypsy v1.0.0 h1:7/wQ7A3UL1bnqRMnZ6T8cwCOArfZCxFmb1iTxaOOo1s=
github.com/kylelemons/go-gypsy v1.0.0/go.mod h1:chkXM0zjdpXOiqkCW1XcCHDfjfk14PH2KKkQWxfJUcU=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8=
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 h1:UFHFmFfixpmfRBcxuu+LA9l8MdURWVdVNUHxO5n1d2w=
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26/go.mod h1:IGhd0qMDsUa9acVjsbsT7bu3ktadtGOHI79+idTew/M=
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

263
api/main.go Normal file
View File

@ -0,0 +1,263 @@
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
geo "github.com/kellydunn/golang-geo"
)
var returnList = []string{
"StopPointName",
"StopID",
"StopPointIndicator",
"StopPointState",
"Latitude",
"Longitude",
}
var apiBase string = "http://ivu.aseag.de/interfaces/ura"
type StopPoint struct {
Name string `json:"name"`
ID int `json:"id"`
Indicator string `json:"indicator"`
State bool `json:"state"`
Coordinates *geo.Point `json:"coord"`
Distance float64 `json:"dist"`
Bearing float64 `json:"bearing"`
BearingString string `json:"bearing_str"`
}
func BearingToCardinal(b float64) string {
var card = []string{"N", "NE", "E", "SE", "S", "SW", "W", "NW"}
b += 22.5
if b < 0 {
b += 360
}
b /= 45
return card[int(b)]
}
func (sp *StopPoint) UnmarshalJSON(buf []byte) error {
// Ex: [0,"Bahnhof Rothe Erde","100302","H.1",0,50.7703152,6.1174127]
var rType int
var id string
var state int
var err error
var lat, lng float64
tmp := []interface{}{&rType, &sp.Name, &id, &sp.Indicator, &state, &lat, &lng}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return fmt.Errorf("Failed to parse: %s", buf)
}
sp.ID, err = strconv.Atoi(id)
if err != nil {
return nil
}
sp.State = state != 0
sp.Coordinates = geo.NewPoint(lat, lng)
if rType != 0 {
return fmt.Errorf("Invalid response type: %d != 0", rType)
}
if g, e := len(tmp), wantLen; g != e {
return fmt.Errorf("wrong number of fields in Notification: %d != %d", g, e)
}
return nil
}
type Prediction struct {
LineName string `json:"line"`
DestinationName string `json:"dest"`
EstimatedTime time.Time `json:"estimated"`
ExpireTime time.Time `json:"expire"`
Delta int `json:"delta"`
DeltaString string `json:"delta_str"`
}
func (p *Prediction) UnmarshalJSON(buf []byte) error {
// Ex: [1,"33","Vaals Heuvel",1610732635000,1610732880000]
var rType int
var estimated, expired json.Number
tmp := []interface{}{&rType, &p.LineName, &p.DestinationName, &estimated, &expired}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return fmt.Errorf("Failed to parse: %s", buf)
}
expiredInt, _ := expired.Int64()
estimatedInt, _ := estimated.Int64()
p.ExpireTime = time.Unix(expiredInt/1000, 0)
p.EstimatedTime = time.Unix(estimatedInt/1000, 0)
if rType != 1 {
return fmt.Errorf("Invalid response type: %d != 1", rType)
}
if g, e := len(tmp), wantLen; g != e {
return fmt.Errorf("wrong number of fields in Notification: %d != %d", g, e)
}
return nil
}
func main() {
r := gin.Default()
r.GET("/stops", func(c *gin.Context) {
latStr := c.Query("latitude")
lngStr := c.Query("longitude")
distStr := c.Query("distance")
limitStr := c.Query("limit")
var lat, lng float64
var dist float64
var err error
var limit int
lat, err = strconv.ParseFloat(latStr, 64)
if err != nil {
}
lng, err = strconv.ParseFloat(lngStr, 64)
if err != nil {
}
point := geo.NewPoint(lat, lng)
dist, err = strconv.ParseFloat(distStr, 64)
if err != nil {
}
limit, err = strconv.Atoi(limitStr)
if err != nil {
}
res, _ := http.Get(apiBase + "/instant_V1" +
fmt.Sprintf("?Circle=%f,%f,%f", point.Lat(), point.Lng(), dist) +
"&StopPointState=0" +
"&ReturnList=" + strings.Join(returnList, ","))
var sps = []StopPoint{}
scanner := bufio.NewScanner(res.Body)
if !scanner.Scan() {
}
_ = scanner.Bytes()
for scanner.Scan() {
line := scanner.Bytes()
var sp StopPoint
err = json.Unmarshal(line, &sp)
if err != nil {
continue
}
sp.Distance = point.GreatCircleDistance(sp.Coordinates) * 1000
sp.Bearing = point.BearingTo(sp.Coordinates)
sp.BearingString = BearingToCardinal(sp.Bearing)
sps = append(sps, sp)
}
sort.Slice(sps, func(a, b int) bool {
return sps[a].Distance < sps[b].Distance
})
if limit > len(sps) || limit == 0 {
limit = len(sps)
}
c.JSON(200, sps[:limit])
})
r.GET("/departures", func(c *gin.Context) {
var err error
var limit int
idStr := c.Query("id")
limitStr := c.Query("limit")
id, err := strconv.Atoi(idStr)
if err != nil {
}
limit, err = strconv.Atoi(limitStr)
if err != nil {
}
res, _ := http.Get(apiBase + "/instant_V1" +
fmt.Sprintf("?StopID=%d", id) +
"&ReturnList=LineName,DestinationName,EstimatedTime,ExpireTime")
var ps = []Prediction{}
scanner := bufio.NewScanner(res.Body)
if !scanner.Scan() {
}
_ = scanner.Bytes()
for scanner.Scan() {
line := scanner.Bytes()
var p Prediction
err = json.Unmarshal(line, &p)
if err != nil {
continue
}
delta := time.Until(p.EstimatedTime)
p.Delta = int(delta / 1e9) // to secs
p.DeltaString = delta.Truncate(time.Second).String()
ps = append(ps, p)
}
sort.Slice(ps, func(a, b int) bool {
return ps[a].EstimatedTime.Before(ps[b].EstimatedTime)
})
if limit > len(ps) || limit == 0 {
limit = len(ps)
}
c.JSON(200, ps[:limit])
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

View File

@ -1,29 +0,0 @@
#!/usr/bin/env python
import json
import requests
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape
def get(environ, start_response):
parameters = parse_qs(environ.get('QUERY_STRING', ''))
url = 'http://ivu.aseag.de/interfaces/ura/instant_V1'
# url = 'http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1'
r = requests.get(url, parameters)
output = []
for line in r.text.splitlines():
print line
output.append(json.loads(line))
start_response('200 OK', [('Content-Type', 'application/json')])
return json.dumps(output)
if __name__ == '__main__':
srv = make_server('localhost', 8080, get)
srv.serve_forever()