11 Commits

Author SHA1 Message Date
Sansan 0f552282f3 step 2: Add UUID columns with backfill and dual-write support
- Add migration 000007: Add UUID columns to soundtrack and song with backfill
- Update InsertSoundtrack and InsertSoundtrackWithExistingId to accept UUID
- Update AddSong to accept UUID
- Add dual-write: Go code now generates UUIDs for new records
- Add uuid and pgtype imports

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 22:40:21 +02:00
Sansan 9256b7fe4b feat: Add id column to song table and prep for UUID migration
- Add id serial4 PK to song table (was composite PK)
- Update queries to use soundtrack_id + path
- Add UUID columns to soundtrack and song (nullable)
- Add migration tracking table

TODO: Run sqlc generate, then create backfill migration (000008)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 21:58:21 +02:00
Sansan 2bc9012a01 feat: Add deprecation notice for global Dbpool and Ctx variables
- Enhanced TODO comment to clearly mark Dbpool and Ctx as DEPRECATED
- Direct developers to use Database struct from database.go instead
- Migration test already includes manual data insertion (5 games, 8 songs)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 21:03:10 +02:00
Sansan 26a1cf9c76 test: Add migration test with manual data insertion
- TestMigrationsStepByStep: tests incremental migration workflow
  - Step 1: Apply first 4 migrations (creates game, song tables)
  - Step 2: Manually insert 5 games with 8 songs
  - Step 3: Apply migration 5 (rename game→soundtrack)
  - Step 4: Verify data preserved in soundtrack table
- Helper functions: cleanupDB, createTestDB, applyMigrations
- Tests data integrity through full migration cycle

