-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotomaps.go
292 lines (211 loc) · 7.77 KB
/
protomaps.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package protomaps
import (
"fmt"
"io/fs"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/aaronland/go-http-leaflet"
aa_static "github.com/aaronland/go-http-static"
"github.com/sfomuseum/go-http-protomaps/static"
"github.com/sfomuseum/go-http-rollup"
)
// ProtomapsOptions provides a list of JavaScript and CSS link to include with HTML output as well as a URL referencing a specific Protomaps PMTiles database to include a data attribute.
type ProtomapsOptions struct {
// A list of relative JavaScript files to reference in one or more <script> tags
JS []string
// A list of relative CSS files to reference in one or more <link rel="stylesheet"> tags
CSS []string
// A URL for a specific PMTiles database to include as a 'data-protomaps-tile-url' attribute on the <body> tag.
TileURL string
// A leaflet.LeafletOptions struct
LeafletOptions *leaflet.LeafletOptions
// AppendJavaScriptAtEOF is a boolean flag to append JavaScript markup at the end of an HTML document
// rather than in the <head> HTML element. Default is false
AppendJavaScriptAtEOF bool
// Rollup (minify and bundle) JavaScript and CSS assets.
RollupAssets bool
Prefix string
// By default the go-http-protomaps package will also include and reference Leaflet.js resources using the aaronland/go-http-leaflet package. If you want or need to disable this behaviour set this variable to false.
AppendLeafletResources bool
// By default the go-http-protomaps package will also include and reference Leaflet.js assets using the aaronland/go-http-leaflet package. If you want or need to disable this behaviour set this variable to false.
AppendLeafletAssets bool
}
// Return a *ProtomapsOptions struct with default paths and URIs.
func DefaultProtomapsOptions() *ProtomapsOptions {
leaflet_opts := leaflet.DefaultLeafletOptions()
opts := &ProtomapsOptions{
CSS: []string{},
JS: []string{
"/javascript/protomaps.min.js",
},
LeafletOptions: leaflet_opts,
AppendLeafletResources: true,
AppendLeafletAssets: true,
}
return opts
}
// AppendResourcesHandlerWithPrefix will rewrite any HTML produced by previous handler to include the necessary markup to load Protomaps JavaScript files and related assets ensuring that all URIs are prepended with a prefix.
func AppendResourcesHandler(next http.Handler, opts *ProtomapsOptions) http.Handler {
if opts.AppendLeafletResources {
opts.LeafletOptions.AppendJavaScriptAtEOF = opts.AppendJavaScriptAtEOF
opts.LeafletOptions.RollupAssets = opts.RollupAssets
opts.LeafletOptions.Prefix = opts.Prefix
next = leaflet.AppendResourcesHandler(next, opts.LeafletOptions)
}
static_opts := aa_static.DefaultResourcesOptions()
static_opts.DataAttributes["protomaps-tile-url"] = opts.TileURL
static_opts.AppendJavaScriptAtEOF = opts.AppendJavaScriptAtEOF
js_uris := opts.JS
css_uris := opts.CSS
if opts.RollupAssets {
if len(opts.JS) > 1 {
js_uris = []string{
"/javascript/protomaps.rollup.js",
}
}
if len(opts.CSS) > 1 {
css_uris = []string{
"/css/protomaps.rollup.css",
}
}
}
static_opts.JS = js_uris
static_opts.CSS = css_uris
return aa_static.AppendResourcesHandlerWithPrefix(next, static_opts, opts.Prefix)
}
// Append all the files in the net/http FS instance containing the embedded Protomaps assets to an *http.ServeMux instance ensuring that all URLs are prepended with prefix.
func AppendAssetHandlers(mux *http.ServeMux, opts *ProtomapsOptions) error {
if opts.AppendLeafletAssets {
opts.LeafletOptions.AppendJavaScriptAtEOF = opts.AppendJavaScriptAtEOF
opts.LeafletOptions.RollupAssets = opts.RollupAssets
opts.LeafletOptions.Prefix = opts.Prefix
err := leaflet.AppendAssetHandlers(mux, opts.LeafletOptions)
if err != nil {
return fmt.Errorf("Failed to append Leaflet assets, %w", err)
}
}
if !opts.RollupAssets {
return aa_static.AppendStaticAssetHandlersWithPrefix(mux, static.FS, opts.Prefix)
}
// START OF make sure we are still serving bundled Tangram styles
err := serveSubDir(mux, opts, "tangram")
if err != nil {
return fmt.Errorf("Failed to append static asset handler for tangram FS, %w", err)
}
// END OF make sure we are still serving bundled Tangram styles
// START OF this should eventually be made a generic function in go-http-rollup
js_paths := make([]string, len(opts.JS))
css_paths := make([]string, len(opts.CSS))
for idx, path := range opts.JS {
path = strings.TrimLeft(path, "/")
js_paths[idx] = path
}
for idx, path := range opts.CSS {
path = strings.TrimLeft(path, "/")
css_paths[idx] = path
}
switch len(js_paths) {
case 0:
// pass
case 1:
err := serveSubDir(mux, opts, "javascript")
if err != nil {
return fmt.Errorf("Failed to append static asset handler for javascript FS, %w", err)
}
default:
rollup_js_paths := map[string][]string{
"protomaps.rollup.js": js_paths,
}
rollup_js_opts := &rollup.RollupJSHandlerOptions{
FS: static.FS,
Paths: rollup_js_paths,
}
rollup_js_handler, err := rollup.RollupJSHandler(rollup_js_opts)
if err != nil {
return fmt.Errorf("Failed to create rollup JS handler, %w", err)
}
rollup_js_uri := "/javascript/protomaps.rollup.js"
if opts.Prefix != "" {
u, err := url.JoinPath(opts.Prefix, rollup_js_uri)
if err != nil {
return fmt.Errorf("Failed to append prefix to %s, %w", rollup_js_uri, err)
}
rollup_js_uri = u
}
mux.Handle(rollup_js_uri, rollup_js_handler)
}
// CSS
switch len(css_paths) {
case 0:
// pass
case 1:
err := serveSubDir(mux, opts, "css")
if err != nil {
return fmt.Errorf("Failed to append static asset handler for css FS, %w", err)
}
default:
rollup_css_paths := map[string][]string{
"protomaps.rollup.css": css_paths,
}
rollup_css_opts := &rollup.RollupCSSHandlerOptions{
FS: static.FS,
Paths: rollup_css_paths,
}
rollup_css_handler, err := rollup.RollupCSSHandler(rollup_css_opts)
if err != nil {
return fmt.Errorf("Failed to create rollup CSS handler, %w", err)
}
rollup_css_uri := "/css/protomaps.rollup.css"
if opts.Prefix != "" {
u, err := url.JoinPath(opts.Prefix, rollup_css_uri)
if err != nil {
return fmt.Errorf("Failed to append prefix to %s, %w", rollup_css_uri, err)
}
rollup_css_uri = u
}
mux.Handle(rollup_css_uri, rollup_css_handler)
}
// END OF this should eventually be made a generic function in go-http-rollup
return nil
}
func serveSubDir(mux *http.ServeMux, opts *ProtomapsOptions, dirname string) error {
sub_fs, err := fs.Sub(static.FS, dirname)
if err != nil {
return fmt.Errorf("Failed to load %s FS, %w", dirname, err)
}
sub_prefix := dirname
if opts.Prefix != "" {
prefix, err := url.JoinPath(opts.Prefix, sub_prefix)
if err != nil {
return fmt.Errorf("Failed to append prefix to %s, %w", sub_prefix, err)
}
sub_prefix = prefix
}
err = aa_static.AppendStaticAssetHandlersWithPrefix(mux, sub_fs, sub_prefix)
if err != nil {
return fmt.Errorf("Failed to append static asset handler for %s FS, %w", dirname, err)
}
return nil
}
// FileHandlerFromPath will take a path and create a http.FileServer handler
// instance for the files in its root directory. The handler is returned with
// a relative URI for the filename in 'path' to be assigned to a net/http
// ServeMux instance.
func FileHandlerFromPath(path string, prefix string) (string, http.Handler, error) {
abs_path, err := filepath.Abs(path)
if err != nil {
return "", nil, fmt.Errorf("Failed to determine absolute path for '%s', %v", path, err)
}
fname := filepath.Base(abs_path)
root := filepath.Dir(abs_path)
tile_dir := http.Dir(root)
tile_handler := http.FileServer(tile_dir)
tile_url := fmt.Sprintf("/%s", fname)
if prefix != "" {
tile_handler = http.StripPrefix(prefix, tile_handler)
tile_url = filepath.Join(prefix, fname)
}
return tile_url, tile_handler, nil
}