-
Notifications
You must be signed in to change notification settings - Fork 0
/
findingaid.go
50 lines (35 loc) · 1.09 KB
/
findingaid.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
package api
import (
"log/slog"
"net/http"
"github.com/whosonfirst/go-whosonfirst-spelunker"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
)
type FindingAidHandlerOptions struct {
Spelunker spelunker.Spelunker
}
func FindingAidHandler(opts *FindingAidHandlerOptions) (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
}
spr, err := httpd.SPRFromRequestURI(ctx, opts.Spelunker, req_uri)
if err != nil {
slog.Error("Failed to get by ID", "id", req_uri.Id, "error", err)
http.Error(rsp, spelunker.ErrNotFound.Error(), http.StatusNotFound)
return
}
repo := spr.Repo()
rsp.Header().Set("Content-Type", "text/plain")
rsp.Write([]byte(repo))
}
h := http.HandlerFunc(fn)
return h, nil
}