85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/toyffee/party-mix/internal/handlers"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func New(db *gorm.DB, jwtSecret string) *gin.Engine {
|
|
r := gin.Default()
|
|
|
|
r.Use(cors.New(cors.Config{
|
|
AllowAllOrigins: true,
|
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
|
|
ExposeHeaders: []string{"Content-Length", "Content-Range", "Accept-Ranges"},
|
|
AllowCredentials: false,
|
|
}))
|
|
|
|
r.GET("/health", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
})
|
|
|
|
proxy := r.Group("/api/proxy")
|
|
{
|
|
proxy.GET("/search", handlers.SearchHandler)
|
|
proxy.GET("/top", handlers.TopChartsHandler)
|
|
proxy.GET("/img", handlers.ImgProxyHandler)
|
|
proxy.GET("/mp3", handlers.MP3ProxyHandler)
|
|
proxy.GET("/yandex-playlist", handlers.YandexPlaylistHandler)
|
|
}
|
|
|
|
auth := r.Group("/api/auth")
|
|
{
|
|
auth.POST("/register", handlers.Register(db))
|
|
auth.POST("/login", handlers.Login(db, jwtSecret))
|
|
auth.GET("/me", handlers.AuthRequired(jwtSecret), handlers.Me(db))
|
|
}
|
|
|
|
r.GET("/api/playlists/public", handlers.GetPublicPlaylists(db))
|
|
|
|
versions := r.Group("/api/versions", handlers.AuthRequired(jwtSecret))
|
|
{
|
|
versions.GET("", handlers.GetVersions(db))
|
|
versions.POST("", handlers.SaveVersion(db))
|
|
versions.DELETE("", handlers.DeleteVersion(db))
|
|
}
|
|
|
|
playlists := r.Group("/api/playlists", handlers.AuthRequired(jwtSecret))
|
|
{
|
|
playlists.GET("", handlers.GetPlaylists(db))
|
|
playlists.POST("", handlers.CreatePlaylist(db))
|
|
playlists.GET("/:id", handlers.GetPlaylist(db))
|
|
playlists.PUT("/:id", handlers.UpdatePlaylist(db))
|
|
playlists.DELETE("/:id", handlers.DeletePlaylist(db))
|
|
playlists.POST("/:id/tracks", handlers.AddTrackToPlaylist(db))
|
|
}
|
|
|
|
remote := r.Group("/api/remote")
|
|
{
|
|
remote.POST("", handlers.CreateRemoteRoom)
|
|
remote.PUT("/:id/state", handlers.PushRemoteState)
|
|
remote.GET("/:id/state", handlers.GetRemoteState)
|
|
remote.POST("/:id/command", handlers.SendRemoteCommand)
|
|
remote.GET("/:id/commands", handlers.PollRemoteCommands)
|
|
}
|
|
|
|
parties := r.Group("/api/parties")
|
|
{
|
|
parties.POST("", handlers.CreateParty(db))
|
|
parties.GET("/:id", handlers.GetParty(db))
|
|
parties.GET("/code/:code", handlers.GetPartyByCode(db))
|
|
parties.POST("/:id/participants", handlers.AddParticipant(db))
|
|
parties.DELETE("/:id/participants/:pid", handlers.RemoveParticipant(db))
|
|
parties.POST("/:id/history", handlers.AddHistory(db))
|
|
parties.GET("/:id/history", handlers.GetHistory(db))
|
|
parties.DELETE("/:id/history", handlers.ClearHistory(db))
|
|
}
|
|
|
|
return r
|
|
}
|