Skip to content

Commit 7eda16d

Browse files
committed
feat: get songs by filename instead of id
1 parent 18f982c commit 7eda16d

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func mapRoutes(app *setup.App) {
3232
app.HandleFunc("POST /songQueue", music.PostHandler)
3333
app.HandleFunc("GET /songQueue", music.GetSongQueueHandler)
3434
app.HandleFunc("GET /player", music.GetSongPlayerHandler)
35-
app.HandleFunc("GET /song/{id}", music.GetSongHandler)
35+
app.HandleFunc("GET /song/{path...}", music.GetSongHandler)
3636
app.HandleFunc("GET /login", login.GetHandler)
3737
app.HandleFunc("POST /login", login.PostHandler)
3838

pages/music/music.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,7 @@ func GetSongPlayerHandler(w reqRes.MyWriter, r *reqRes.MyRequest) {
132132
}
133133

134134
func GetSongHandler(w reqRes.MyWriter, r *reqRes.MyRequest) {
135-
idString := r.PathValue("id")
136-
137-
var song models.Song
138-
result := r.Db.First(&song, idString)
139-
if result.Error != nil {
140-
message := fmt.Sprintf("Failed to get song:\n%v\n", result.Error)
141-
http.Error(w, message, http.StatusBadRequest)
142-
return
143-
}
144-
145-
filename := filepath.Join(r.AppConfig.SongsFolder, song.File)
146-
135+
pathQueryParam := r.PathValue("path")
136+
filename := filepath.Join(r.AppConfig.SongsFolder, pathQueryParam)
147137
pages.ServeFile(w, r, filename)
148138
}

pages/serveFile.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@ import (
66
"encoding/hex"
77
"fmt"
88
"go-server/setup/reqRes"
9-
"log"
109
"net/http"
1110
"os"
1211
)
1312

1413
func ServeFile(w reqRes.MyWriter, r *reqRes.MyRequest, filename string) {
1514
fileInfo, err := os.Stat(filename)
1615
if err != nil {
17-
log.Printf("Failed to stat file %s:\n%v\n", filename, err)
18-
w.WriteHeader(http.StatusBadRequest)
16+
message := fmt.Sprintf("Failed to stat file %s:\n%v\n", filename, err)
17+
http.Error(w, message, http.StatusBadRequest)
1918
return
2019
}
2120
if fileInfo.IsDir() {
22-
log.Printf("Failed to stat file: %s, it's a directory", filename)
23-
w.WriteHeader(http.StatusBadRequest)
21+
message := fmt.Sprintf("Failed to stat file: %s, it's a directory", filename)
22+
http.Error(w, message, http.StatusBadRequest)
2423
return
2524
}
2625

2726
file, err := os.ReadFile(filename)
2827
if err != nil {
29-
log.Printf("Failed to read file %s:\n%v\n", filename, err)
30-
w.WriteHeader(http.StatusInternalServerError)
28+
message := fmt.Sprintf("Failed to read file %s:\n%v\n", filename, err)
29+
http.Error(w, message, http.StatusInternalServerError)
3130
return
3231
}
3332

setup/middleware.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func CreateJwtAuthMiddleware(app App) Middleware {
3838
return func(next HandlerFn) HandlerFn {
3939
return func(w reqRes.MyWriter, r *reqRes.MyRequest) {
4040
if r.URL.Path == "/login" ||
41-
strings.HasPrefix(r.URL.Path, "/public") {
41+
strings.HasPrefix(r.URL.Path, "/public") ||
42+
strings.HasPrefix(r.URL.Path, "/song") {
4243
next(w, r)
4344
return
4445
}

0 commit comments

Comments
 (0)