This repository has been archived by the owner on May 16, 2024. It is now read-only.
/
id.go
181 lines (137 loc) · 3.3 KB
/
id.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
177
178
179
180
181
package www
import (
"errors"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-uri"
"html/template"
"log"
"net/http"
"path/filepath"
"strconv"
"time"
)
type IDHandlerOptions struct {
Templates *template.Template
Endpoints *Endpoints
}
type IDVars struct {
SPR spr.StandardPlacesResult
URI string
URIArgs *uri.URIArgs
IsAlternate bool
LastModified string
Endpoints *Endpoints
}
func IDHandler(r reader.Reader, opts IDHandlerOptions) (http.Handler, error) {
id_t := opts.Templates.Lookup("id")
if id_t == nil {
return nil, errors.New("Missing id template")
}
alt_t := opts.Templates.Lookup("alt")
if alt_t == nil {
return nil, errors.New("Missing alt template")
}
error_t := opts.Templates.Lookup("error")
if error_t == nil {
return nil, errors.New("Missing error template")
}
notfound_t := opts.Templates.Lookup("id")
if notfound_t == nil {
return nil, errors.New("Missing notfound template")
}
handle_other := func(rsp http.ResponseWriter, req *http.Request, f []byte, endpoint string) {
if endpoint == "" {
vars := NotFoundVars{
Endpoints: opts.Endpoints,
}
RenderTemplate(rsp, notfound_t, vars)
return
}
id, err := properties.Id(f)
if err != nil {
vars := ErrorVars{
Error: err,
Endpoints: opts.Endpoints,
// status...
}
RenderTemplate(rsp, error_t, vars)
}
str_id := strconv.FormatInt(id, 10)
url := filepath.Join(endpoint, str_id)
http.Redirect(rsp, req, url, http.StatusMovedPermanently)
return
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
uri, err, _ := ParseURIFromRequest(req, r)
if err != nil {
log.Printf("Failed to parse URI from request %s, %v", req.URL, err)
vars := ErrorVars{
Error: err,
Endpoints: opts.Endpoints,
// status...
}
RenderTemplate(rsp, error_t, vars)
return
}
f := uri.Feature
path := req.URL.Path
ext := filepath.Ext(path)
switch ext {
case ".geojson":
handle_other(rsp, req, f, opts.Endpoints.Data)
return
case ".png":
handle_other(rsp, req, f, opts.Endpoints.Png)
return
case ".spr":
handle_other(rsp, req, f, opts.Endpoints.Spr)
return
case ".svg":
handle_other(rsp, req, f, opts.Endpoints.Svg)
return
default:
// pass
}
s, err := spr.WhosOnFirstSPR(f)
if err != nil {
vars := ErrorVars{
Error: err,
Endpoints: opts.Endpoints,
}
RenderTemplate(rsp, error_t, vars)
return
}
now := time.Now()
lastmod := now.Format(time.RFC3339)
/*
data_url := new(url.URL)
data_url.Scheme = req.URL.Scheme
data_url.Host = req.URL.Host
data_url.Path = opts.Endpoints.Data
data_endpoint := data_url.String()
png_url := new(url.URL)
png_url.Scheme = req.URL.Scheme
png_url.Host = req.URL.Host
png_url.Path = opts.Endpoints.Data
png_endpoint := png_url.String()
*/
vars := IDVars{
SPR: s,
URI: uri.URI,
URIArgs: uri.URIArgs,
IsAlternate: uri.IsAlternate,
LastModified: lastmod,
Endpoints: opts.Endpoints,
}
t := id_t
if uri.IsAlternate {
t = alt_t
}
RenderTemplate(rsp, t, vars)
return
}
h := http.HandlerFunc(fn)
return h, nil
}