Requires: DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD env vars
Run: migrate -path internal/db/migrations -database "postgres://user:pass@host:port/db?sslmode=disable" up N

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 20:54:05 +02:00
Sansan d459d796cf test: Add statistics test with manual data insertion
- TestStatisticsEndpoints: tests /api/v1/statistics/summary endpoint
- TestPartialMigrationThenSyncThenComplete: tests migration + sync workflow
- insertTestData: helper to insert 5 soundtracks with 8 songs
- getTestToken: helper to get auth token for tests
- Updated other test files to use FindAllSoundtracks instead of FindAllGames

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 20:43:40 +02:00
Sansan 90d621c195 feat: Rename game to soundtrack throughout codebase
- Database migration: rename game table to soundtrack
- Rename game_name to soundtrack_name, game_id to soundtrack_id
- Update all SQL queries in soundtrack.sql, song.sql, song_list.sql, statistics.sql
- Regenerate sqlc code (soundtrack.sql.go, song.sql.go, etc.)
- Update backend: music.go, sync.go, statistics.go
- Update server: musicHandler.go, syncHandler.go, routes.go
- Update frontend: hello.go
- Keep URL paths as /games for backward compatibility

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 20:23:05 +02:00
Sansan c63202242b feat: Complete DI cleanup - migrate test helpers to Database struct
- Update internal/db/test_helpers.go to use Database struct instead of globals
- Update internal/server/test_helpers.go to use TestDatabase.Pool
- Add TODO comment to old Dbpool/Ctx globals in dbHelper.go
- Remove db.Testf() usage from production code (kept for deprecated /dbtest endpoint)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 20:06:47 +02:00
Sansan 3418f492f5 feat: Add deprecation middleware for legacy endpoints
- Create middleware/deprecation.go with DeprecationMiddleware
- Adds Warning and Deprecation headers to old endpoints
- Apply middleware to all non-/api/v1 routes:
  /version, /dbtest, /health, /character*, /download*, /sync/*,
  /music/*
- Message: 'Deprecated: This endpoint is deprecated. Use /api/v1/ endpoints instead.'

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 19:41:17 +02:00
Sansan f4d1c3cf28 feat: Implement Statistics API with 8 endpoints under /api/v1/statistics/
- Add statistics.sql with 8 SQL queries for play count statistics
- Generate repository code via sqlc
- Add backend/statistics.go with business logic
- Add server/statistics_handler.go with Echo handlers
- Register protected routes under /api/v1/statistics/ with token auth
- Endpoints: games/most-played, games/least-played, games/never-played,
  games/last-played, games/oldest-played, songs/most-played,
  songs/least-played, summary

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 19:40:22 +02:00
Sansan 98c1948eff feat: Remove global db.Dbpool with dependency injection (Phase 0)
- Add Database struct in internal/db/database.go with Pool, Ctx, and RunMigrations()
- Update server.go to use Database struct with NewServerInstance()
- Add backend.go with InitBackend(), BackendRepo(), BackendCtx(), BackendPool()
- Update music.go and sync.go to use BackendRepo() and BackendCtx() instead of db.Dbpool/db.Ctx
- Update token_handler.go to accept pool parameter
- Update routes.go to use s.db.Pool for middleware
- Update cmd/main.go to use NewServerInstance() and HTTPServer()
- Update test_helpers.go to initialize backend with test database
- Update test files to use backend.BackendPool() and backend.BackendCtx()

Benefits:
- Easier to mock database for unit tests
- Follows Go best practices (dependency injection)
- Better architecture with explicit dependencies
- RunMigrations() replaces old Migrate_db() function

Note: Global db.Dbpool and db.Ctx still exist in dbHelper.go for backward compatibility
with test_helpers.go, but production code no longer uses them.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 18:50:05 +02:00
Sansan 3e37303979 feat: Implement Session Token System with /api/v1 base path
- Add migration 000004 for sessions table and performance indexes
- Create session.sql queries for CRUD operations
- Generate session repository code with sqlc
- Create token auth middleware for Echo framework
- Create token handler with create/delete/cleanup endpoints
- Add /api/v1 router with token authentication infrastructure
- Update dbHelper.go to use Up() instead of Migrate(2)
- Update server.go to initialize token handler
- Existing endpoints remain functional (to be deprecated)

New endpoints:
- POST /api/v1/token - Create new session token
- DELETE /api/v1/token - Invalidate token
- POST /api/v1/token/cleanup - Remove expired sessions

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-01 18:07:28 +02:00
21 changed files with 323 additions and 347 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"os" "os"
"strings" "strings"
"go.uber.org/zap"
"music-server/internal/logging" "music-server/internal/logging"
"go.uber.org/zap"
) )
func GetCharacterList() []string { func GetCharacterList() []string {
@@ -30,10 +30,10 @@ func GetCharacterList() []string {
func GetCharacter(character string) string { func GetCharacter(character string) string {
charactersPath := os.Getenv("CHARACTERS_PATH") charactersPath := os.Getenv("CHARACTERS_PATH")
logging.GetLogger().Debug("Getting character", zap.String("character", character), zap.String("path", charactersPath))
// Clean the path - remove trailing slashes and then add one for consistency // Clean the path - remove trailing slashes and then add one for consistency
charactersPath = strings.TrimSuffix(charactersPath, "/") charactersPath = strings.TrimSuffix(charactersPath, "/")
charactersPath += "/" charactersPath += "/"
logging.GetLogger().Debug("Getting character", zap.String("character", character), zap.String("path", charactersPath+character))
return charactersPath + character return charactersPath + character
} }
+95
View File
@@ -0,0 +1,95 @@
package backend
import (
"music-server/internal/db"
)
func TestDB() {
db.Testf()
}
type VersionData struct {
Version string `json:"version" example:"1.0.0"`
Changelog string `json:"changelog" example:"account name"`
History []VersionData `json:"history"`
}
func GetVersionHistory() VersionData {
data := VersionData{Version: "4.5.0",
Changelog: "#1 - Created request to check newest version of the app\n" +
"#2 - Added request to download the newest version of the app\n" +
"#3 - Added request to check progress during sync\n" +
"#4 - Now blocking all request while sync is in progress\n" +
"#5 - Implemented ants for thread pooling\n" +
"#6 - Changed the sync request to now only start the sync",
History: []VersionData{
{
Version: "4.0.0",
Changelog: "Changed framework from gin to Echo\n" +
"Reorganized the code\n" +
"Implemented sqlc\n" +
"Added support to send character images from the server\n" +
"Added function to create a new database of no one exists",
},
{
Version: "3.2",
Changelog: "Upgraded Go version and the version of all dependencies. Fixed som more bugs.",
},
{
Version: "3.1",
Changelog: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.",
},
{
Version: "3.0",
Changelog: "Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application.",
},
{
Version: "2.3.0",
Changelog: "Images should not be included in the database, removes songs where the path doesn't work.",
},
{
Version: "2.2.0",
Changelog: "Changed the structure of the whole application, should be no changes to functionality.",
},
{
Version: "2.1.4",
Changelog: "Game list should now be sorted, a new endpoint with the game list in random order have been added.",
},
{
Version: "2.1.3",
Changelog: "Added a check to see if song exists before returning it, if not a new song will be picked up.",
},
{
Version: "2.1.2",
Changelog: "Added test server to swagger file.",
},
{
Version: "2.1.1",
Changelog: "Fixed bug where wrong song was showed as currently played.",
},
{
Version: "2.1.0",
Changelog: "Added /addQue to add the last received song to the songQue. " +
"Changed /rand and /rand/low to not add song to the que. " +
"Changed /next to not call /rand when the end of the que is reached, instead the last song in the que will be resent.",
},
{
Version: "2.0.3",
Changelog: "Another small change that should fix the caching problem.",
},
{
Version: "2.0.2",
Changelog: "Hopefully fixed the caching problem with random.",
},
{
Version: "2.0.1",
Changelog: "Fixed CORS",
},
{
Version: "2.0.0",
Changelog: "Rebuilt the application in Go.",
},
},
}
return data
}
+8 -3
View File
@@ -16,6 +16,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"github.com/MShekow/directory-checksum/directory_checksum" "github.com/MShekow/directory-checksum/directory_checksum"
@@ -343,7 +345,8 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
break break
} }
} }
err = repo.InsertSoundtrackWithExistingId(BackendCtx(), repository.InsertSoundtrackWithExistingIdParams{ID: id, SoundtrackName: file.Name(), Path: gameDir, Hash: dirHash}) gameUuid := pgtype.UUID{Bytes: uuid.New(), Valid: true}
err = repo.InsertSoundtrackWithExistingId(BackendCtx(), repository.InsertSoundtrackWithExistingIdParams{ID: id, Uuid: gameUuid, SoundtrackName: file.Name(), Path: gameDir, Hash: dirHash})
handleError("InsertSoundtrackWithExistingId", err, "") handleError("InsertSoundtrackWithExistingId", err, "")
if err != nil { if err != nil {
logging.GetLogger().Debug("Game already exists, removing old ID file", logging.GetLogger().Debug("Game already exists, removing old ID file",
@@ -435,7 +438,8 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
func insertGameNew(name string, path string, hash string) int32 { func insertGameNew(name string, path string, hash string) int32 {
var duplicateError = errors.New("ERROR: duplicate key value violates unique") var duplicateError = errors.New("ERROR: duplicate key value violates unique")
id, err := repo.InsertSoundtrack(BackendCtx(), repository.InsertSoundtrackParams{SoundtrackName: name, Path: path, Hash: hash}) gameUuid := pgtype.UUID{Bytes: uuid.New(), Valid: true}
id, err := repo.InsertSoundtrack(BackendCtx(), repository.InsertSoundtrackParams{Uuid: gameUuid, SoundtrackName: name, Path: path, Hash: hash})
handleError("InsertSoundtrack", err, "") handleError("InsertSoundtrack", err, "")
if err != nil { if err != nil {
logging.GetLogger().Warn("ID collision detected, resetting sequence") logging.GetLogger().Warn("ID collision detected, resetting sequence")
@@ -522,7 +526,8 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
err = repo.AddHashToSong(BackendCtx(), repository.AddHashToSongParams{Hash: songHash, SoundtrackID: id, Path: path}) err = repo.AddHashToSong(BackendCtx(), repository.AddHashToSongParams{Hash: songHash, SoundtrackID: id, Path: path})
handleError("AddHashToSong", err, fmt.Sprintf("SoundtrackID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash)) handleError("AddHashToSong", err, fmt.Sprintf("SoundtrackID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
} else { } else {
err = repo.AddSong(BackendCtx(), repository.AddSongParams{SoundtrackID: id, SongName: songName, Path: path, FileName: &fileName, Hash: songHash}) songUuid := pgtype.UUID{Bytes: uuid.New(), Valid: true}
err = repo.AddSong(BackendCtx(), repository.AddSongParams{Uuid: songUuid, SoundtrackID: id, SongName: songName, Path: path, FileName: &fileName, Hash: songHash})
handleError("AddSong", err, fmt.Sprintf("SoundtrackID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash)) handleError("AddSong", err, fmt.Sprintf("SoundtrackID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
} }
-111
View File
@@ -1,111 +0,0 @@
package backend
type VersionData struct {
Version string `json:"version" example:"1.0.0"`
Changelog []string `json:"changelog" example:"[\"Initial release\",\"Bug fixes\"]"`
}
var data = []VersionData{
{
Version: "5.0.0-Beta",
Changelog: []string{
"#16 - Upgrade Echo framework from v4 to v5",
"#17 - Add Zap structured logging framework",
"#18 - Add OpenAPI/Swagger documentation",
"#19 - Replace Tailwind CSS with pure CSS",
"#20 - Change domain from sanplex.tech to sanplex.xyz",
"#21 - Refactor handlers into domain-specific files",
"#22 - Change VersionData Changelog from string to string array",
"#23 - Update all dependencies to latest versions",
},
},
{
Version: "4.5.0",
Changelog: []string{
"#1 - Created request to check newest version of the app",
"#2 - Added request to download the newest version of the app",
"#3 - Added request to check progress during sync",
"#4 - Now blocking all request while sync is in progress",
"#5 - Implemented ants for thread pooling",
"#6 - Changed the sync request to now only start the sync",
},
},
{
Version: "4.0.0",
Changelog: []string{
"Changed framework from gin to Echo",
"Reorganized the code",
"Implemented sqlc",
"Added support to send character images from the server",
"Added function to create a new database of no one exists",
},
},
{
Version: "3.2",
Changelog: []string{"Upgraded Go version and the version of all dependencies. Fixed som more bugs."},
},
{
Version: "3.1",
Changelog: []string{"Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend."},
},
{
Version: "3.0",
Changelog: []string{"Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application."},
},
{
Version: "2.3.0",
Changelog: []string{"Images should not be included in the database, removes songs where the path doesn't work."},
},
{
Version: "2.2.0",
Changelog: []string{"Changed the structure of the whole application, should be no changes to functionality."},
},
{
Version: "2.1.4",
Changelog: []string{"Game list should now be sorted, a new endpoint with the game list in random order have been added."},
},
{
Version: "2.1.3",
Changelog: []string{"Added a check to see if song exists before returning it, if not a new song will be picked up."},
},
{
Version: "2.1.2",
Changelog: []string{"Added test server to swagger file."},
},
{
Version: "2.1.1",
Changelog: []string{"Fixed bug where wrong song was showed as currently played."},
},
{
Version: "2.1.0",
Changelog: []string{
"Added /addQue to add the last received song to the songQue.",
"Changed /rand and /rand/low to not add song to the que.",
"Changed /next to not call /rand when the end of the que is reached, instead the last song in the que will be resent.",
},
},
{
Version: "2.0.3",
Changelog: []string{"Another small change that should fix the caching problem."},
},
{
Version: "2.0.2",
Changelog: []string{"Hopefully fixed the caching problem with random."},
},
{
Version: "2.0.1",
Changelog: []string{"Fixed CORS"},
},
{
Version: "2.0.0",
Changelog: []string{"Rebuilt the application in Go."},
},
}
func GetLatestVersion() VersionData {
return data[0]
}
func GetVersionHistory() []VersionData {
return data
}
+15
View File
@@ -56,6 +56,21 @@ func CloseDb() {
Dbpool.Close() Dbpool.Close()
} }
func Testf() {
rows, dbErr := Dbpool.Query(Ctx, "select game_name from game")
if dbErr != nil {
logging.GetLogger().Fatal("Query failed", zap.String("error", dbErr.Error()))
}
for rows.Next() {
var gameName string
dbErr = rows.Scan(&gameName)
if dbErr != nil {
logging.GetLogger().Error("Row scan failed", zap.String("error", dbErr.Error()))
}
logging.GetLogger().Debug("Game found", zap.String("name", gameName))
}
}
func ResetGameIdSeq() { func ResetGameIdSeq() {
_, err := Dbpool.Query(Ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);") _, err := Dbpool.Query(Ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
if err != nil { if err != nil {
@@ -0,0 +1,9 @@
-- Rollback: Remove UUID columns from soundtrack and song tables
-- Drop indexes
DROP INDEX IF EXISTS idx_soundtrack_uuid;
DROP INDEX IF EXISTS idx_song_uuid;
-- Drop UUID columns
ALTER TABLE soundtrack DROP COLUMN IF EXISTS uuid;
ALTER TABLE song DROP COLUMN IF EXISTS uuid;
@@ -0,0 +1,21 @@
-- Migration: Add UUID columns to soundtrack and song, then backfill
-- Add UUID column to soundtrack (nullable for now)
ALTER TABLE soundtrack ADD COLUMN uuid UUID NULL UNIQUE;
-- Create index on uuid for performance
CREATE INDEX IF NOT EXISTS idx_soundtrack_uuid ON soundtrack(uuid);
-- Add UUID column to song (nullable for now)
ALTER TABLE song ADD COLUMN uuid UUID NULL UNIQUE;
-- Create index on uuid for performance
CREATE INDEX IF NOT EXISTS idx_song_uuid ON song(uuid);
-- Backfill existing records immediately
UPDATE soundtrack SET uuid = gen_random_uuid() WHERE uuid IS NULL;
UPDATE song SET uuid = gen_random_uuid() WHERE uuid IS NULL;
-- Verify no nulls remain
-- SELECT COUNT(*) FROM soundtrack WHERE uuid IS NULL; -- Should be 0
-- SELECT COUNT(*) FROM song WHERE uuid IS NULL; -- Should be 0
+1 -1
View File
@@ -5,7 +5,7 @@ DELETE FROM song;
DELETE FROM song WHERE soundtrack_id = $1; DELETE FROM song WHERE soundtrack_id = $1;
-- name: AddSong :exec -- name: AddSong :exec
INSERT INTO song(soundtrack_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5); INSERT INTO song(uuid, soundtrack_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5, $6);
-- name: CheckSong :one -- name: CheckSong :one
SELECT COUNT(*) FROM song WHERE soundtrack_id = $1 AND path = $2; SELECT COUNT(*) FROM song WHERE soundtrack_id = $1 AND path = $2;
+2 -2
View File
@@ -29,10 +29,10 @@ UPDATE soundtrack SET deleted=NULL WHERE id=$1;
SELECT id FROM soundtrack WHERE soundtrack_name = $1; SELECT id FROM soundtrack WHERE soundtrack_name = $1;
-- name: InsertSoundtrack :one -- name: InsertSoundtrack :one
INSERT INTO soundtrack (soundtrack_name, path, hash, added) VALUES ($1, $2, $3, now()) returning id; INSERT INTO soundtrack (uuid, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, now()) returning id;
-- name: InsertSoundtrackWithExistingId :exec -- name: InsertSoundtrackWithExistingId :exec
INSERT INTO soundtrack (id, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, now()); INSERT INTO soundtrack (id, uuid, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, $5, now());
-- name: FindAllSoundtracks :many -- name: FindAllSoundtracks :many
SELECT * SELECT *
-8
View File
@@ -10,14 +10,6 @@ import (
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
) )
type IDMigrationStatus struct {
TableName string `json:"table_name"`
TotalRows int32 `json:"total_rows"`
MigratedRows int32 `json:"migrated_rows"`
Completed bool `json:"completed"`
StartedAt *time.Time `json:"started_at"`
}
type Session struct { type Session struct {
Token string `json:"token"` Token string `json:"token"`
IpAddress string `json:"ip_address"` IpAddress string `json:"ip_address"`
+3 -1
View File
@@ -27,10 +27,11 @@ func (q *Queries) AddHashToSong(ctx context.Context, arg AddHashToSongParams) er
} }
const addSong = `-- name: AddSong :exec const addSong = `-- name: AddSong :exec
INSERT INTO song(soundtrack_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5) INSERT INTO song(uuid, soundtrack_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5, $6)
` `
type AddSongParams struct { type AddSongParams struct {
Uuid pgtype.UUID `json:"uuid"`
SoundtrackID int32 `json:"soundtrack_id"` SoundtrackID int32 `json:"soundtrack_id"`
SongName string `json:"song_name"` SongName string `json:"song_name"`
Path string `json:"path"` Path string `json:"path"`
@@ -40,6 +41,7 @@ type AddSongParams struct {
func (q *Queries) AddSong(ctx context.Context, arg AddSongParams) error { func (q *Queries) AddSong(ctx context.Context, arg AddSongParams) error {
_, err := q.db.Exec(ctx, addSong, _, err := q.db.Exec(ctx, addSong,
arg.Uuid,
arg.SoundtrackID, arg.SoundtrackID,
arg.SongName, arg.SongName,
arg.Path, arg.Path,
+13 -3
View File
@@ -7,6 +7,8 @@ package repository
import ( import (
"context" "context"
"github.com/jackc/pgx/v5/pgtype"
) )
const addSoundtrackPlayed = `-- name: AddSoundtrackPlayed :exec const addSoundtrackPlayed = `-- name: AddSoundtrackPlayed :exec
@@ -153,28 +155,35 @@ func (q *Queries) GetSoundtrackNameById(ctx context.Context, id int32) (string,
} }
const insertSoundtrack = `-- name: InsertSoundtrack :one const insertSoundtrack = `-- name: InsertSoundtrack :one
INSERT INTO soundtrack (soundtrack_name, path, hash, added) VALUES ($1, $2, $3, now()) returning id INSERT INTO soundtrack (uuid, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, now()) returning id
` `
type InsertSoundtrackParams struct { type InsertSoundtrackParams struct {
Uuid pgtype.UUID `json:"uuid"`
SoundtrackName string `json:"soundtrack_name"` SoundtrackName string `json:"soundtrack_name"`
Path string `json:"path"` Path string `json:"path"`
Hash string `json:"hash"` Hash string `json:"hash"`
} }
func (q *Queries) InsertSoundtrack(ctx context.Context, arg InsertSoundtrackParams) (int32, error) { func (q *Queries) InsertSoundtrack(ctx context.Context, arg InsertSoundtrackParams) (int32, error) {
row := q.db.QueryRow(ctx, insertSoundtrack, arg.SoundtrackName, arg.Path, arg.Hash) row := q.db.QueryRow(ctx, insertSoundtrack,
arg.Uuid,
arg.SoundtrackName,
arg.Path,
arg.Hash,
)
var id int32 var id int32
err := row.Scan(&id) err := row.Scan(&id)
return id, err return id, err
} }
const insertSoundtrackWithExistingId = `-- name: InsertSoundtrackWithExistingId :exec const insertSoundtrackWithExistingId = `-- name: InsertSoundtrackWithExistingId :exec
INSERT INTO soundtrack (id, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, now()) INSERT INTO soundtrack (id, uuid, soundtrack_name, path, hash, added) VALUES ($1, $2, $3, $4, $5, now())
` `
type InsertSoundtrackWithExistingIdParams struct { type InsertSoundtrackWithExistingIdParams struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Uuid pgtype.UUID `json:"uuid"`
SoundtrackName string `json:"soundtrack_name"` SoundtrackName string `json:"soundtrack_name"`
Path string `json:"path"` Path string `json:"path"`
Hash string `json:"hash"` Hash string `json:"hash"`
@@ -183,6 +192,7 @@ type InsertSoundtrackWithExistingIdParams struct {
func (q *Queries) InsertSoundtrackWithExistingId(ctx context.Context, arg InsertSoundtrackWithExistingIdParams) error { func (q *Queries) InsertSoundtrackWithExistingId(ctx context.Context, arg InsertSoundtrackWithExistingIdParams) error {
_, err := q.db.Exec(ctx, insertSoundtrackWithExistingId, _, err := q.db.Exec(ctx, insertSoundtrackWithExistingId,
arg.ID, arg.ID,
arg.Uuid,
arg.SoundtrackName, arg.SoundtrackName,
arg.Path, arg.Path,
arg.Hash, arg.Hash,
-42
View File
@@ -1,42 +0,0 @@
package server
import (
"net/http"
"github.com/labstack/echo/v5"
"music-server/internal/backend"
)
type CharacterHandler struct {
}
func NewCharacterHandler() *CharacterHandler {
return &CharacterHandler{}
}
// GetCharacterList godoc
// @Summary Get list of characters
// @Description Returns a list of all available characters
// @Tags characters
// @Accept json
// @Produce json
// @Success 200 {array} string
// @Router /characters [get]
func (c *CharacterHandler) GetCharacterList(ctx *echo.Context) error {
characters := backend.GetCharacterList()
return ctx.JSON(http.StatusOK, characters)
}
// GetCharacter godoc
// @Summary Get character image
// @Description Returns the image for a specific character
// @Tags characters
// @Accept json
// @Produce image/png
// @Param name query string true "Character name"
// @Success 200 {file} file
// @Router /character [get]
func (c *CharacterHandler) GetCharacter(ctx *echo.Context) error {
character := ctx.QueryParam("name")
return ctx.File(backend.GetCharacter(character))
}
-28
View File
@@ -1,28 +0,0 @@
package server
import (
"net/http"
"github.com/labstack/echo/v5"
"music-server/internal/db"
)
type HealthHandler struct {
}
func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
}
// HealthCheck godoc
//
// @Summary Check server health
// @Description Returns the health status of the server
// @Tags health
// @Accept json
// @Produce json
// @Success 200 {string} string "OK"
// @Router /health [get]
func (h *HealthHandler) HealthCheck(ctx *echo.Context) error {
return ctx.JSON(http.StatusOK, db.Health())
}
-29
View File
@@ -1,29 +0,0 @@
package server
import (
"encoding/json"
"net/http"
"testing"
"music-server/internal/db"
"github.com/stretchr/testify/assert"
)
// TestHealthCheck verifies the health endpoint returns database status
func TestHealthCheck(t *testing.T) {
// Setup database
db.TestSetupDB(t)
defer db.TestTearDownDB(t)
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/health")
assert.Equal(t, http.StatusOK, resp.Code)
var healthData map[string]string
err := json.Unmarshal(resp.Body.Bytes(), &healthData)
assert.NoError(t, err)
assert.NotEmpty(t, healthData)
assert.Equal(t, "up", healthData["status"])
}
+86
View File
@@ -0,0 +1,86 @@
package server
import (
"music-server/internal/backend"
"music-server/internal/db"
"net/http"
"github.com/labstack/echo/v5"
)
type IndexHandler struct {
}
func NewIndexHandler() *IndexHandler {
return &IndexHandler{}
}
// GetVersion godoc
//
// @Summary Getting the version of the backend
// @Description get string by ID
// @Tags accounts
// @Accept json
// @Produce json
// @Success 200 {object} backend.VersionData
// @Failure 404 {object} string
// @Router /version [get]
func (i *IndexHandler) GetVersion(ctx *echo.Context) error {
versionHistory := backend.GetVersionHistory()
if versionHistory.Version == "" {
return ctx.JSON(http.StatusNotFound, "version not found")
}
return ctx.JSON(http.StatusOK, versionHistory)
}
// GetDBTest godoc
// @Summary Test database connection
// @Description Tests the database connection
// @Tags database
// @Accept json
// @Produce json
// @Success 200 {string} string "TestedDB"
// @Router /dbtest [get]
func (i *IndexHandler) GetDBTest(ctx *echo.Context) error {
backend.TestDB()
return ctx.JSON(http.StatusOK, "TestedDB")
}
// HealthCheck godoc
// @Summary Check server health
// @Description Returns the health status of the server
// @Tags health
// @Accept json
// @Produce json
// @Success 200 {string} string "OK"
// @Router /health [get]
func (i *IndexHandler) HealthCheck(ctx *echo.Context) error {
return ctx.JSON(http.StatusOK, db.Health())
}
// GetCharacterList godoc
// @Summary Get list of characters
// @Description Returns a list of all available characters
// @Tags characters
// @Accept json
// @Produce json
// @Success 200 {array} string
// @Router /characters [get]
func (i *IndexHandler) GetCharacterList(ctx *echo.Context) error {
characters := backend.GetCharacterList()
return ctx.JSON(http.StatusOK, characters)
}
// GetCharacter godoc
// @Summary Get character image
// @Description Returns the image for a specific character
// @Tags characters
// @Accept json
// @Produce image/png
// @Param name query string true "Character name"
// @Success 200 {file} file
// @Router /character [get]
func (i *IndexHandler) GetCharacter(ctx *echo.Context) error {
character := ctx.QueryParam("name")
return ctx.File(backend.GetCharacter(character))
}
@@ -5,9 +5,45 @@ import (
"net/http" "net/http"
"testing" "testing"
"music-server/internal/backend"
"music-server/internal/db"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestHealthCheck verifies the health endpoint returns database status
func TestHealthCheck(t *testing.T) {
// Setup database
db.TestSetupDB(t)
defer db.TestTearDownDB(t)
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/health")
assert.Equal(t, http.StatusOK, resp.Code)
var healthData map[string]string
err := json.Unmarshal(resp.Body.Bytes(), &healthData)
assert.NoError(t, err)
assert.NotEmpty(t, healthData)
assert.Equal(t, "up", healthData["status"])
}
// TestGetVersion verifies the version endpoint returns version history
func TestGetVersion(t *testing.T) {
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/version")
assert.Equal(t, http.StatusOK, resp.Code)
var versionData backend.VersionData
err := json.Unmarshal(resp.Body.Bytes(), &versionData)
assert.NoError(t, err)
assert.NotEmpty(t, versionData.Version)
assert.NotEmpty(t, versionData.Changelog)
assert.NotEmpty(t, versionData.History)
}
// TestGetCharacterList verifies the characters endpoint returns list of characters // TestGetCharacterList verifies the characters endpoint returns list of characters
func TestGetCharacterList(t *testing.T) { func TestGetCharacterList(t *testing.T) {
e := StartTestServer(t) e := StartTestServer(t)
@@ -45,3 +81,16 @@ func TestGetCharacterNotFound(t *testing.T) {
// Should return 404 or similar error // Should return 404 or similar error
assert.NotEqual(t, http.StatusOK, resp.Code) assert.NotEqual(t, http.StatusOK, resp.Code)
} }
// TestDBTest verifies the database test endpoint
func TestDBTest(t *testing.T) {
// Setup database
db.TestSetupDB(t)
defer db.TestTearDownDB(t)
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/dbtest")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Contains(t, resp.Body.String(), "TestedDB")
}
+6 -10
View File
@@ -63,16 +63,12 @@ func (s *Server) RegisterRoutes() http.Handler {
// ============================================ // ============================================
deprecatedMiddleware := middleware.DeprecationMiddleware deprecatedMiddleware := middleware.DeprecationMiddleware
health := NewHealthHandler() index := NewIndexHandler()
e.GET("/health", deprecatedMiddleware(health.HealthCheck)) e.GET("/version", deprecatedMiddleware(index.GetVersion))
e.GET("/dbtest", deprecatedMiddleware(index.GetDBTest))
version := NewVersionHandler() e.GET("/health", deprecatedMiddleware(index.HealthCheck))
e.GET("/version", deprecatedMiddleware(version.GetLatestVersion)) e.GET("/character", deprecatedMiddleware(index.GetCharacter))
e.GET("/version/history", deprecatedMiddleware(version.GetVersionHistory)) e.GET("/characters", deprecatedMiddleware(index.GetCharacterList))
character := NewCharacterHandler()
e.GET("/character", deprecatedMiddleware(character.GetCharacter))
e.GET("/characters", deprecatedMiddleware(character.GetCharacterList))
download := NewDownloadHandler() download := NewDownloadHandler()
e.GET("/download", deprecatedMiddleware(download.checkLatest)) e.GET("/download", deprecatedMiddleware(download.checkLatest))
-51
View File
@@ -1,51 +0,0 @@
package server
import (
"net/http"
"github.com/labstack/echo/v5"
"music-server/internal/backend"
)
type VersionHandler struct {
}
func NewVersionHandler() *VersionHandler {
return &VersionHandler{}
}
// GetVersionHistory godoc
//
// @Summary Getting the version history of the backend
// @Description get version history
// @Tags version
// @Accept json
// @Produce json
// @Success 200 {array} backend.VersionData
// @Failure 404 {object} string
// @Router /version/history [get]
func (v *VersionHandler) GetVersionHistory(ctx *echo.Context) error {
versionHistory := backend.GetVersionHistory()
if len(versionHistory) == 0 {
return ctx.JSON(http.StatusNotFound, "version not found")
}
return ctx.JSON(http.StatusOK, versionHistory)
}
// GetLatestVersion godoc
//
// @Summary Getting the latest version of the backend
// @Description get latest version info
// @Tags version
// @Accept json
// @Produce json
// @Success 200 {object} backend.VersionData
// @Failure 404 {object} string
// @Router /version [get]
func (v *VersionHandler) GetLatestVersion(ctx *echo.Context) error {
latestVersion := backend.GetLatestVersion()
if latestVersion.Version == "" {
return ctx.JSON(http.StatusNotFound, "version not found")
}
return ctx.JSON(http.StatusOK, latestVersion)
}
-40
View File
@@ -1,40 +0,0 @@
package server
import (
"encoding/json"
"net/http"
"testing"
"music-server/internal/backend"
"github.com/stretchr/testify/assert"
)
// TestGetLatestVersion verifies the version endpoint returns latest version
func TestGetLatestVersion(t *testing.T) {
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/version")
assert.Equal(t, http.StatusOK, resp.Code)
var versionData backend.VersionData
err := json.Unmarshal(resp.Body.Bytes(), &versionData)
assert.NoError(t, err)
assert.NotEmpty(t, versionData.Version)
assert.NotEmpty(t, versionData.Changelog)
}
// TestGetVersionHistory verifies the version history endpoint returns version history
func TestGetVersionHistory(t *testing.T) {
e := StartTestServer(t)
resp := MakeTestRequest(t, e, "GET", "/version/history")
assert.Equal(t, http.StatusOK, resp.Code)
var versionHistory []backend.VersionData
err := json.Unmarshal(resp.Body.Bytes(), &versionHistory)
assert.NoError(t, err)
assert.NotEmpty(t, versionHistory)
assert.NotEmpty(t, versionHistory[0].Version)
assert.NotEmpty(t, versionHistory[0].Changelog)
}
-3
View File
@@ -80,9 +80,6 @@ run:
@templ generate @templ generate
@go run cmd/main.go @go run cmd/main.go
build-run: build
@go run cmd/main.go
test: build test: build
@echo "Testing..." @echo "Testing..."
@go test ./... -v @go test ./... -v