-
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.
issue #2 - basic working runtime uris.js; still need to wrangle custo…
…m (un)marshaling for Root element
- Loading branch information
sfomuseumbot
committed
May 23, 2024
1 parent
6db07c7
commit e9e8fda
Showing
5 changed files
with
109 additions
and
1 deletion.
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
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 -}} |
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 | ||
} |