-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
115 lines (86 loc) · 2.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var (
// Version number
Version = "1.0"
// CommitID of this build, set during build
CommitID string
// Branch is set during build
Branch string
)
func getVersion(ctx echo.Context) error {
return ctx.String(http.StatusOK,
fmt.Sprintf("%s - %s @ %s", Version, CommitID, Branch))
}
func getLiveBoard(ctx echo.Context) error {
stn := ctx.Param("stn")
b := getBoard(stn)
if strings.Compare(b.Station.Code, stn) != 0 {
return ctx.String(http.StatusNotFound, "Invalid station code")
}
return ctx.JSON(http.StatusOK, b)
}
func getServiceDetails(ctx echo.Context) error {
id := ctx.Param("id")
service := serviceDetails(id)
if len(service) < 1 {
ctx.String(http.StatusNotAcceptable, "Invalid service id")
}
return ctx.JSON(http.StatusOK, service)
}
func getStation(ctx echo.Context) error {
name := ctx.Param("stn")
if v, ok := Stations[name]; ok {
return ctx.JSON(http.StatusOK, v)
}
return ctx.String(http.StatusNotFound, "No such station")
}
func getSearchStations(ctx echo.Context) error {
name := ctx.Param("name")
return ctx.JSON(http.StatusOK, Stations.Search(name, 10))
}
func getStatus(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, getRouteStatuses())
}
func getNearest(ctx echo.Context) error {
lat, _ := strconv.ParseFloat(ctx.Param("lat"), 64)
lon, _ := strconv.ParseFloat(ctx.Param("lon"), 64)
dist, st := Stations.Nearest(Coordinate{lat: lat, lon: lon})
return ctx.JSON(
http.StatusOK, map[string]interface{}{"station": st, "distance": dist},
)
}
func getStations(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, Stations)
}
func index(ctx echo.Context) error {
d, _ := ioutil.ReadFile("./index.html")
return ctx.HTMLBlob(http.StatusOK, d)
}
func main() {
addr := ":" + os.Getenv("PORT")
e := echo.New()
e.Use(middleware.CORS())
loadStations()
loadRoutes("./routes.json")
e.GET("/", index)
e.GET("/version", getVersion)
e.GET("/status", getStatus)
e.GET("/live/:stn", getLiveBoard)
e.GET("/service/:id", getServiceDetails)
e.GET("/stations", getStations)
e.GET("/station/:stn", getStation)
e.GET("/search/:name", getSearchStations)
e.GET("/nearest/:lat/:lon", getNearest)
e.Static("/static", "./static")
e.Logger.Debug(e.Start(addr))
}