-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive whosonfirst.spelunker.uris.js at runtime (#38)
* issue #2 - basic working runtime uris.js; still need to wrangle custom (un)marshaling for Root element * issue #2 – make runtime whosonfirst.spelunker.uris.js work --------- Co-authored-by: sfomuseumbot <sfomuseumbot@localhost>
- Loading branch information
1 parent
6db07c7
commit ce3f920
Showing
7 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{{ define "whosonfirst_spelunker_uris" -}} | ||
var whosonfirst = whosonfirst || {}; | ||
whosonfirst.spelunker = whosonfirst.spelunker || {}; | ||
|
||
whosonfirst.spelunker.uris = (function(){ | ||
|
||
var _table = {{ .Table }}; | ||
|
||
var self = { | ||
|
||
abs_root_url: function(){ | ||
|
||
var root = _table.root_url; | ||
|
||
if (! root.endsWith("/")){ | ||
root += "/"; | ||
} | ||
|
||
return root; | ||
}, | ||
|
||
table: function(){ | ||
return _table; | ||
}, | ||
}; | ||
|
||
return self; | ||
})(); | ||
{{ end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |