/
concordance.go
176 lines (138 loc) · 5.18 KB
/
concordance.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package www
import (
"fmt"
"html/template"
"log/slog"
"net/http"
"strings"
"github.com/aaronland/go-pagination"
"github.com/sfomuseum/go-http-auth"
"github.com/whosonfirst/go-whosonfirst-sources"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
)
type HasConcordanceHandlerOptions struct {
Spelunker spelunker.Spelunker
Authenticator auth.Authenticator
Templates *template.Template
URIs *httpd.URIs
}
type HasConcordanceHandlerVars struct {
PageTitle string
URIs *httpd.URIs
Concordance *spelunker.Concordance
Places []spr.StandardPlacesResult
Pagination pagination.Results
PaginationURL string
FacetsURL string
FacetsContextURL string
Source *sources.WOFSource
}
func HasConcordanceHandler(opts *HasConcordanceHandlerOptions) (http.Handler, error) {
t := opts.Templates.Lookup("concordance")
if t == nil {
return nil, fmt.Errorf("Failed to locate 'concordance' template")
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger := slog.Default()
logger = logger.With("request", req.URL)
ns := req.PathValue("namespace")
pred := req.PathValue("predicate")
value := req.PathValue("value")
ns = strings.TrimRight(ns, ":")
pred = strings.TrimLeft(pred, ":")
pred = strings.TrimRight(pred, "=")
logger = logger.With("namespace", ns)
logger = logger.With("predicate", pred)
logger = logger.With("value", value)
if ns == "*" {
ns = ""
}
if pred == "*" {
pred = ""
}
if value == "*" {
value = ""
}
c := spelunker.NewConcordanceFromTriple(ns, pred, value)
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.HasConcordance(ctx, pg_opts, ns, pred, value, filters)
if err != nil {
logger.Error("Failed to get records having concordance", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
page_title := fmt.Sprintf("Concordances for %s", c)
var src *sources.WOFSource
if ns != "" {
v, err := sources.GetSourceByPrefix(ns)
if err != nil {
logger.Warn("Failed to derive source from namespace", "error", err)
} else {
src = v
}
}
var pagination_url string
var facets_url string
var facets_context_url string
switch {
case ns != "" && pred != "" && value != "":
pagination_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTriple, ns, pred, value, filters, nil)
facets_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTripleFaceted, ns, pred, value, filters, nil)
case ns != "" && pred != "":
pagination_url = httpd.URIForConcordanceNSPred(opts.URIs.ConcordanceNSPred, ns, pred, filters, nil)
facets_url = httpd.URIForConcordanceNSPred(opts.URIs.ConcordanceNSPredFaceted, ns, pred, filters, nil)
case pred != "" && value != "":
pagination_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTriple, "*", pred, value, filters, nil)
facets_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTripleFaceted, "*", pred, value, filters, nil)
case ns != "" && value != "":
pagination_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTriple, ns, "*", value, filters, nil)
facets_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTripleFaceted, ns, "*", value, filters, nil)
case ns != "":
pagination_url = httpd.URIForConcordanceNS(opts.URIs.ConcordanceNS, ns, filters, nil)
facets_url = httpd.URIForConcordanceNS(opts.URIs.ConcordanceNSFaceted, ns, filters, nil)
case pred != "":
pagination_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTriple, "*", pred, "*", filters, nil)
facets_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTripleFaceted, "*", pred, "*", filters, nil)
case value != "":
pagination_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTriple, "*", "*", value, filters, nil)
facets_url = httpd.URIForConcordanceTriple(opts.URIs.ConcordanceTripleFaceted, "*", "*", value, filters, nil)
default:
logger.Info("WUT")
}
facets_context_url = req.URL.Path
vars := HasConcordanceHandlerVars{
PageTitle: page_title,
URIs: opts.URIs,
Concordance: c,
Places: r.Results(),
Pagination: pg_r,
Source: src,
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 render template", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
}
}
h := http.HandlerFunc(fn)
return h, nil
}