Skip to content

Commit 71ccb32

Browse files
authored
Include base path in static resource URLs (#260)
1 parent 7c3cb5e commit 71ccb32

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

server/handler/frontend.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package handler
1717
import (
1818
"html/template"
1919
"net/http"
20+
"path"
2021
"sort"
2122
"strings"
2223

@@ -35,8 +36,15 @@ type FilesConfig struct {
3536
Templates string `yaml:"templates"`
3637
}
3738

38-
func LoadTemplates(c *FilesConfig) (templatetree.HTMLTree, error) {
39+
func LoadTemplates(c *FilesConfig, basePath string) (templatetree.HTMLTree, error) {
40+
if basePath == "" {
41+
basePath = "/"
42+
}
43+
3944
root := template.New("root").Funcs(template.FuncMap{
45+
"resource": func(r string) string {
46+
return path.Join(basePath, "static", r)
47+
},
4048
"titlecase": strings.Title,
4149
"sortByStatus": func(results []*common.Result) []*common.Result {
4250
r := make([]*common.Result, len(results))

server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func New(c *Config) (*Server, error) {
161161
),
162162
)
163163

164-
templates, err := handler.LoadTemplates(&c.Files)
164+
templates, err := handler.LoadTemplates(&c.Files, basePath)
165165
if err != nil {
166166
return nil, errors.Wrap(err, "failed to load templates")
167167
}

server/templates/details.html.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{define "title"}}{{.PullRequest.GetBase.GetRepo.GetFullName}}#{{.PullRequest.GetNumber}} - Details | PolicyBot{{end}}
33

44
{{define "scripts"}}
5-
<script defer src="/static/js/filter.js"></script>
5+
<script defer src="{{ resource "js/filter.js" }}"></script>
66
{{end}}
77

88
{{define "body-class"}}bg-light-gray5 text-dark-gray1 flex flex-col h-screen{{end}}

server/templates/page.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
<title>{{block "title" .}}PolicyBot{{end}}</title>
88

9-
<link rel="icon" href="/static/img/favicon.ico" />
10-
<link rel="stylesheet" href="/static/css/main.css">
9+
<link rel="icon" href="{{ resource "img/favicon.ico" }}" />
10+
<link rel="stylesheet" href="{{ resource "css/main.css" }}">
1111
{{block "scripts" .}}{{end}}
1212
</head>
1313
<body class="{{block "body-class" .}}bg-light-gray5 text-dark-gray1{{end}}">

0 commit comments

Comments
 (0)