06cbad708d
- 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>
146 lines
4.8 KiB
Go
146 lines
4.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"
|
|
"music-server/internal/logging"
|
|
)
|
|
|
|
// @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)
|
|
|
|
health := NewHealthHandler()
|
|
e.GET("/health", health.HealthCheck)
|
|
|
|
version := NewVersionHandler()
|
|
e.GET("/version", version.GetLatestVersion)
|
|
e.GET("/version/history", version.GetVersionHistory)
|
|
|
|
character := NewCharacterHandler()
|
|
e.GET("/character", character.GetCharacter)
|
|
e.GET("/characters", character.GetCharacterList)
|
|
|
|
download := NewDownloadHandler()
|
|
e.GET("/download", download.checkLatest)
|
|
e.GET("/download/list", download.listAssetsOfLatest)
|
|
e.GET("/download/windows", download.downloadLatestWindows)
|
|
e.GET("/download/linux", download.downloadLatestLinux)
|
|
|
|
sync := NewSyncHandler()
|
|
syncGroup := e.Group("/sync")
|
|
syncGroup.GET("", sync.SyncGamesNewOnlyChanges)
|
|
syncGroup.GET("/progress", sync.SyncProgress)
|
|
syncGroup.GET("/new", sync.SyncGamesNewOnlyChanges)
|
|
syncGroup.GET("/full", sync.SyncGamesNewFull)
|
|
syncGroup.GET("/new/full", sync.SyncGamesNewFull)
|
|
syncGroup.GET("/quick", sync.SyncGamesNewOnlyChanges)
|
|
syncGroup.GET("/reset", sync.ResetGames)
|
|
|
|
music := NewMusicHandler()
|
|
musicGroup := e.Group("/music")
|
|
musicGroup.GET("", music.GetSong)
|
|
musicGroup.GET("/soundTest", music.GetSoundCheckSong)
|
|
musicGroup.GET("/reset", music.ResetMusic)
|
|
musicGroup.GET("/rand", music.GetRandomSong)
|
|
musicGroup.GET("/rand/low", music.GetRandomSongLowChance)
|
|
musicGroup.GET("/rand/classic", music.GetRandomSongClassic)
|
|
musicGroup.GET("/info", music.GetSongInfo)
|
|
musicGroup.GET("/list", music.GetPlayedSongs)
|
|
musicGroup.GET("/next", music.GetNextSong)
|
|
musicGroup.GET("/previous", music.GetPreviousSong)
|
|
musicGroup.GET("/all", music.GetAllGamesRandom)
|
|
musicGroup.GET("/all/order", music.GetAllGames)
|
|
musicGroup.GET("/all/random", music.GetAllGamesRandom)
|
|
musicGroup.PUT("/played", music.PutPlayed)
|
|
musicGroup.GET("/addQue", music.AddLatestToQue)
|
|
musicGroup.GET("/addPlayed", 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 - will be used by VGMQ and Statistics API
|
|
_ = apiV1.Group("", tokenAuthMiddleware)
|
|
|
|
// Note: Future protected endpoints (VGMQ, Statistics) will be added here
|
|
|
|
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
|
|
}
|