-
Notifications
You must be signed in to change notification settings - Fork 0
/
placetypes.go
69 lines (51 loc) · 1.46 KB
/
placetypes.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
package www
import (
"fmt"
"html/template"
"log/slog"
"net/http"
"github.com/sfomuseum/go-http-auth"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
)
type PlacetypesHandlerOptions struct {
Spelunker spelunker.Spelunker
Authenticator auth.Authenticator
Templates *template.Template
URIs *httpd.URIs
}
type PlacetypesHandlerVars struct {
PageTitle string
URIs *httpd.URIs
Facets []*spelunker.FacetCount
}
func PlacetypesHandler(opts *PlacetypesHandlerOptions) (http.Handler, error) {
t := opts.Templates.Lookup("placetypes")
if t == nil {
return nil, fmt.Errorf("Failed to locate 'placetypes' template")
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger := slog.Default()
logger = logger.With("request", req.URL)
faceting, err := opts.Spelunker.GetPlacetypes(ctx)
if err != nil {
logger.Error("Failed to get placetypes", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
vars := PlacetypesHandlerVars{
PageTitle: "Placetypes",
URIs: opts.URIs,
Facets: faceting.Results,
}
rsp.Header().Set("Content-Type", "text/html")
err = t.Execute(rsp, vars)
if err != nil {
logger.Error("Failed to render template", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
}
}
h := http.HandlerFunc(fn)
return h, nil
}