/
leaflet.go
178 lines (133 loc) · 4.44 KB
/
leaflet.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
package leaflet
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"github.com/aaronland/go-http-leaflet/static"
aa_static "github.com/aaronland/go-http-static"
"github.com/sfomuseum/go-http-rollup"
)
var ROLLUP_ASSETS = false
// LeafletOptions provides a list of JavaScript and CSS link to include with HTML output.
type LeafletOptions struct {
JS []string
CSS []string
DataAttributes map[string]string
// 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
RollupAssets bool
Prefix string
Logger *log.Logger
}
// Append the Javascript and CSS URLs for the Leaflet.Fullscreen plugin.
func (opts *LeafletOptions) EnableFullscreen() {
opts.CSS = append(opts.CSS, "/css/leaflet.fullscreen.css")
opts.JS = append(opts.JS, "/javascript/leaflet.fullscreen.min.js")
}
// Append the Javascript and CSS URLs for the Leaflet.Hash plugin.
func (opts *LeafletOptions) EnableHash() {
opts.JS = append(opts.JS, "/javascript/leaflet-hash.js")
}
// Append the Javascript and CSS URLs for the leaflet-geoman plugin.
// https://github.com/geoman-io/leaflet-geoman/
func (opts *LeafletOptions) EnableDraw() {
opts.CSS = append(opts.CSS, "/css/leaflet-geoman.css")
opts.JS = append(opts.JS, "/javascript/leaflet-geoman.min.js")
}
// Return a *LeafletOptions struct with default paths and URIs.
func DefaultLeafletOptions() *LeafletOptions {
logger := log.New(io.Discard, "", 0)
opts := &LeafletOptions{
CSS: []string{
"/css/leaflet.css",
},
JS: []string{
"/javascript/leaflet.js",
},
DataAttributes: make(map[string]string),
Logger: logger,
}
return opts
}
// AppendResourcesHandlerWithPrefix will rewrite any HTML produced by previous handler to include the necessary markup to load Leaflet JavaScript files and related assets ensuring that all URIs are prepended with a prefix.
func AppendResourcesHandler(next http.Handler, opts *LeafletOptions) http.Handler {
static_opts := aa_static.DefaultResourcesOptions()
static_opts.DataAttributes = opts.DataAttributes
static_opts.AppendJavaScriptAtEOF = opts.AppendJavaScriptAtEOF
if opts.RollupAssets {
static_opts.CSS = []string{
"/css/leaflet.rollup.css",
}
static_opts.JS = []string{
"/javascript/leaflet.rollup.js",
}
} else {
static_opts.CSS = opts.CSS
static_opts.JS = opts.JS
}
return aa_static.AppendResourcesHandlerWithPrefix(next, static_opts, opts.Prefix)
}
// Append all the files in the net/http FS instance containing the embedded Leaflet assets to an *http.ServeMux instance.
func AppendAssetHandlers(mux *http.ServeMux, opts *LeafletOptions) error {
if !opts.RollupAssets {
return aa_static.AppendStaticAssetHandlersWithPrefix(mux, static.FS, opts.Prefix)
}
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
}
rollup_js_paths := map[string][]string{
"leaflet.rollup.js": js_paths,
}
rollup_js_opts := &rollup.RollupJSHandlerOptions{
FS: static.FS,
Paths: rollup_js_paths,
Logger: opts.Logger,
}
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/leaflet.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
rollup_css_paths := map[string][]string{
"leaflet.rollup.css": css_paths,
}
rollup_css_opts := &rollup.RollupCSSHandlerOptions{
FS: static.FS,
Paths: rollup_css_paths,
Logger: opts.Logger,
}
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/leaflet.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)
return nil
}