Skip to content

Commit acf70b0

Browse files
authored
Merge pull request #34 from johnwmail/dev
Refactor to use Fetch API and remove legacy iframe handling
2 parents ed48ee5 + 3179a2d commit acf70b0

File tree

5 files changed

+1
-114
lines changed

5 files changed

+1
-114
lines changed

MIGRATION.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,19 @@ All functions that previously called `loadFromServer()` now use `fetchAPI()`:
8181

8282
- `echoReqHtml()` — HTML wrapper used to execute parent callbacks from an iframe.
8383
- `ea()` — JS helper that encoded arrays into a string for embedding in HTML callback arguments.
84-
- `setVersion()` (frontend) — compatibility wrapper; version rendering is handled directly by `loadVersion()` now.
8584

8685
In Go code, leftover helpers used only for the iframe flow were also removed.
8786

8887
## Notable updated handlers (backend)
8988

9089
- `handleRequest` — accepts JSON (`Content-Type: application/json`) and falls back to form parsing if needed. It dispatches on the request `function` field and returns JSON.
91-
- `handleVersion` — returns `{status: "ok", "version": "..."}`.
9290
- `handleDirRequest`, `handleGetAllMp3`, `handleGetAllMp3InDir`, `handleGetAllDirs`, `handleGetAllMp3InDirs`, `handleSearchTitle`, `handleSearchDir` — all return consistent JSON shapes.
9391

9492
The backend changes are focused on returning proper JSON and preserving behaviors (directory listing, searches, presigned URLs for S3, or local-file routing).
9593

9694
## Frontend changes (static/script.js)
9795

9896
- New `fetchAPI()` async wrapper that sends JSON and parses JSON responses.
99-
- `loadVersion()` is async and updates the version DOM element directly.
10097
- UI callbacks (`getBrowserData`, `getSearchTitle`, `getSearchDir`, etc.) now accept JSON objects and handle them without iframe callbacks.
10198

10299
## Backward compatibility
@@ -116,7 +113,6 @@ All functions that previously called `loadFromServer()` now use `fetchAPI()`:
116113
Run these locally before/after deployment:
117114

118115
- [x] Directory browsing (browse directories and see file lists)
119-
- [x] Version loads and displays in the UI
120116
- [x] Search by title (results show expected matches)
121117
- [x] Search by directory (results show expected matches)
122118
- [x] Get all MP3(s) and per-directory MP3 listings

main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,11 @@ func handleRequest(c *gin.Context) {
293293
handleGetAllDirs(c)
294294
case "getAllMp3InDirs":
295295
handleGetAllMp3InDirs(c, req.Data)
296-
case "version":
297-
handleVersion(c)
298296
default:
299297
c.JSON(http.StatusOK, gin.H{"status": "error", "message": "Unknown function"})
300298
}
301299
}
302300

303-
func handleVersion(c *gin.Context) {
304-
log.Printf("Returning version: %s", Version)
305-
c.JSON(http.StatusOK, gin.H{"status": "ok", "version": Version})
306-
}
307-
308301
// --- S3 Helper Functions ---
309302

