Replaced the gin framwwork with echo

This commit is contained in:
2025-01-13 11:57:48 +01:00
parent 8e2d22b899
commit 034ba35fbb
32 changed files with 348 additions and 266 deletions

82
internal/server/routes.go Normal file
View File

@@ -0,0 +1,82 @@
package server
import (
"fmt"
"music-server/cmd/web"
"music-server/pkg/api"
"net/http"
"sort"
"strings"
"github.com/a-h/templ"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func (s *Server) RegisterRoutes() http.Handler {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.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 := http.FileServer(http.FS(web.Swagger))
e.GET("/swagger/*", echo.WrapHandler(swagger))
index := api.NewIndex()
e.GET("/version", index.GetVersion)
e.GET("/health", index.GetDBTest)
sync := api.NewSync()
syncGroup := e.Group("/sync")
syncGroup.GET("", sync.SyncGames)
syncGroup.GET("/new", sync.SyncGamesNewOnlyChanges)
syncGroup.GET("/new/full", sync.SyncGamesNewFull)
syncGroup.GET("/quick", sync.SyncGamesQuick)
syncGroup.GET("/reset", sync.ResetGames)
music := api.NewMusic()
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)
routes := e.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") {
fmt.Printf(" %s %s\n", r.Method, r.Path)
}
}
return e
}

35
internal/server/server.go Normal file
View File

@@ -0,0 +1,35 @@
package server
import (
"fmt"
"music-server/pkg/conf"
"net/http"
"os"
"strconv"
"time"
)
type Server struct {
port int
}
func NewServer() *http.Server {
port, _ := strconv.Atoi(os.Getenv("PORT"))
NewServer := &Server{
port: port,
}
conf.SetupDb()
// Declare Server config
server := &http.Server{
Addr: fmt.Sprintf(":%d", NewServer.port),
Handler: NewServer.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
return server
}