Files
MusicServer/internal/server/routes.go
T
Sansan 2f407f6eef 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-08 20:36:12 +02:00

178 lines
6.8 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
health := NewHealthHandler()
e.GET("/health", deprecatedMiddleware(health.HealthCheck))
version := NewVersionHandler()
e.GET("/version", deprecatedMiddleware(version.GetLatestVersion))
e.GET("/version/history", deprecatedMiddleware(version.GetVersionHistory))
character := NewCharacterHandler()
e.GET("/character", deprecatedMiddleware(character.GetCharacter))
e.GET("/characters", deprecatedMiddleware(character.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.SyncGamesNewOnlyChanges))
syncGroup.GET("/progress", deprecatedMiddleware(sync.SyncProgress))
syncGroup.GET("/new", deprecatedMiddleware(sync.SyncGamesNewOnlyChanges))
syncGroup.GET("/full", deprecatedMiddleware(sync.SyncGamesNewFull))
syncGroup.GET("/new/full", deprecatedMiddleware(sync.SyncGamesNewFull))
syncGroup.GET("/quick", deprecatedMiddleware(sync.SyncGamesNewOnlyChanges))
syncGroup.GET("/reset", deprecatedMiddleware(sync.ResetGames))
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.GetAllGamesRandom))
musicGroup.GET("/all/order", deprecatedMiddleware(music.GetAllGames))
musicGroup.GET("/all/random", deprecatedMiddleware(music.GetAllGamesRandom))
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
}