/
main.go
120 lines (83 loc) · 2.77 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/aaronland/go-http-leaflet"
"github.com/aaronland/go-http-leaflet/templates/html"
)
type ExampleVars struct {
TileURL string
EnableHash bool
EnableFullscreen bool
EnableDraw bool
}
func ExampleHandler(templates *template.Template, example_vars *ExampleVars) (http.Handler, error) {
t := templates.Lookup("example")
if t == nil {
return nil, errors.New("Missing 'example' template")
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
err := t.Execute(rsp, example_vars)
if err != nil {
http.Error(rsp, err.Error(), http.StatusInternalServerError)
}
return
}
return http.HandlerFunc(fn), nil
}
func main() {
host := flag.String("host", "localhost", "The host name to listen for requests on.")
port := flag.Int("port", 8080, "The host port to listen for requests on.")
tile_url := flag.String("tile-url", "", "A valid Leaflet layer tile URL")
enable_hash := flag.Bool("enable-hash", false, "Enable the Leaflet Hash plugin")
enable_fullscreen := flag.Bool("enable-fullscreen", false, "Enable the Leaflet Fullscreen plugin")
enable_draw := flag.Bool("enable-draw", false, "Enable the Leaflet Draw plugin")
js_eof := flag.Bool("javascript-at-eof", false, "A boolean flag to include JavaScript resources (<script> tags) at the end of HTML output.")
rollup_assets := flag.Bool("rollup-assets", false, "Rollup (minify and bundle) JavaScript and CSS assets.")
flag.Parse()
logger := log.Default()
t, err := template.ParseFS(html.FS, "*.html")
if err != nil {
log.Fatalf("Failed to parse templates, %v", err)
}
mux := http.NewServeMux()
leaflet_opts := leaflet.DefaultLeafletOptions()
leaflet_opts.AppendJavaScriptAtEOF = *js_eof
leaflet_opts.RollupAssets = *rollup_assets
leaflet_opts.Logger = logger
if *enable_hash {
leaflet_opts.EnableHash()
}
if *enable_fullscreen {
leaflet_opts.EnableFullscreen()
}
if *enable_draw {
leaflet_opts.EnableDraw()
}
err = leaflet.AppendAssetHandlers(mux, leaflet_opts)
if err != nil {
logger.Fatalf("Failed to append Leaflet assets handler, %v", err)
}
example_vars := &ExampleVars{
TileURL: *tile_url,
EnableHash: *enable_hash,
EnableFullscreen: *enable_fullscreen,
EnableDraw: *enable_draw,
}
example_handler, err := ExampleHandler(t, example_vars)
if err != nil {
log.Fatalf("Failed to create example handler, %v", err)
}
example_handler = leaflet.AppendResourcesHandler(example_handler, leaflet_opts)
mux.Handle("/", example_handler)
endpoint := fmt.Sprintf("%s:%d", *host, *port)
log.Printf("Listening for requests on %s\n", endpoint)
err = http.ListenAndServe(endpoint, mux)
if err != nil {
log.Fatalf("Failed to serve requests, %v", err)
}
}