Skip to content

Commit 3ae884e

Browse files
authored
Merge pull request #37 from johnwmail/dev
Merge pull request #35 from johnwmail/feature/tmpl
2 parents acf70b0 + 08575bc commit 3ae884e

File tree

9 files changed

+47
-15
lines changed

9 files changed

+47
-15
lines changed

.github/workflows/container.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ jobs:
3535
else
3636
VERSION="sha-${GITHUB_SHA::7}"
3737
fi
38-
echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
39-
sed -i "s/__VERSION__/${VERSION}/g" static/index.html
38+
# If using static/index.html replacement, uncomment to replace __VERSION__ placeholder
39+
#echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
40+
#sed -i "s/__VERSION__/${VERSION}/g" static/index.html
4041
4142
- name: Set up QEMU (for multi-arch builds)
4243
uses: docker/setup-qemu-action@v3

.github/workflows/deploy-lambda.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ jobs:
6969
- name: Build Lambda bootstrap
7070
run: |
7171
# Replace __VERSION__ placeholder in index.html for cache busting
72-
echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
73-
sed -i "s/__VERSION__/${VERSION}/g" static/index.html
72+
#echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
73+
#sed -i "s/__VERSION__/${VERSION}/g" static/index.html
7474
7575
go build \
7676
-ldflags "-X main.BuildTime=$(date --utc +%Y-%m-%dT%H:%M:%SZ) -X main.CommitHash=${{ github.sha }} -X main.Version=${VERSION}" \
@@ -82,6 +82,7 @@ jobs:
8282
mkdir -p lambda-artifacts
8383
mv -f bootstrap lambda-artifacts/bootstrap
8484
cp -r static lambda-artifacts/static
85+
cp -r templates lambda-artifacts/templates
8586
ls -l lambda-artifacts
8687
8788
- name: Configure AWS credentials

.github/workflows/release.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ jobs:
7373
fi
7474
7575
# Replace __VERSION__ placeholder in index.html for cache busting
76-
echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
77-
sed -i "s/__VERSION__/${VERSION}/g" static/index.html
76+
#echo "Replacing __VERSION__ with ${VERSION} in static/index.html"
77+
#sed -i "s/__VERSION__/${VERSION}/g" static/index.html
7878
7979
# Ensure dist directory exists
8080
mkdir -p dist
@@ -105,6 +105,7 @@ jobs:
105105
# Copy the binary and rename it to generic 'go-music'
106106
cp "$file" "$pkg_dir/go-music"
107107
cp -r ../static "$pkg_dir/"
108+
cp -r ../templates "$pkg_dir/"
108109
tar -czf "${file}.tar.gz" "$pkg_dir"
109110
rm -rf "$pkg_dir"
110111
echo "Created archive: ${file}.tar.gz"

MIGRATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ All functions that previously called `loadFromServer()` now use `fetchAPI()`:
144144
Once the new fetch-based flow is stable, consider removing:
145145

146146
1. Form-data fallback parsing in `handleRequest()`.
147-
2. Any leftover iframe/form DOM elements in `static/index.html`.
147+
2. Any leftover iframe/form DOM elements in `templates/index.html`.
148148
3. Any migration-only comments or shim helpers in the codebase.
149149

