/
recent_faceted.go
109 lines (81 loc) · 2.59 KB
/
recent_faceted.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
package api
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"regexp"
// "github.com/sfomuseum/go-http-auth"
"github.com/sfomuseum/iso8601duration"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
)
type RecentFacetedHandlerOptions struct {
Spelunker spelunker.Spelunker
// Authenticator auth.Authenticator
}
func RecentFacetedHandler(opts *RecentFacetedHandlerOptions) (http.Handler, error) {
re_full, err := regexp.Compile(`P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?`)
if err != nil {
return nil, fmt.Errorf("Failed to compile ISO8601 duration pattern, %w", err)
}
re_week, err := regexp.Compile(`P((?P<week>\d+)W)`)
if err != nil {
return nil, fmt.Errorf("Failed to compile ISO8601 duration week pattern, %w", err)
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger := slog.Default()
logger = logger.With("request", req.URL)
str_d := req.PathValue("duration")
switch {
case re_week.MatchString(str_d):
// ok
case re_full.MatchString(str_d):
// ok
default:
str_d = "P30D"
}
logger = logger.With("duration", str_d)
d, err := duration.FromString(str_d)
if err != nil {
logger.Error("Failed to parse duration", "error", err)
http.Error(rsp, "Bad request", http.StatusBadRequest)
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
}
facets, err := httpd.FacetsFromRequest(ctx, req, filter_params)
if err != nil {
logger.Error("Failed to derive facets from requrst", "error", err)
http.Error(rsp, "Bad request", http.StatusBadRequest)
return
}
if len(facets) == 0 {
logger.Error("No facets from requrst")
http.Error(rsp, "Bad request", http.StatusBadRequest)
return
}
facets_rsp, err := opts.Spelunker.GetRecentFaceted(ctx, d.ToDuration(), filters, facets)
if err != nil {
logger.Error("Failed to get recent", "error", err)
http.Error(rsp, "womp womp", http.StatusInternalServerError)
return
}
rsp.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(rsp)
err = enc.Encode(facets_rsp)
if err != nil {
logger.Error("Failed to encode facets response", "error", err)
http.Error(rsp, "womp womp", http.StatusInternalServerError)
return
}
}
h := http.HandlerFunc(fn)
return h, nil
}