310303
func s3ListAllAudioFiles(prefix string) ([]string, error) {

main_test.go

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -60,55 +60,6 @@ func TestIsAudioFile(t *testing.T) {
6060

6161
// (removed TestEa and its test-local ea implementation because the helper was removed from production code)
6262

63-
// TestHandleVersion tests the version endpoint
64-
func TestHandleVersion(t *testing.T) {
65-
// Save original values
66-
origVersion := Version
67-
origCommitHash := CommitHash
68-
origBuildTime := BuildTime
69-
70-
defer func() {
71-
Version = origVersion
72-
CommitHash = origCommitHash
73-
BuildTime = origBuildTime
74-
}()
75-
76-
tests := []struct {
77-
name string
78-
version string
79-
}{
80-
{
81-
name: "Dev version",
82-
version: "dev",
83-
},
84-
{
85-
name: "Release version",
86-
version: "v1.2.3",
87-
},
88-
}
89-
90-
for _, tt := range tests {
91-
t.Run(tt.name, func(t *testing.T) {
92-
Version = tt.version
93-
94-
gin.SetMode(gin.TestMode)
95-
w := httptest.NewRecorder()
96-
c, _ := gin.CreateTestContext(w)
97-
98-
handleVersion(c)
99-
100-
assert.Equal(t, http.StatusOK, w.Code)
101-
102-
// Verify JSON response
103-
var response map[string]interface{}
104-
err := json.Unmarshal(w.Body.Bytes(), &response)
105-
assert.NoError(t, err)
106-
assert.Equal(t, "ok", response["status"])
107-
assert.Equal(t, tt.version, response["version"])
108-
})
109-
}
110-
}
111-
11263
// TestUsingLocal tests the backend detection
11364
func TestUsingLocal(t *testing.T) {
11465
// Save original
@@ -354,14 +305,6 @@ func TestHandleRequest(t *testing.T) {
354305
wantJSONStatus string
355306
checkFields []string
356307
}{
357-
{
358-
name: "Get version",
359-
function: "version",
360-
data: "",
361-
wantStatus: http.StatusOK,
362-
wantJSONStatus: "ok",
363-
checkFields: []string{"version"},
364-
},
365308
{
366309
name: "List root directory",
367310
function: "dir",
@@ -428,39 +371,6 @@ func TestHandleRequest(t *testing.T) {
428371
}
429372
}
430373

431-
// TestHandleRequestFormData tests backwards compatibility with form data
432-
func TestHandleRequestFormData(t *testing.T) {
433-
tmpDir, err := os.MkdirTemp("", "gomusic-test-*")
434-
assert.NoError(t, err)
435-
defer os.RemoveAll(tmpDir)
436-
437-
origLocalMusicDir := localMusicDir
438-
defer func() {
439-
localMusicDir = origLocalMusicDir
440-
}()
441-
localMusicDir = tmpDir
442-
443-
gin.SetMode(gin.TestMode)
444-
445-
w := httptest.NewRecorder()
446-
c, _ := gin.CreateTestContext(w)
447-
448-
// Create form data request (legacy iframe style)
449-
formData := "dffunc=version&dfdata="
450-
c.Request = httptest.NewRequest("POST", "/api", strings.NewReader(formData))
451-
c.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
452-
453-
handleRequest(c)
454-
455-
assert.Equal(t, http.StatusOK, w.Code)
456-
457-
var response map[string]interface{}
458-
err = json.Unmarshal(w.Body.Bytes(), &response)
459-
assert.NoError(t, err)
460-
assert.Equal(t, "ok", response["status"])
461-
assert.Contains(t, response, "version")
462-
}
463-
464374
// TestHandleDirRequest tests directory listing
465375
func TestHandleDirRequest(t *testing.T) {
466376
tmpDir, err := os.MkdirTemp("", "gomusic-test-*")

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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">Loading ...</span>
37+
<span id="appVersion">__VERSION__</span>
3838
</div>
3939
</a>
4040
</div>

static/script.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ function getBrowserData(data) {
8383
browserDirs = data.dirs || [];
8484
browserTitles = data.files || [];
8585
updateBrowser();
86-
// Load version after initial directory is loaded (only on first load)
87-
if (browserCurDir === '' && gebi('appVersion').textContent === 'Loading ...') {
88-
loadVersion();
89-
}
9086
} else {
9187
alert(data.message || 'Error loading directory');
9288
}
@@ -128,14 +124,6 @@ function init() {
128124
}
129125
}
130126

131-
async function loadVersion() {
132-
const data = await fetchAPI('version', '');
133-
if (data.status === 'ok' && data.version) {
134-
gebi('appVersion').textContent = data.version;
135-
}
136-
}
137-
138-
139127
function markLoading(tab) {
140128
var browserLoader = gebi('markLoadBrowser');
141129
var searchLoader = gebi('markLoadSearch');

0 commit comments

Comments
 (0)