Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1c90d91

Browse files
author
Craig Furman
authored
chore(appliance): extract layout template (#63467)
To avoid duplication of common elements from page to page. In preparation for introducing some more pages, it made sense to extract a layout. Relates to https://linear.app/sourcegraph/issue/REL-20/maintenance-ui-admin-must-configure-initial-password-on-first-boot but does not close it.
1 parent 116d9d7 commit 1c90d91

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

internal/appliance/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
"web/static/css/bootstrap.min.css",
1919
"web/static/css/custom.css",
2020
"web/static/script/bootstrap.bundle.min.js",
21+
"web/template/layout.gohtml",
2122
],
2223
importpath = "github.com/sourcegraph/sourcegraph/internal/appliance",
2324
visibility = ["//:__subpackages__"],

internal/appliance/html.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@ import (
44
"context"
55
"fmt"
66
"html/template"
7+
"io"
78
"net/http"
9+
"path/filepath"
810

911
"github.com/life4/genesis/slices"
1012

1113
"github.com/sourcegraph/log"
1214

1315
"github.com/sourcegraph/sourcegraph/internal/appliance/config"
1416
"github.com/sourcegraph/sourcegraph/internal/releaseregistry"
17+
"github.com/sourcegraph/sourcegraph/lib/errors"
1518
)
1619

1720
const (
18-
templateSetup = "web/template/setup.gohtml"
19-
formValueOn = "on"
21+
formValueOn = "on"
2022
)
2123

22-
var (
23-
setupTmpl *template.Template
24-
)
25-
26-
func init() {
27-
setupTmpl = template.Must(template.ParseFS(templateFS, templateSetup))
24+
func templatePath(name string) string {
25+
return filepath.Join("web", "template", name+".gohtml")
2826
}
2927

3028
func (a *Appliance) applianceHandler(w http.ResponseWriter, r *http.Request) {
@@ -33,6 +31,14 @@ func (a *Appliance) applianceHandler(w http.ResponseWriter, r *http.Request) {
3331
}
3432
}
3533

34+
func renderTemplate(name string, w io.Writer, data any) error {
35+
tmpl, err := template.ParseFS(templateFS, templatePath("layout"), templatePath(name))
36+
if err != nil {
37+
return errors.Wrapf(err, "rendering template: %s", name)
38+
}
39+
return tmpl.Execute(w, data)
40+
}
41+
3642
func (a *Appliance) getSetupHandler(w http.ResponseWriter, r *http.Request) {
3743
versions, err := a.getVersions(r.Context())
3844
if err != nil {
@@ -45,7 +51,7 @@ func (a *Appliance) getSetupHandler(w http.ResponseWriter, r *http.Request) {
4551
return
4652
}
4753

48-
err = setupTmpl.Execute(w, struct {
54+
err = renderTemplate("setup", w, struct {
4955
Versions []string
5056
}{
5157
Versions: versions,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="icon" type="image/svg+xml" href="/static/img/favicon.png" />
7+
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
8+
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
9+
<script src="/static/script/htmx.min.js"></script>
10+
<script src="/static/script/bootstrap.bundle.min.js"></script>
11+
<title>{{ block "title" . }}{{ end }}</title>
12+
</head>
13+
14+
<body hx-boost="true" class="container">
15+
{{ block "content" . }}{{ end }}
16+
</body>
17+
</html>

internal/appliance/web/template/setup.gohtml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<link rel="icon" type="image/svg+xml" href="/static/img/favicon.png" />
7-
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
8-
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
9-
<script src="/static/script/htmx.min.js"></script>
10-
<script src="/static/script/bootstrap.bundle.min.js"></script>
11-
<title>Sourcegraph Appliance - Setup</title>
12-
</head>
1+
{{ template "layout.gohtml" }}
132

14-
<body hx-boost="true" class="container">
3+
{{ define "title" }}Sourcegraph Appliance - Setup{{ end }}
4+
5+
{{- define "content" }}
156
<h1>Sourcegraph Appliance Setup</h1>
167

178
<form action="/appliance/setup" method="post">
@@ -155,6 +146,4 @@
155146
</div>
156147
<button type="submit" class="btn btn-primary">Start Setup</button>
157148
</form>
158-
159-
</body>
160-
</html>
149+
{{- end }}

0 commit comments

Comments
 (0)