/
nullisland.go
103 lines (78 loc) · 2.62 KB
/
nullisland.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
package www
import (
"fmt"
"html/template"
"log/slog"
"net/http"
"github.com/aaronland/go-pagination"
"github.com/sfomuseum/go-http-auth"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
)
type NullIslandHandlerOptions struct {
Spelunker spelunker.Spelunker
Authenticator auth.Authenticator
Templates *template.Template
URIs *httpd.URIs
}
type NullIslandHandlerVars struct {
PageTitle string
URIs *httpd.URIs
Places []spr.StandardPlacesResult
Pagination pagination.Results
PaginationURL string
FacetsURL string
FacetsContextURL string
}
func NullIslandHandler(opts *NullIslandHandlerOptions) (http.Handler, error) {
t := opts.Templates.Lookup("nullisland")
if t == nil {
return nil, fmt.Errorf("Failed to locate 'recent' template")
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger := slog.Default()
logger = logger.With("request", req.URL)
pg_opts, err := httpd.PaginationOptionsFromRequest(req)
if err != nil {
logger.Error("Failed to create pagination options", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
filter_params := httpd.DefaultFilterParams()
filters, err := httpd.FiltersFromRequest(ctx, req, filter_params)
if err != nil {
logger.Error("Failed to derive filters from request", "error", err)
http.Error(rsp, "Bad request", http.StatusBadRequest)
return
}
r, pg_r, err := opts.Spelunker.VisitingNullIsland(ctx, pg_opts, filters)
if err != nil {
logger.Error("Failed to get recent", "error", err)
http.Error(rsp, "InternalServerError", http.StatusInternalServerError)
return
}
// This is not ideal but I am not sure what is better yet...
pagination_url := httpd.URIForNullIsland(opts.URIs.NullIsland, filters, nil)
// This is not ideal but I am not sure what is better yet...
facets_url := httpd.URIForNullIsland(opts.URIs.NullIslandFaceted, filters, nil)
facets_context_url := pagination_url
vars := NullIslandHandlerVars{
Places: r.Results(),
Pagination: pg_r,
URIs: opts.URIs,
PaginationURL: pagination_url,
FacetsURL: facets_url,
FacetsContextURL: facets_context_url,
}
rsp.Header().Set("Content-Type", "text/html")
err = t.Execute(rsp, vars)
if err != nil {
logger.Error("Failed to return ", "error", err)
http.Error(rsp, "InternalServerError", http.StatusInternalServerError)
}
}
h := http.HandlerFunc(fn)
return h, nil
}