Skip to content

Commit

Permalink
issue #2 - basic working runtime uris.js; still need to wrangle custo…
Browse files Browse the repository at this point in the history
…m (un)marshaling for Root element
  • Loading branch information
sfomuseumbot committed May 23, 2024
1 parent 6db07c7 commit e9e8fda
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/server/handlers_www.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/sfomuseum/go-http-opensearch"
opensearch_http "github.com/sfomuseum/go-http-opensearch/http"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd/templates/javascript"
"github.com/whosonfirst/go-whosonfirst-spelunker-httpd/www"
)

Expand All @@ -19,6 +20,29 @@ func staticHandlerFunc(ctx context.Context) (http.Handler, error) {
return http.StripPrefix(run_options.URIs.Static, fs_handler), nil
}

func urisJSHandlerFunc(ctx context.Context) (http.Handler, error) {

setupWWWOnce.Do(setupWWW)

if setupWWWError != nil {
slog.Error("Failed to set up common configuration", "error", setupWWWError)
return nil, fmt.Errorf("Failed to set up common configuration, %w", setupWWWError)
}

js_templates, err := javascript.LoadTemplates(ctx)

if err != nil {
return nil, fmt.Errorf("Failed to load JavaScript templates, %w", err)
}

opts := &www.URIsJSHandlerOptions{
Templates: js_templates,
URIs: uris_table,
}

return www.URIsJSHandler(opts)
}

func openSearchHandlerFunc(ctx context.Context) (http.Handler, error) {

setupWWWOnce.Do(setupWWW)
Expand Down
2 changes: 2 additions & 0 deletions app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ 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,

// API/machine-readable
run_options.URIs.ConcordanceNSFaceted: hasConcordanceFacetedHandlerFunc,
Expand Down
22 changes: 22 additions & 0 deletions templates/javascript/whosonfirst.spelunker.uris.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{ define "whosonfirst_spelunker_uris" -}}
var whosonfirst = whosonfirst || {};
whosonfirst.spelunker = whosonfirst.spelunker || {};

whosonfirst.spelunker.uris = (function(){

var _table = {{ .Table }};

var self = {

abs_root_url: function(){
return "/";
},

table: function(){
return _table;
},
};

return self;
})();
{{ end -}}
2 changes: 1 addition & 1 deletion uris.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type URIs struct {
SVG string `json:"svg"`
SVGAlt []string `json:"svg_alt"`

Root *url.URL
Root *url.URL `json:"root_url"`
}

func (u *URIs) ApplyPrefix(prefix string) error {
Expand Down
60 changes: 60 additions & 0 deletions www/uris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package www

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"text/template"

"github.com/whosonfirst/go-whosonfirst-spelunker-httpd"
)

type URIsJSHandlerOptions struct {
Templates *template.Template
URIs *httpd.URIs
}

type URIsJSVars struct {
Table string
}

func URIsJSHandler(opts *URIsJSHandlerOptions) (http.Handler, error) {

t := opts.Templates.Lookup("whosonfirst_spelunker_uris")

if t == nil {
return nil, fmt.Errorf("Failed to locate 'whosonfirst_spelunker_uris' template")
}

fn := func(rsp http.ResponseWriter, req *http.Request) {

logger := slog.Default()
logger = logger.With("request", req.URL)

enc_table, err := json.Marshal(opts.URIs)

if err != nil {
logger.Error("Failed to marshal URIs table", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}

vars := URIsJSVars{
Table: string(enc_table),
}

rsp.Header().Set("Content-type", "text/javascript")
err = t.Execute(rsp, vars)

if err != nil {
logger.Error("Failed to execute template", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}

return
}

return http.HandlerFunc(fn), nil
}

0 comments on commit e9e8fda

Please sign in to comment.