150150
## Notes about `setVersion()`

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ The app switches to Lambda mode when `AWS_LAMBDA_FUNCTION_NAME` is present. The
118118
-o bootstrap .
119119
```
120120

121-
2. Package with static assets:
121+
2. Package with static and templates assets:
122122
```bash
123123
mkdir -p lambda-artifacts
124124
mv bootstrap lambda-artifacts/
125125
cp -r static lambda-artifacts/
126+
cp -r templates lambda-artifacts/
126127
cd lambda-artifacts && zip -r ../deployment.zip . && cd ..
127128
```
128129

docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ WORKDIR /app
2222

2323
# copy static assets from build stage (they live at /build/static inside the build stage)
2424
COPY --from=build /build/static /app/static
25+
COPY --from=build /build/templates /app/templates
2526
# copy the compiled binary placed at /build/go-music in the build stage
2627
COPY --from=build /build/go-music /app/go-music
2728

main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"html/template"
89
"log"
910
"net/http"
1011
"os"
@@ -24,6 +25,7 @@ import (
2425

2526
var ginLambda *ginadapter.GinLambdaV2
2627
var r *gin.Engine
28+
var indexTmpl *template.Template
2729

2830
const (
2931
CHARSET = "UTF-8"
@@ -47,9 +49,9 @@ var s3Client *s3.Client
4749

4850
// Build info variables, set via -ldflags at build time
4951
var (
50-
Version = "dev"
51-
BuildTime = "unknown"
52-
CommitHash = "none"
52+
Version = "vDev"
53+
BuildTime = "timeless"
54+
CommitHash = "sha-unknown"
5355
)
5456

5557
// init function runs before main and sets up the Gin router.
@@ -73,8 +75,18 @@ func init() {
7375

7476
r = gin.Default()
7577
r.Static("/static", "./static")
78+
// Parse index.html once and render as a Go template so we can inject build info
79+
// like the Version string dynamically from the Go build.
80+
indexTmpl = template.Must(template.ParseFiles("./templates/index.html"))
7681
r.GET("/", func(c *gin.Context) {
77-
c.File("./static/index.html")
82+
data := struct{ Version string }{Version: Version}
83+
var buf bytes.Buffer
84+
if err := indexTmpl.Execute(&buf, data); err != nil {
85+
log.Printf("failed to render index template: %v", err)
86+
c.String(http.StatusInternalServerError, "Internal Server Error")
87+
return
88+
}
89+
c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes())
7890
})
7991
r.GET("/favicon.ico", func(c *gin.Context) {
8092
c.File("./static/favicon.ico")

main_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,18 @@ func TestHandleSearchInDir(t *testing.T) {
651651
assert.Contains(t, mm, "dir")
652652
}
653653
}
654+
655+
// TestIndexServesVersion ensures the server-side template injects the build Version
656+
// into the served HTML for the index page.
657+
func TestIndexServesVersion(t *testing.T) {
658+
gin.SetMode(gin.TestMode)
659+
660+
w := httptest.NewRecorder()
661+
req := httptest.NewRequest("GET", "/", nil)
662+
// Use the global router created in init()
663+
r.ServeHTTP(w, req)
664+
665+
assert.Equal(t, http.StatusOK, w.Code)
666+
body := w.Body.String()
667+
assert.Contains(t, body, Version, "index page should contain Version string")
668+
}

static/index.html renamed to templates/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
88
<meta name="mobile-web-app-capable" content="yes">
99
<meta name="theme-color" content="#2196f3">
10-
<link rel="stylesheet" href="/static/style.css?v=__VERSION__">
10+
<link rel="stylesheet" href="/static/style.css?v={{ .Version }}">
1111
</head>
1212

1313
<body onload="init()">
@@ -34,7 +34,7 @@ <h1 class="header-title">
3434
d="M8 0C3.58 0 0 3.58 0 8a7.96 7.96 0 0 0 5.47 7.59c.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.01.08-2.1 0 0 .67-.21 2.2.82a7.55 7.55 0 0 1 2-.27c.68 0 1.37.09 2 .27 1.52-1.03 2.2-.82 2.2-.82.44 1.09.16 1.9.08 2.1.51.56.82 1.28.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A7.96 7.96 0 0 0 16 8c0-4.42-3.58-8-8-8Z" />
3535
</svg>
3636
</span>
37-
<span id="appVersion">__VERSION__</span>
37+
<span id="appVersion">{{ .Version }}</span>
3838
</div>
3939
</a>
4040
</div>
@@ -121,7 +121,7 @@ <h1 class="header-title">
121121

122122
</main>
123123

124-
<script src="/static/script.js?v=__VERSION__"></script>
124+
<script src="/static/script.js?v={{ .Version }}"></script>
125125
</body>
126126

127127
</html>

0 commit comments

Comments
 (0)