diff --git a/app/server/options.go b/app/server/options.go index c04c553..b176110 100644 --- a/app/server/options.go +++ b/app/server/options.go @@ -71,7 +71,7 @@ func RunOptionsFromFlagSet(ctx context.Context, fs *flag.FlagSet) (*RunOptions, } uris_table = httpd.DefaultURIs() - uris_table.Root = root_u + uris_table.RootURL = root_u.String() t_funcs := html_template.FuncMap{ "IsAvailable": sfom_funcs.IsAvailable, diff --git a/app/server/server.go b/app/server/server.go index e3bac58..b07c5b8 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -6,6 +6,7 @@ import ( "fmt" "log/slog" "net/http" + "net/url" "github.com/aaronland/go-http-server" "github.com/aaronland/go-http-server/handler" @@ -47,6 +48,12 @@ func RunWithOptions(ctx context.Context, opts *RunOptions, logger *slog.Logger) // START OF defer loading handlers (and all their dependencies) until they are actually routed to // in case we are running in a "serverless" environment like AWS Lambda + path_urisjs, err := url.JoinPath(run_options.URIs.Static, "/javascript/whosonfirst.spelunker.uris.js") + + if err != nil { + return fmt.Errorf("Failed to construct path for whosonfirst.spelunker.uris.js, %w", err) + } + handlers := map[string]handler.RouteHandlerFunc{ // WWW/human-readable @@ -71,7 +78,7 @@ func RunWithOptions(ctx context.Context, opts *RunOptions, logger *slog.Logger) // Static assets run_options.URIs.Static: staticHandlerFunc, // Run-time static assets - "/javascript/whosonfirst.spelunker.uris.js": urisJSHandlerFunc, + path_urisjs: urisJSHandlerFunc, // API/machine-readable run_options.URIs.ConcordanceNSFaceted: hasConcordanceFacetedHandlerFunc, diff --git a/static/javascript/whosonfirst.spelunker.uris.js b/static/javascript/whosonfirst.spelunker.uris.js deleted file mode 100644 index 8772685..0000000 --- a/static/javascript/whosonfirst.spelunker.uris.js +++ /dev/null @@ -1,13 +0,0 @@ -var whosonfirst = whosonfirst || {}; -whosonfirst.spelunker = whosonfirst.spelunker || {}; - -whosonfirst.spelunker.uris = (function(){ - - var self = { - abs_root_url: function(){ - return "/"; - } - }; - - return self; -})(); diff --git a/templates/javascript/whosonfirst.spelunker.uris.js b/templates/javascript/whosonfirst.spelunker.uris.js index ef22a32..770196e 100644 --- a/templates/javascript/whosonfirst.spelunker.uris.js +++ b/templates/javascript/whosonfirst.spelunker.uris.js @@ -9,7 +9,14 @@ whosonfirst.spelunker.uris = (function(){ var self = { abs_root_url: function(){ - return "/"; + + var root = _table.root_url; + + if (! root.endsWith("/")){ + root += "/"; + } + + return root; }, table: function(){ diff --git a/uris.go b/uris.go index d1801a1..9732a0c 100644 --- a/uris.go +++ b/uris.go @@ -59,7 +59,7 @@ type URIs struct { SVG string `json:"svg"` SVGAlt []string `json:"svg_alt"` - Root *url.URL `json:"root_url"` + RootURL string `json:"root_url"` } func (u *URIs) ApplyPrefix(prefix string) error { @@ -170,16 +170,18 @@ func DefaultURIs() *URIs { func (uris_table *URIs) Abs(path string) (string, error) { - if uris_table.Root == nil { - return "#", fmt.Errorf("Root URL has not been assigned") + root_u, err := url.Parse(uris_table.RootURL) + + if err != nil { + return "", fmt.Errorf("Failed to parse root URL, %w", err) } - u := url.URL{} - u.Host = uris_table.Root.Host - u.Scheme = uris_table.Root.Scheme - u.Path = path + this_u := url.URL{} + this_u.Host = root_u.Host + this_u.Scheme = root_u.Scheme + this_u.Path = path - return u.String(), nil + return this_u.String(), nil } func URIForIdSimple(uri string, id int64) string {