-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
154 lines (133 loc) · 3.98 KB
/
Copy pathmain.go
File metadata and controls
154 lines (133 loc) · 3.98 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
_ "embed"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/authenticvision/rgeo"
"github.com/gin-gonic/gin"
"github.com/twpayne/go-geom"
)
// snappingDistanceKM controls how far ReverseGeocodeSnapping will look for
// the nearest landmass when a coordinate doesn't fall directly inside a
// polygon (e.g. GPS noise placing a photo just offshore).
const snappingDistanceKM = 5.0
//go:embed docs/openapi.json
var openAPISpec []byte
func main() {
addr := flag.String("addr", envOr("LISTEN_ADDR", ":8080"), "address to listen on")
healthcheck := flag.Bool("healthcheck", false, "perform an HTTP healthcheck against -addr and exit (used by Docker HEALTHCHECK)")
flag.Parse()
if *healthcheck {
os.Exit(runHealthcheck(*addr))
}
log.Println("loading reverse geocoding datasets...")
geo, err := rgeo.New(rgeo.Cities10, rgeo.Provinces10)
if err != nil {
log.Fatalf("failed to initialize rgeo: %v", err)
}
geo.Build()
geo.SetSnappingDistanceEarth(snappingDistanceKM)
log.Println("datasets loaded")
r := newRouter(geo)
log.Printf("listening on %s", *addr)
if err := r.Run(*addr); err != nil {
log.Fatalf("server failed: %v", err)
}
}
// newRouter builds the Gin engine and registers all routes. Split out from
// main so tests can exercise the HTTP layer with httptest without going
// through flag parsing or r.Run.
func newRouter(geo *rgeo.Rgeo) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery(), gin.Logger())
r.GET("/health", func(c *gin.Context) {
c.Status(http.StatusOK)
})
r.GET("/openapi.json", func(c *gin.Context) {
c.Data(http.StatusOK, "application/json", openAPISpec)
})
r.GET("/reverse", reverseHandler(geo))
return r
}
func reverseHandler(geo *rgeo.Rgeo) gin.HandlerFunc {
return func(c *gin.Context) {
latStr := c.Query("lat")
lonStr := c.Query("lon")
if latStr == "" || lonStr == "" {
c.JSON(http.StatusBadRequest, nominatimErrorResponse{Error: "missing required parameters: lat, lon"})
return
}
lat, err := strconv.ParseFloat(latStr, 64)
if err != nil || lat < -90 || lat > 90 {
c.JSON(http.StatusBadRequest, nominatimErrorResponse{Error: "invalid lat parameter"})
return
}
lon, err := strconv.ParseFloat(lonStr, 64)
if err != nil || lon < -180 || lon > 180 {
c.JSON(http.StatusBadRequest, nominatimErrorResponse{Error: "invalid lon parameter"})
return
}
zoom := 18
if zoomStr := c.Query("zoom"); zoomStr != "" {
if z, err := strconv.Atoi(zoomStr); err == nil {
zoom = z
}
}
loc, err := geo.ReverseGeocodeSnapping(geom.Coord{lon, lat})
if err != nil {
if errors.Is(err, rgeo.ErrLocationNotFound) {
c.JSON(http.StatusOK, nominatimErrorResponse{Error: "Unable to geocode"})
return
}
c.JSON(http.StatusInternalServerError, nominatimErrorResponse{Error: "internal geocoding error"})
return
}
c.JSON(http.StatusOK, buildReverseResponse(loc, lat, lon, zoom))
}
}
func toLower(s string) string {
b := []byte(s)
for i, c := range b {
if c >= 'A' && c <= 'Z' {
b[i] = c + ('a' - 'A')
}
}
return string(b)
}
func envOr(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
// runHealthcheck performs a plain HTTP GET against /health on addr and
// returns a process exit code. It exists so a scratch-based container
// (no shell, no curl/wget) can still run `HEALTHCHECK CMD ["/server", "-healthcheck"]`
// by invoking the same static binary with a different flag.
func runHealthcheck(addr string) int {
host := addr
if len(host) > 0 && host[0] == ':' {
host = "127.0.0.1" + host
}
client := http.Client{Timeout: 3 * time.Second}
resp, err := client.Get(fmt.Sprintf("http://%s/health", host))
if err != nil {
fmt.Fprintln(os.Stderr, "healthcheck failed:", err)
return 1
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
if resp.StatusCode != http.StatusOK {
fmt.Fprintln(os.Stderr, "healthcheck failed: status", resp.StatusCode)
return 1
}
return 0
}