Skip to content

Commit 57f0114

Browse files
committed
Make the home page work for all base paths.
This sets a global variable, bt.basePath, for use in JavaScript. The other global variable, in the Go code, is now in the templates package. For #41
1 parent 50d0b75 commit 57f0114

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

bulktracker.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ import (
4343
"github.com/bsiegert/BulkTracker/json"
4444
"github.com/bsiegert/BulkTracker/log"
4545
"github.com/bsiegert/BulkTracker/pages"
46+
"github.com/bsiegert/BulkTracker/templates"
4647
)
4748

4849
var (
4950
port = flag.Int("port", 8080, "The port to use.")
5051
metricsAddr = flag.String("metrics_addr", "", "host:port for serving Prometheus metrics, or 'main' to serve them on the main port")
5152
dbPath = flag.String("db_path", "BulkTracker.db", "The path to the SQLite database file.")
52-
basePath = flag.String("base_path", "/", "The path under which to serve the UI, e.g. '/bulktracker/'.")
5353
)
5454

55+
func init() {
56+
flag.StringVar(&templates.BasePath, "base_path", "/", "The path under which to serve the UI, e.g. '/bulktracker/'.")
57+
}
58+
5559
//go:embed images mock static robots.txt
5660
var staticContent embed.FS
5761

@@ -183,10 +187,10 @@ func main() {
183187
}
184188

185189
log.Infof(ctx, "Listening on port %d", *port)
186-
if *basePath != "/" {
187-
http.Handle("/", http.RedirectHandler(*basePath, http.StatusSeeOther))
190+
if templates.BasePath != "/" {
191+
http.Handle("/", http.RedirectHandler(templates.BasePath, http.StatusSeeOther))
188192
}
189-
http.Handle(*basePath, http.StripPrefix(strings.TrimRight(*basePath, "/"), mux))
193+
http.Handle(templates.BasePath, http.StripPrefix(strings.TrimRight(templates.BasePath, "/"), mux))
190194
err = http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
191195
if err != nil {
192196
log.Errorf(ctx, "%s", err)

static/select-pkgresult.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
$(document).ready(function() {
22
$('#pkgresults').submit(function() {
33
var pkg = encodeURIComponent($('select#results-pkg').val()).replace(/%2F/gi, "/");
4-
$(location).attr('href', `/${pkg}`);
4+
$(location).attr('href', `${bt.basePath}${pkg}`);
55
return false;
66
});
77

88
$('#results-pkg').select2({
99
theme: 'bootstrap',
1010
tags: true,
1111
ajax: {
12-
url: '/json/autocomplete/',
12+
url: bt.basePath+'json/autocomplete/',
1313
dataType: 'json'
1414
}
1515
});

templates/header.html

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
<title>BulkTracker</title>
99

10-
<link href="/static/bootstrap.min.css" rel="stylesheet">
11-
<link href="/static/dataTables.bootstrap.css" rel="stylesheet">
12-
<link href="/static/select2.min.css" rel="stylesheet">
13-
<link href="/static/select2-bootstrap.min.css" rel="stylesheet">
10+
<link href="{{.BasePath}}static/bootstrap.min.css" rel="stylesheet">
11+
<link href="{{.BasePath}}static/dataTables.bootstrap.css" rel="stylesheet">
12+
<link href="{{.BasePath}}static/select2.min.css" rel="stylesheet">
13+
<link href="{{.BasePath}}static/select2-bootstrap.min.css" rel="stylesheet">
1414
<style type="text/css">
1515
.column-item {
1616
width: 16em;
@@ -21,18 +21,22 @@
2121
</style>
2222
</head>
2323
<body>
24-
<script src="/static/jquery.min.js"></script>
25-
<script src="/static/bootstrap.min.js"></script>
26-
<script src="/static/jquery.dataTables.min.js"></script>
27-
<script src="/static/dataTables.bootstrap.js"></script>
28-
<script src="/static/select2.min.js"></script>
24+
<script>
25+
var bt = bt || {};
26+
bt.basePath = "{{.BasePath}}";
27+
</script>
28+
<script src="{{.BasePath}}static/jquery.min.js"></script>
29+
<script src="{{.BasePath}}static/bootstrap.min.js"></script>
30+
<script src="{{.BasePath}}static/jquery.dataTables.min.js"></script>
31+
<script src="{{.BasePath}}static/dataTables.bootstrap.js"></script>
32+
<script src="{{.BasePath}}static/select2.min.js"></script>
2933
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
3034
<div style="background:#F26711; padding: 20px; margin-bottom: 20px">
3135
<div class="pull-left" style="padding-right: 20px">
32-
<a href="/"><img src="/static/pkgsrc-white.png" width="64px" height="64px"></a>
36+
<a href="{{.BasePath}}"><img src="{{.BasePath}}static/pkgsrc-white.png" width="64px" height="64px"></a>
3337
</div>
3438
<h1>
35-
<a href="/" style="color: white">
39+
<a href="{{.BasePath}}" style="color: white">
3640
BulkTracker
3741
<small style="color: white">pkgsrc bulk build status</small>
3842
</a>

templates/start_page_lead.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</p>
1212
</div>
1313

14-
<script type="text/javascript" src="/static/select-pkgresult.js"></script>
14+
<script type="text/javascript" src="{{.BasePath}}static/select-pkgresult.js"></script>
1515

1616
<h2>Package Results</h2>
1717

templates/start_page_lead2.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
</div><div class="row">
22

3-
<h2>Latest Builds per Platform&nbsp; <a href="/builds" class="btn btn-primary">Show all</a></h2>
3+
<h2>Latest Builds per Platform&nbsp; <a href="builds" class="btn btn-primary">Show all</a></h2>
44

templates/templates.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,27 @@ import (
3232
//go:embed *.html
3333
var emb embed.FS
3434

35+
// BasePath is the path at which the UI is served.
36+
var BasePath = "/"
37+
38+
// bp is a shim type that avoids allocations in common templates.
39+
type bp struct{}
40+
41+
func (bp) BasePath() string { return BasePath }
42+
3543
// t is the top-level template object.
3644
var t = template.Must(template.ParseFS(emb, "*.html"))
3745

3846
func PageHeader(w io.Writer) {
39-
t.ExecuteTemplate(w, "header.html", nil)
47+
t.ExecuteTemplate(w, "header.html", bp{})
4048
}
4149

4250
func PageFooter(w io.Writer) {
4351
t.ExecuteTemplate(w, "footer.html", nil)
4452
}
4553

4654
func StartPageLead(w io.Writer) {
47-
t.ExecuteTemplate(w, "start_page_lead.html", nil)
55+
t.ExecuteTemplate(w, "start_page_lead.html", bp{})
4856
}
4957

5058
func StartPageLead2(w io.Writer) {

0 commit comments

Comments
 (0)