-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson.go
51 lines (36 loc) · 1.12 KB
/
geojson.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
package api
import (
"log/slog"
"net/http"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
)
type GeoJSONHandlerOptions struct {
Spelunker spelunker.Spelunker
}
func GeoJSONHandler(opts *GeoJSONHandlerOptions) (http.Handler, error) {
logger := slog.Default()
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger = logger.With("request", req.URL)
logger = logger.With("address", req.RemoteAddr)
req_uri, err, status := httpd.ParseURIFromRequest(req, nil)
if err != nil {
slog.Error("Failed to parse URI from request", "error", err)
http.Error(rsp, spelunker.ErrNotFound.Error(), status)
return
}
wof_id := req_uri.Id
logger = logger.With("wof id", wof_id)
r, err := httpd.FeatureFromRequestURI(ctx, opts.Spelunker, req_uri)
if err != nil {
slog.Error("Failed to get by ID", "id", wof_id, "error", err)
http.Error(rsp, spelunker.ErrNotFound.Error(), http.StatusNotFound)
return
}
rsp.Header().Set("Content-Type", "application/json")
rsp.Write(r)
}
h := http.HandlerFunc(fn)
return h, nil
}