Moved around more code. Implemented more sqlc. Added support to generate swagger.
Added support for profiling. Removed the pkg module altogether. Everything except old sync is now using code generated by sqlc.
This commit is contained in:
@@ -2,9 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"music-server/internal/backend"
|
||||
"music-server/internal/db"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type IndexHandler struct {
|
||||
@@ -14,6 +15,16 @@ func NewIndexHandler() *IndexHandler {
|
||||
return &IndexHandler{}
|
||||
}
|
||||
|
||||
// GetVersion godoc
|
||||
//
|
||||
// @Summary Getting the version of the backend
|
||||
// @Description get string by ID
|
||||
// @Tags accounts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} backend.VersionData
|
||||
// @Failure 404 {object} string
|
||||
// @Router /version [get]
|
||||
func (i *IndexHandler) GetVersion(ctx echo.Context) error {
|
||||
versionHistory := backend.GetVersionHistory()
|
||||
if versionHistory.Version == "" {
|
||||
@@ -27,6 +38,10 @@ func (i *IndexHandler) GetDBTest(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusOK, "TestedDB")
|
||||
}
|
||||
|
||||
func (i *IndexHandler) HealthCheck(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusOK, db.Health())
|
||||
}
|
||||
|
||||
func (i *IndexHandler) GetCharacters(ctx echo.Context) error {
|
||||
characters := backend.GetCharacters()
|
||||
return ctx.JSON(http.StatusOK, characters)
|
||||
|
||||
@@ -2,11 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"music-server/internal/backend"
|
||||
"music-server/internal/helpers"
|
||||
"music-server/pkg/models"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type MusicHandler struct {
|
||||
@@ -21,13 +20,23 @@ func (m *MusicHandler) GetSong(ctx echo.Context) error {
|
||||
if song == "" {
|
||||
return ctx.String(http.StatusBadRequest, "song can't be empty")
|
||||
}
|
||||
s := backend.GetSong(song)
|
||||
return helpers.SendSong(ctx, s)
|
||||
songPath := backend.GetSong(song)
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
|
||||
song := backend.GetSoundCheckSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetSoundCheckSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
|
||||
@@ -36,18 +45,33 @@ func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
||||
song := backend.GetRandomSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetRandomSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
|
||||
song := backend.GetRandomSongLowChance()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetRandomSongLowChance()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongClassic(ctx echo.Context) error {
|
||||
song := backend.GetRandomSongClassic()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetRandomSongClassic()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetSongInfo(ctx echo.Context) error {
|
||||
@@ -61,13 +85,23 @@ func (m *MusicHandler) GetPlayedSongs(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
|
||||
song := backend.GetNextSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetNextSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
|
||||
song := backend.GetPreviousSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
songPath := backend.GetPreviousSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, err)
|
||||
}
|
||||
defer file.Close()
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetAllGames(ctx echo.Context) error {
|
||||
@@ -80,11 +114,14 @@ func (m *MusicHandler) GetAllGamesRandom(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusOK, gameList)
|
||||
}
|
||||
|
||||
type played struct {
|
||||
Song int
|
||||
}
|
||||
|
||||
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
||||
var played models.Played
|
||||
var played played
|
||||
err := ctx.Bind(&played)
|
||||
if err != nil {
|
||||
//helpers.NewError(ctx, http.StatusBadRequest, err)
|
||||
return ctx.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
backend.SetPlayed(played.Song)
|
||||
|
||||
@@ -7,9 +7,13 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
_ "music-server/cmd/docs"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/middleware"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
echoSwagger "github.com/swaggo/echo-swagger" // echo-swagger middleware
|
||||
//_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.
|
||||
)
|
||||
|
||||
func (s *Server) RegisterRoutes() http.Handler {
|
||||
@@ -36,9 +40,17 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
swagger := http.FileServer(http.FS(web.Swagger))
|
||||
e.GET("/swagger/*", echo.WrapHandler(swagger))
|
||||
|
||||
swaggerRedirect := func(c echo.Context) error {
|
||||
return c.Redirect(http.StatusMovedPermanently, "/doc/index.html")
|
||||
}
|
||||
e.GET("/doc", swaggerRedirect)
|
||||
e.GET("/doc/", swaggerRedirect)
|
||||
e.GET("/doc/*", echoSwagger.WrapHandler)
|
||||
|
||||
index := NewIndexHandler()
|
||||
e.GET("/version", index.GetVersion)
|
||||
e.GET("/health", index.GetDBTest)
|
||||
e.GET("/dbtest", index.GetDBTest)
|
||||
e.GET("/health", index.HealthCheck)
|
||||
e.GET("/character", index.GetCharacter)
|
||||
e.GET("/characters", index.GetCharacters)
|
||||
|
||||
@@ -78,6 +90,5 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
fmt.Printf(" %s %s\n", r.Method, r.Path)
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"music-server/pkg/db"
|
||||
"music-server/internal/db"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -17,7 +17,7 @@ type Server struct {
|
||||
var (
|
||||
host = os.Getenv("DB_HOST")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
database = os.Getenv("DB_NAME")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
username = os.Getenv("DB_USERNAME")
|
||||
password = os.Getenv("DB_PASSWORD")
|
||||
musicPath = os.Getenv("MUSIC_PATH")
|
||||
@@ -31,18 +31,18 @@ func NewServer() *http.Server {
|
||||
}
|
||||
|
||||
//conf.SetupDb()
|
||||
if host == "" || dbPort == "" || username == "" || password == "" || database == "" || musicPath == "" {
|
||||
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" {
|
||||
log.Fatal("Invalid settings")
|
||||
}
|
||||
|
||||
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
|
||||
host, dbPort, username, password, database)
|
||||
host, dbPort, username, password, dbName)
|
||||
|
||||
log.Printf("Path: %s\n", musicPath)
|
||||
|
||||
db.Migrate_db(host, dbPort, username, password, database)
|
||||
db.Migrate_db(host, dbPort, username, password, dbName)
|
||||
|
||||
db.InitDB(host, dbPort, username, password, database)
|
||||
db.InitDB(host, dbPort, username, password, dbName)
|
||||
|
||||
// Declare Server config
|
||||
server := &http.Server{
|
||||
|
||||
@@ -3,9 +3,10 @@ package server
|
||||
import (
|
||||
"log"
|
||||
"music-server/internal/backend"
|
||||
"music-server/internal/database"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type SyncHandler struct {
|
||||
@@ -16,13 +17,13 @@ func NewSyncHandler() *SyncHandler {
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGames(ctx echo.Context) error {
|
||||
backend.SyncGames()
|
||||
database.SyncGames()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesQuick(ctx echo.Context) error {
|
||||
backend.SyncGamesQuick()
|
||||
database.SyncGamesQuick()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user