90d621c195
- 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>
174 lines
6.7 KiB
Go
174 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"music-server/cmd/web"
|
|
"music-server/internal/logging"
|
|
"music-server/internal/server/middleware"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v5"
|
|
echoMiddleware "github.com/labstack/echo/v5/middleware"
|
|
echoSwagger "github.com/swaggo/echo-swagger/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// @Title MusicServer API
|
|
// @version 1.0
|
|
// @description API for the MusicServer application
|
|
// @termsOfService http://sanplex.xyz/terms/
|
|
|
|
// @contact.name Sebastian Olsson
|
|
// @contact.email zarnor91@gmail.com
|
|
|
|
// @license.name MIT
|
|
// @license.url http://opensource.org/licenses/MIT
|
|
|
|
// @host localhost:8080
|
|
// @BasePath /
|
|
func (s *Server) RegisterRoutes() http.Handler {
|
|
e := echo.New()
|
|
|
|
// Serve OpenAPI spec at /openapi
|
|
e.GET("/openapi", echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.ServeFile(w, r, "cmd/docs/swagger.json")
|
|
})))
|
|
e.Use(logging.RequestLogger())
|
|
e.Use(echoMiddleware.Recover())
|
|
|
|
e.Use(echoMiddleware.CORSWithConfig(echoMiddleware.CORSConfig{
|
|
AllowOrigins: []string{"https://*", "http://*"},
|
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
|
AllowHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
|
AllowCredentials: true,
|
|
MaxAge: 300,
|
|
}))
|
|
|
|
fileServer := http.FileServer(http.FS(web.Assets))
|
|
e.GET("/assets/*", echo.WrapHandler(fileServer))
|
|
|
|
e.GET("/search", echo.WrapHandler(templ.Handler(web.HelloForm())))
|
|
e.POST("/find", echo.WrapHandler(http.HandlerFunc(web.FindGameWebHandler)))
|
|
|
|
e.Static("/", "/frontend")
|
|
|
|
// Swagger UI
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
|
|
// ============================================
|
|
// Legacy Endpoints (Deprecated - use /api/v1/ instead)
|
|
// ============================================
|
|
deprecatedMiddleware := middleware.DeprecationMiddleware
|
|
|
|
index := NewIndexHandler()
|
|
e.GET("/version", deprecatedMiddleware(index.GetVersion))
|
|
e.GET("/dbtest", deprecatedMiddleware(index.GetDBTest))
|
|
e.GET("/health", deprecatedMiddleware(index.HealthCheck))
|
|
e.GET("/character", deprecatedMiddleware(index.GetCharacter))
|
|
e.GET("/characters", deprecatedMiddleware(index.GetCharacterList))
|
|
|
|
download := NewDownloadHandler()
|
|
e.GET("/download", deprecatedMiddleware(download.checkLatest))
|
|
e.GET("/download/list", deprecatedMiddleware(download.listAssetsOfLatest))
|
|
e.GET("/download/windows", deprecatedMiddleware(download.downloadLatestWindows))
|
|
e.GET("/download/linux", deprecatedMiddleware(download.downloadLatestLinux))
|
|
|
|
sync := NewSyncHandler()
|
|
syncGroup := e.Group("/sync")
|
|
syncGroup.GET("", deprecatedMiddleware(sync.SyncSoundtracksNewOnlyChanges))
|
|
syncGroup.GET("/progress", deprecatedMiddleware(sync.SyncProgress))
|
|
syncGroup.GET("/new", deprecatedMiddleware(sync.SyncSoundtracksNewOnlyChanges))
|
|
syncGroup.GET("/full", deprecatedMiddleware(sync.SyncSoundtracksNewFull))
|
|
syncGroup.GET("/new/full", deprecatedMiddleware(sync.SyncSoundtracksNewFull))
|
|
syncGroup.GET("/quick", deprecatedMiddleware(sync.SyncSoundtracksNewOnlyChanges))
|
|
syncGroup.GET("/reset", deprecatedMiddleware(sync.ResetDB))
|
|
|
|
music := NewMusicHandler()
|
|
musicGroup := e.Group("/music")
|
|
musicGroup.GET("", deprecatedMiddleware(music.GetSong))
|
|
musicGroup.GET("/soundTest", deprecatedMiddleware(music.GetSoundCheckSong))
|
|
musicGroup.GET("/reset", deprecatedMiddleware(music.ResetMusic))
|
|
musicGroup.GET("/rand", deprecatedMiddleware(music.GetRandomSong))
|
|
musicGroup.GET("/rand/low", deprecatedMiddleware(music.GetRandomSongLowChance))
|
|
musicGroup.GET("/rand/classic", deprecatedMiddleware(music.GetRandomSongClassic))
|
|
musicGroup.GET("/info", deprecatedMiddleware(music.GetSongInfo))
|
|
musicGroup.GET("/list", deprecatedMiddleware(music.GetPlayedSongs))
|
|
musicGroup.GET("/next", deprecatedMiddleware(music.GetNextSong))
|
|
musicGroup.GET("/previous", deprecatedMiddleware(music.GetPreviousSong))
|
|
musicGroup.GET("/all", deprecatedMiddleware(music.GetAllSoundtracksRandom))
|
|
musicGroup.GET("/all/order", deprecatedMiddleware(music.GetAllSoundtracks))
|
|
musicGroup.GET("/all/random", deprecatedMiddleware(music.GetAllSoundtracksRandom))
|
|
musicGroup.PUT("/played", deprecatedMiddleware(music.PutPlayed))
|
|
musicGroup.GET("/addQue", deprecatedMiddleware(music.AddLatestToQue))
|
|
musicGroup.GET("/addPlayed", deprecatedMiddleware(music.AddLatestPlayed))
|
|
|
|
// ============================================
|
|
// API v1 Routes with Token Authentication
|
|
// ============================================
|
|
|
|
// Create /api/v1 group
|
|
apiV1 := e.Group("/api/v1")
|
|
|
|
// Public endpoints - no token required
|
|
apiV1.POST("/token", func(c *echo.Context) error {
|
|
return s.tokenHandler.CreateTokenHandler(c)
|
|
})
|
|
apiV1.DELETE("/token", func(c *echo.Context) error {
|
|
return s.tokenHandler.DeleteTokenHandler(c)
|
|
})
|
|
apiV1.POST("/token/cleanup", func(c *echo.Context) error {
|
|
return s.tokenHandler.CleanupExpiredSessionsHandler(c)
|
|
})
|
|
|
|
// Protected endpoints - require valid token
|
|
// Create token auth middleware with pool access
|
|
tokenAuthMiddleware := middleware.TokenAuthMiddleware(s.db.Pool)
|
|
|
|
// Protected group with token authentication
|
|
protectedV1 := apiV1.Group("", tokenAuthMiddleware)
|
|
|
|
// Statistics API endpoints (protected by token auth)
|
|
statistics := s.statisticsHandler
|
|
protectedV1.GET("/statistics/games/most-played", func(c *echo.Context) error {
|
|
return statistics.GetMostPlayedGames(c)
|
|
})
|
|
protectedV1.GET("/statistics/games/least-played", func(c *echo.Context) error {
|
|
return statistics.GetLeastPlayedGames(c)
|
|
})
|
|
protectedV1.GET("/statistics/games/never-played", func(c *echo.Context) error {
|
|
return statistics.GetNeverPlayedGames(c)
|
|
})
|
|
protectedV1.GET("/statistics/games/last-played", func(c *echo.Context) error {
|
|
return statistics.GetLastPlayedGames(c)
|
|
})
|
|
protectedV1.GET("/statistics/games/oldest-played", func(c *echo.Context) error {
|
|
return statistics.GetOldestPlayedGames(c)
|
|
})
|
|
protectedV1.GET("/statistics/songs/most-played", func(c *echo.Context) error {
|
|
return statistics.GetMostPlayedSongs(c)
|
|
})
|
|
protectedV1.GET("/statistics/songs/least-played", func(c *echo.Context) error {
|
|
return statistics.GetLeastPlayedSongs(c)
|
|
})
|
|
protectedV1.GET("/statistics/summary", func(c *echo.Context) error {
|
|
return statistics.GetStatisticsSummary(c)
|
|
})
|
|
|
|
// Future: VGMQ endpoints will be added to protectedV1 group
|
|
_ = protectedV1 // Use the variable to avoid unused variable error
|
|
|
|
routes := e.Router().Routes()
|
|
sort.Slice(routes, func(i, j int) bool {
|
|
return routes[i].Path < routes[j].Path
|
|
})
|
|
for _, r := range routes {
|
|
if (r.Method == "GET" || r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE") && !strings.Contains(r.Name, "github") {
|
|
logging.GetLogger().Debug("Registered route", zap.String("method", r.Method), zap.String("path", r.Path))
|
|
}
|
|
}
|
|
return e
|
|
}
|