-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
101 lines (73 loc) · 2.13 KB
/
show.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
package show
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"github.com/aaronland/go-http-maps/v2"
"github.com/sfomuseum/go-pmtiles-show/static/www"
www_show "github.com/sfomuseum/go-www-show/v2"
)
func Run(ctx context.Context) error {
fs := DefaultFlagSet()
return RunWithFlagSet(ctx, fs)
}
func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet) error {
opts, err := RunOptionsFromFlagSet(ctx, fs)
if err != nil {
return err
}
return RunWithOptions(ctx, opts)
}
func RunWithOptions(ctx context.Context, opts *RunOptions) error {
if opts.Verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
slog.Debug("Verbose logging enabled")
}
mux := http.NewServeMux()
maps_opts := &maps.AssignMapConfigHandlerOptions{
MapProvider: opts.MapProvider,
MapTileURI: opts.MapTileURI,
InitialView: opts.InitialView,
LeafletStyle: opts.LeafletStyle,
LeafletPointStyle: opts.LeafletPointStyle,
ProtomapsTheme: opts.ProtomapsTheme,
}
err := maps.AssignMapConfigHandler(maps_opts, mux, "/map.json")
if err != nil {
return fmt.Errorf("Failed to assign map config handler, %w", err)
}
//
cfg := &Config{
RasterLayers: make(map[string]string),
VectorLayers: make(map[string]string),
}
for label, path := range opts.RasterLayers {
mux_url, mux_handler, err := maps.ProtomapsFileHandlerFromPath(path, "")
if err != nil {
return fmt.Errorf("Failed to create protomaps handler for %s, %w", path, err)
}
mux.Handle(mux_url, mux_handler)
cfg.RasterLayers[label] = mux_url
}
for label, path := range opts.VectorLayers {
mux_url, mux_handler, err := maps.ProtomapsFileHandlerFromPath(path, "")
if err != nil {
return fmt.Errorf("Failed to create protomaps handler for %s, %w", path, err)
}
mux.Handle(mux_url, mux_handler)
cfg.VectorLayers[label] = mux_url
}
cfg_handler := ConfigHandler(cfg)
mux.Handle("/config.json", cfg_handler)
//
www_fs := http.FS(www.FS)
mux.Handle("/", http.FileServer(www_fs))
www_show_opts := &www_show.RunOptions{
Port: opts.Port,
Browser: opts.Browser,
Mux: mux,
}
return www_show.RunWithOptions(ctx, www_show_opts)
}