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:
@@ -1,18 +1,23 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/models"
|
||||
"music-server/internal/db"
|
||||
)
|
||||
|
||||
func TestDB() {
|
||||
db.Testf()
|
||||
}
|
||||
|
||||
func GetVersionHistory() models.VersionData {
|
||||
data := models.VersionData{Version: "3.2",
|
||||
type VersionData struct {
|
||||
Version string `json:"version" example:"1.0.0"`
|
||||
Changelog string `json:"changelog" example:"account name"`
|
||||
History []VersionData `json:"history"`
|
||||
}
|
||||
|
||||
func GetVersionHistory() VersionData {
|
||||
data := VersionData{Version: "3.2",
|
||||
Changelog: "Upgraded Go version and the version of all dependencies. Fixed som more bugs.",
|
||||
History: []models.VersionData{
|
||||
History: []VersionData{
|
||||
{
|
||||
Version: "3.1",
|
||||
Changelog: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.",
|
||||
|
||||
@@ -3,17 +3,48 @@ package backend
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/models"
|
||||
"music-server/internal/db"
|
||||
"music-server/internal/db/repository"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SongInfo struct {
|
||||
Game string `json:"Game"`
|
||||
GamePlayed int32 `json:"GamePlayed"`
|
||||
Song string `json:"Song"`
|
||||
SongPlayed int32 `json:"SongPlayed"`
|
||||
CurrentlyPlaying bool `json:"CurrentlyPlaying"`
|
||||
SongNo int `json:"SongNo"`
|
||||
}
|
||||
|
||||
var currentSong = -1
|
||||
var games []models.GameData
|
||||
var songQue []models.SongData
|
||||
var lastFetched models.SongData
|
||||
|
||||
// var games []models.GameData
|
||||
var gamesNew []repository.Game
|
||||
|
||||
// var songQue []models.SongData
|
||||
var songQueNew []repository.Song
|
||||
|
||||
// var lastFetched models.SongData
|
||||
var lastFetchedNew repository.Song
|
||||
var repo *repository.Queries
|
||||
|
||||
func initRepo() {
|
||||
if repo == nil {
|
||||
repo = repository.New(db.Dbpool)
|
||||
}
|
||||
}
|
||||
|
||||
func getAllGames() []repository.Game {
|
||||
if len(gamesNew) == 0 {
|
||||
initRepo()
|
||||
gamesNew, _ = repo.FindAllGames(db.Ctx)
|
||||
}
|
||||
return gamesNew
|
||||
|
||||
}
|
||||
|
||||
func GetSoundCheckSong() string {
|
||||
files, err := os.ReadDir("songs")
|
||||
@@ -25,105 +56,125 @@ func GetSoundCheckSong() string {
|
||||
}
|
||||
|
||||
func Reset() {
|
||||
songQue = nil
|
||||
songQueNew = nil
|
||||
currentSong = -1
|
||||
games = db.FindAllGames()
|
||||
initRepo()
|
||||
gamesNew, _ = repo.FindAllGames(db.Ctx)
|
||||
//games = db.FindAllGames()
|
||||
}
|
||||
|
||||
func AddLatestToQue() {
|
||||
if lastFetched.Path != "" {
|
||||
currentSong = len(songQue)
|
||||
songQue = append(songQue, lastFetched)
|
||||
lastFetched = models.SongData{}
|
||||
if lastFetchedNew.Path != "" {
|
||||
currentSong = len(songQueNew)
|
||||
songQueNew = append(songQueNew, lastFetchedNew)
|
||||
lastFetchedNew = repository.Song{}
|
||||
}
|
||||
}
|
||||
|
||||
func AddLatestPlayed() {
|
||||
if len(songQue) == 0 {
|
||||
if len(songQueNew) == 0 {
|
||||
return
|
||||
}
|
||||
var currentSongData = songQue[currentSong]
|
||||
currentSongData := songQueNew[currentSong]
|
||||
|
||||
db.AddGamePlayed(currentSongData.GameId)
|
||||
db.AddSongPlayed(currentSongData.GameId, currentSongData.SongName)
|
||||
initRepo()
|
||||
repo.AddGamePlayed(db.Ctx, currentSongData.GameID)
|
||||
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: currentSongData.GameID, SongName: currentSongData.SongName})
|
||||
//db.AddGamePlayed(currentSongData.GameId)
|
||||
//db.AddSongPlayed(currentSongData.GameId, currentSongData.SongName)
|
||||
}
|
||||
|
||||
func SetPlayed(songNumber int) {
|
||||
if len(songQue) == 0 || songNumber >= len(songQue) {
|
||||
if len(songQueNew) == 0 || songNumber >= len(songQueNew) {
|
||||
return
|
||||
}
|
||||
var songData = songQue[songNumber]
|
||||
db.AddGamePlayed(songData.GameId)
|
||||
db.AddSongPlayed(songData.GameId, songData.SongName)
|
||||
songData := songQueNew[songNumber]
|
||||
initRepo()
|
||||
repo.AddGamePlayed(db.Ctx, songData.GameID)
|
||||
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: songData.GameID, SongName: songData.SongName})
|
||||
//db.AddGamePlayed(songData.GameId)
|
||||
//db.AddSongPlayed(songData.GameId, songData.SongName)
|
||||
}
|
||||
|
||||
func GetRandomSong() string {
|
||||
if len(games) == 0 {
|
||||
/*if len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
if len(games) == 0 {
|
||||
}*/
|
||||
getAllGames()
|
||||
if len(gamesNew) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
song := getSongFromList(games)
|
||||
lastFetched = song
|
||||
song := getSongFromList(gamesNew)
|
||||
lastFetchedNew = song
|
||||
return song.Path
|
||||
}
|
||||
|
||||
func GetRandomSongLowChance() string {
|
||||
if len(games) == 0 {
|
||||
/*if len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
var listOfGames []models.GameData
|
||||
//var listOfGames []models.GameData
|
||||
var listOfGames []repository.Game
|
||||
|
||||
var averagePlayed = getAveragePlayed(games)
|
||||
var averagePlayed = getAveragePlayed()
|
||||
|
||||
for _, data := range games {
|
||||
var timesToAdd = averagePlayed - data.TimesPlayed
|
||||
for _, data := range gamesNew {
|
||||
timesToAdd := averagePlayed - data.TimesPlayed
|
||||
if timesToAdd <= 0 {
|
||||
listOfGames = append(listOfGames, data)
|
||||
} else {
|
||||
for i := 0; i < timesToAdd; i++ {
|
||||
for i := int32(0); i < timesToAdd; i++ {
|
||||
listOfGames = append(listOfGames, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
song := getSongFromList(listOfGames)
|
||||
|
||||
lastFetched = song
|
||||
lastFetchedNew = song
|
||||
return song.Path
|
||||
|
||||
}
|
||||
|
||||
func GetRandomSongClassic() string {
|
||||
if games == nil || len(games) == 0 {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
}*/
|
||||
|
||||
var listOfAllSongs []models.SongData
|
||||
for _, game := range games {
|
||||
listOfAllSongs = append(listOfAllSongs, db.FindSongsFromGame(game.Id)...)
|
||||
getAllGames()
|
||||
|
||||
var listOfAllSongs []repository.Song
|
||||
for _, game := range gamesNew {
|
||||
//listOfAllSongs = append(listOfAllSongs, db.FindSongsFromGame(game.Id)...)
|
||||
songList, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
|
||||
listOfAllSongs = append(listOfAllSongs, songList...)
|
||||
}
|
||||
|
||||
songFound := false
|
||||
var song models.SongData
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
song = listOfAllSongs[rand.Intn(len(listOfAllSongs))]
|
||||
gameData, err := db.GetGameById(song.GameId)
|
||||
//gameData, err := db.GetGameById(song.GameId)
|
||||
gameData, err := repo.GetGameById(db.Ctx, song.GameID)
|
||||
|
||||
if err != nil {
|
||||
db.RemoveBrokenSong(song)
|
||||
log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, gameData.GameName, song.FileName)
|
||||
continue
|
||||
}
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil || gameData.Path+song.FileName != song.Path {
|
||||
if err != nil || (song.FileName != nil && gameData.Path+*song.FileName != song.Path) {
|
||||
//File not found
|
||||
db.RemoveBrokenSong(song)
|
||||
log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, gameData.GameName, song.FileName)
|
||||
} else {
|
||||
songFound = true
|
||||
}
|
||||
@@ -132,19 +183,19 @@ func GetRandomSongClassic() string {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
lastFetched = song
|
||||
lastFetchedNew = song
|
||||
return song.Path
|
||||
}
|
||||
|
||||
func GetSongInfo() models.SongInfo {
|
||||
if songQue == nil {
|
||||
return models.SongInfo{}
|
||||
func GetSongInfo() SongInfo {
|
||||
if songQueNew == nil {
|
||||
return SongInfo{}
|
||||
}
|
||||
var currentSongData = songQue[currentSong]
|
||||
var currentSongData = songQueNew[currentSong]
|
||||
|
||||
currentGameData := getCurrentGame(currentSongData)
|
||||
|
||||
return models.SongInfo{
|
||||
return SongInfo{
|
||||
Game: currentGameData.GameName,
|
||||
GamePlayed: currentGameData.TimesPlayed,
|
||||
Song: currentSongData.SongName,
|
||||
@@ -154,12 +205,12 @@ func GetSongInfo() models.SongInfo {
|
||||
}
|
||||
}
|
||||
|
||||
func GetPlayedSongs() []models.SongInfo {
|
||||
var songList []models.SongInfo
|
||||
func GetPlayedSongs() []SongInfo {
|
||||
var songList []SongInfo
|
||||
|
||||
for i, song := range songQue {
|
||||
for i, song := range songQueNew {
|
||||
gameData := getCurrentGame(song)
|
||||
songList = append(songList, models.SongInfo{
|
||||
songList = append(songList, SongInfo{
|
||||
Game: gameData.GameName,
|
||||
GamePlayed: gameData.TimesPlayed,
|
||||
Song: song.SongName,
|
||||
@@ -173,34 +224,36 @@ func GetPlayedSongs() []models.SongInfo {
|
||||
|
||||
func GetSong(song string) string {
|
||||
currentSong, _ = strconv.Atoi(song)
|
||||
if currentSong >= len(songQue) {
|
||||
currentSong = len(songQue) - 1
|
||||
if currentSong >= len(songQueNew) {
|
||||
currentSong = len(songQueNew) - 1
|
||||
} else if currentSong < 0 {
|
||||
currentSong = 0
|
||||
}
|
||||
var songData = songQue[currentSong]
|
||||
songData := songQueNew[currentSong]
|
||||
return songData.Path
|
||||
}
|
||||
|
||||
func GetAllGames() []string {
|
||||
if games == nil || len(games) == 0 {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
var jsonArray []string
|
||||
for _, game := range games {
|
||||
for _, game := range gamesNew {
|
||||
jsonArray = append(jsonArray, game.GameName)
|
||||
}
|
||||
return jsonArray
|
||||
}
|
||||
|
||||
func GetAllGamesRandom() []string {
|
||||
if games == nil || len(games) == 0 {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
var jsonArray []string
|
||||
for _, game := range games {
|
||||
for _, game := range gamesNew {
|
||||
jsonArray = append(jsonArray, game.GameName)
|
||||
}
|
||||
rand.Shuffle(len(jsonArray), func(i, j int) { jsonArray[i], jsonArray[j] = jsonArray[j], jsonArray[i] })
|
||||
@@ -208,40 +261,41 @@ func GetAllGamesRandom() []string {
|
||||
}
|
||||
|
||||
func GetNextSong() string {
|
||||
if songQue == nil {
|
||||
if songQueNew == nil {
|
||||
return ""
|
||||
}
|
||||
if currentSong == len(songQue)-1 || currentSong == -1 {
|
||||
var songData = songQue[currentSong]
|
||||
if currentSong == len(songQueNew)-1 || currentSong == -1 {
|
||||
songData := songQueNew[currentSong]
|
||||
return songData.Path
|
||||
} else {
|
||||
currentSong = currentSong + 1
|
||||
var songData = songQue[currentSong]
|
||||
songData := songQueNew[currentSong]
|
||||
return songData.Path
|
||||
}
|
||||
}
|
||||
|
||||
func GetPreviousSong() string {
|
||||
if songQue == nil {
|
||||
if songQueNew == nil {
|
||||
return ""
|
||||
}
|
||||
if currentSong == -1 || currentSong == 0 {
|
||||
var songData = songQue[0]
|
||||
songData := songQueNew[0]
|
||||
return songData.Path
|
||||
} else {
|
||||
currentSong = currentSong - 1
|
||||
var songData = songQue[currentSong]
|
||||
songData := songQueNew[currentSong]
|
||||
return songData.Path
|
||||
}
|
||||
}
|
||||
|
||||
func getSongFromList(games []models.GameData) models.SongData {
|
||||
func getSongFromList(games []repository.Game) repository.Song {
|
||||
songFound := false
|
||||
var song models.SongData
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
game := getRandomGame(games)
|
||||
//log.Println("game = ", game)
|
||||
songs := db.FindSongsFromGame(game.Id)
|
||||
//songs := db.FindSongsFromGame(game.Id)
|
||||
songs, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
|
||||
//log.Println("songs = ", songs)
|
||||
if len(songs) == 0 {
|
||||
continue
|
||||
@@ -254,10 +308,12 @@ func getSongFromList(games []models.GameData) models.SongData {
|
||||
//log.Println("game.Path+song.FileName: ", game.Path+song.FileName)
|
||||
//log.Println("song.Path: ", song.Path)
|
||||
//log.Println("game.Path+song.FileName != song.Path: ", game.Path+song.FileName != song.Path)
|
||||
if err != nil || game.Path+song.FileName != song.Path || strings.HasSuffix(song.FileName, ".wav") {
|
||||
if err != nil || (song.FileName != nil && game.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
|
||||
//File not found
|
||||
db.RemoveBrokenSong(song)
|
||||
log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' FileName: " + song.FileName)
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, game.GameName, song.FileName)
|
||||
} else {
|
||||
songFound = true
|
||||
}
|
||||
@@ -270,23 +326,24 @@ func getSongFromList(games []models.GameData) models.SongData {
|
||||
return song
|
||||
}
|
||||
|
||||
func getCurrentGame(currentSongData models.SongData) models.GameData {
|
||||
for _, game := range games {
|
||||
if game.Id == currentSongData.GameId {
|
||||
func getCurrentGame(currentSongData repository.Song) repository.Game {
|
||||
for _, game := range gamesNew {
|
||||
if game.ID == currentSongData.GameID {
|
||||
return game
|
||||
}
|
||||
}
|
||||
return models.GameData{}
|
||||
return repository.Game{}
|
||||
}
|
||||
|
||||
func getAveragePlayed(gameList []models.GameData) int {
|
||||
var sum int
|
||||
for _, data := range gameList {
|
||||
func getAveragePlayed() int32 {
|
||||
getAllGames()
|
||||
var sum int32
|
||||
for _, data := range gamesNew {
|
||||
sum += data.TimesPlayed
|
||||
}
|
||||
return sum / len(gameList)
|
||||
return sum / int32(len(gamesNew))
|
||||
}
|
||||
|
||||
func getRandomGame(listOfGames []models.GameData) models.GameData {
|
||||
func getRandomGame(listOfGames []repository.Game) repository.Game {
|
||||
return listOfGames[rand.Intn(len(listOfGames))]
|
||||
}
|
||||
|
||||
@@ -8,9 +8,8 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"music-server/db/repository"
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/models"
|
||||
"music-server/internal/db"
|
||||
"music-server/internal/db/repository"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -63,203 +62,14 @@ func (gs GameStatus) String() string {
|
||||
}
|
||||
|
||||
var syncWg sync.WaitGroup
|
||||
var repo *repository.Queries
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func SyncGames() {
|
||||
start := time.Now()
|
||||
host := os.Getenv("DB_HOST")
|
||||
var dir string
|
||||
if host != "" {
|
||||
dir = "/sorted/"
|
||||
} else {
|
||||
dir = "/Users/sebastian/ResilioSync/Sorterat_test/"
|
||||
}
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
db.SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
func SyncGamesQuick() {
|
||||
start := time.Now()
|
||||
host := os.Getenv("DB_HOST")
|
||||
var dir string
|
||||
if host != "" {
|
||||
dir = "/sorted/"
|
||||
} else {
|
||||
dir = "/Users/sebastian/ResilioSync/Sorterat_test/"
|
||||
}
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
db.SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
func syncGame(file os.DirEntry, foldersToSkip []string, dir string) {
|
||||
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
|
||||
fmt.Println(file.Name())
|
||||
path := dir + file.Name() + "/"
|
||||
fmt.Println(path)
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id := -1
|
||||
for _, entry := range entries {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id = getIdFromFile(fileInfo)
|
||||
if id != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if id == -1 {
|
||||
addNewGame(file.Name(), path)
|
||||
} else {
|
||||
checkIfChanged(id, file.Name(), path)
|
||||
checkSongs(path, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ResetDB() {
|
||||
db.ClearSongs(-1)
|
||||
db.ClearGames()
|
||||
//db.ClearSongs(-1)
|
||||
repo.ClearSongs(db.Ctx)
|
||||
//db.ClearGames()
|
||||
repo.ClearGames(db.Ctx)
|
||||
}
|
||||
|
||||
func getIdFromFile(file os.FileInfo) int {
|
||||
name := file.Name()
|
||||
if !file.IsDir() && strings.HasSuffix(name, ".id") {
|
||||
name = strings.Replace(name, ".id", "", 1)
|
||||
name = strings.Replace(name, ".", "", 1)
|
||||
i, _ := strconv.Atoi(name)
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func checkIfChanged(id int, name string, path string) {
|
||||
fmt.Printf("Id from file: %v\n", id)
|
||||
nameFromDb := db.GetGameName(id)
|
||||
fmt.Printf("Name from file: %v\n", name)
|
||||
fmt.Printf("Name from DB: %v\n", nameFromDb)
|
||||
if nameFromDb == "" {
|
||||
fmt.Println("Not in db")
|
||||
db.InsertGameWithExistingId(id, name, path)
|
||||
fmt.Println("Added to db")
|
||||
} else if name != nameFromDb {
|
||||
fmt.Println("Diff name")
|
||||
db.UpdateGameName(id, name, path)
|
||||
checkBrokenSongs()
|
||||
}
|
||||
db.RemoveDeletionDate(id)
|
||||
}
|
||||
|
||||
func addNewGame(name string, path string) {
|
||||
newId := db.GetIdByGameName(name)
|
||||
if newId != -1 {
|
||||
checkBrokenSongs()
|
||||
db.RemoveDeletionDate(newId)
|
||||
} else {
|
||||
newId = db.InsertGame(name, path)
|
||||
}
|
||||
|
||||
fmt.Printf("newId = %v", newId)
|
||||
fileName := path + "/." + strconv.Itoa(newId) + ".id"
|
||||
fmt.Printf("fileName = %v", fileName)
|
||||
|
||||
err := os.WriteFile(fileName, nil, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
checkSongs(path, newId)
|
||||
}
|
||||
|
||||
func checkSongs(gameDir string, gameId int) {
|
||||
files, err := os.ReadDir(gameDir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
entry, err := file.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if isSong(entry) {
|
||||
path := gameDir + entry.Name()
|
||||
fileName := entry.Name()
|
||||
songName, _ := strings.CutSuffix(fileName, ".mp3")
|
||||
if db.CheckSong(path) {
|
||||
db.UpdateSong(songName, fileName, path)
|
||||
} else {
|
||||
db.AddSong(models.SongData{GameId: gameId, SongName: songName, Path: path, FileName: fileName})
|
||||
}
|
||||
} else if isCoverImage(entry) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
}
|
||||
//TODO: Add number of songs here
|
||||
}
|
||||
|
||||
func checkBrokenSongs() {
|
||||
allSongs := db.FetchAllSongs()
|
||||
var brokenSongs []models.SongData
|
||||
for _, song := range allSongs {
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil {
|
||||
//File not found
|
||||
brokenSongs = append(brokenSongs, song)
|
||||
fmt.Printf("song broken: %v", song.Path)
|
||||
} else {
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
db.RemoveBrokenSongs(brokenSongs)
|
||||
}
|
||||
func SyncGamesNewFull() Response {
|
||||
return syncGamesNew(true)
|
||||
}
|
||||
@@ -271,7 +81,7 @@ func SyncGamesNewOnlyChanges() Response {
|
||||
func syncGamesNew(full bool) Response {
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
repo = repository.New(db.Dbpool)
|
||||
initRepo()
|
||||
start := time.Now()
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
|
||||
103
internal/database/game.go
Normal file
103
internal/database/game.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"music-server/internal/db"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type gameData struct {
|
||||
Id int
|
||||
GameName string
|
||||
Added time.Time
|
||||
Deleted time.Time
|
||||
LastChanged time.Time
|
||||
Path string
|
||||
TimesPlayed int
|
||||
LastPlayed time.Time
|
||||
NumberOfSongs int32
|
||||
}
|
||||
|
||||
func GetGameName(gameId int) string {
|
||||
var gameName = ""
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return gameName
|
||||
}
|
||||
|
||||
func SetGameDeletionDate() {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET deleted=$1 WHERE deleted IS NULL", time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateGameName(id int, name string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
|
||||
name, path, time.Now(), id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func RemoveDeletionDate(id int) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET deleted=null WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetIdByGameName(name string) int {
|
||||
var gameId = -1
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGame(name string, path string) int {
|
||||
gameId := -1
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
db.ResetGameIdSeq()
|
||||
err2 := db.Dbpool.QueryRow(db.Ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err2 != nil {
|
||||
if compareError.Error() != err2.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGameWithExistingId(id int, name string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
|
||||
id, name, path, time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
206
internal/database/oldSync.go
Normal file
206
internal/database/oldSync.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func SyncGames() {
|
||||
start := time.Now()
|
||||
dir := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
func SyncGamesQuick() {
|
||||
start := time.Now()
|
||||
dir := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
func syncGame(file os.DirEntry, foldersToSkip []string, dir string) {
|
||||
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
|
||||
fmt.Println(file.Name())
|
||||
path := dir + file.Name() + "/"
|
||||
fmt.Println(path)
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id := -1
|
||||
for _, entry := range entries {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id = getIdFromFile(fileInfo)
|
||||
if id != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if id == -1 {
|
||||
addNewGame(file.Name(), path)
|
||||
} else {
|
||||
checkIfChanged(id, file.Name(), path)
|
||||
checkSongs(path, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getIdFromFile(file os.FileInfo) int {
|
||||
name := file.Name()
|
||||
if !file.IsDir() && strings.HasSuffix(name, ".id") {
|
||||
name = strings.Replace(name, ".id", "", 1)
|
||||
name = strings.Replace(name, ".", "", 1)
|
||||
i, _ := strconv.Atoi(name)
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func checkIfChanged(id int, name string, path string) {
|
||||
fmt.Printf("Id from file: %v\n", id)
|
||||
nameFromDb := GetGameName(id)
|
||||
fmt.Printf("Name from file: %v\n", name)
|
||||
fmt.Printf("Name from DB: %v\n", nameFromDb)
|
||||
if nameFromDb == "" {
|
||||
fmt.Println("Not in db")
|
||||
InsertGameWithExistingId(id, name, path)
|
||||
fmt.Println("Added to db")
|
||||
} else if name != nameFromDb {
|
||||
fmt.Println("Diff name")
|
||||
UpdateGameName(id, name, path)
|
||||
checkBrokenSongs()
|
||||
}
|
||||
RemoveDeletionDate(id)
|
||||
}
|
||||
|
||||
func addNewGame(name string, path string) {
|
||||
newId := GetIdByGameName(name)
|
||||
if newId != -1 {
|
||||
checkBrokenSongs()
|
||||
RemoveDeletionDate(newId)
|
||||
} else {
|
||||
newId = InsertGame(name, path)
|
||||
}
|
||||
|
||||
fmt.Printf("newId = %v", newId)
|
||||
fileName := path + "/." + strconv.Itoa(newId) + ".id"
|
||||
fmt.Printf("fileName = %v", fileName)
|
||||
|
||||
err := os.WriteFile(fileName, nil, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
checkSongs(path, newId)
|
||||
}
|
||||
|
||||
func checkSongs(gameDir string, gameId int) {
|
||||
files, err := os.ReadDir(gameDir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
entry, err := file.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if isSong(entry) {
|
||||
path := gameDir + entry.Name()
|
||||
fileName := entry.Name()
|
||||
songName, _ := strings.CutSuffix(fileName, ".mp3")
|
||||
if CheckSong(path) {
|
||||
UpdateSong(songName, fileName, path)
|
||||
} else {
|
||||
AddSong(SongData{GameId: gameId, SongName: songName, Path: path, FileName: fileName})
|
||||
}
|
||||
} else if isCoverImage(entry) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
}
|
||||
//TODO: Add number of songs here
|
||||
}
|
||||
|
||||
func checkBrokenSongs() {
|
||||
allSongs := FetchAllSongs()
|
||||
var brokenSongs []SongData
|
||||
for _, song := range allSongs {
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil {
|
||||
//File not found
|
||||
brokenSongs = append(brokenSongs, song)
|
||||
fmt.Printf("song broken: %v", song.Path)
|
||||
} else {
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
RemoveBrokenSongs(brokenSongs)
|
||||
}
|
||||
|
||||
func isSong(entry fs.FileInfo) bool {
|
||||
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".mp3")
|
||||
}
|
||||
|
||||
func isCoverImage(entry fs.FileInfo) bool {
|
||||
return !entry.IsDir() && strings.Contains(entry.Name(), "cover") &&
|
||||
(strings.HasSuffix(entry.Name(), ".jpg") || strings.HasSuffix(entry.Name(), ".png"))
|
||||
}
|
||||
|
||||
func contains(s []string, searchTerm string) bool {
|
||||
i := sort.SearchStrings(s, searchTerm)
|
||||
return i < len(s) && s[i] == searchTerm
|
||||
}
|
||||
92
internal/database/song.go
Normal file
92
internal/database/song.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"music-server/internal/db"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SongData struct {
|
||||
GameId int
|
||||
SongName string
|
||||
Path string
|
||||
TimesPlayed int
|
||||
FileName string
|
||||
}
|
||||
|
||||
var compareError = errors.New("no rows in result set")
|
||||
|
||||
func AddSong(song SongData) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"INSERT INTO song(game_id, song_name, path, file_name) VALUES ($1, $2, $3, $4)",
|
||||
song.GameId, song.SongName, song.Path, song.FileName)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckSong(songPath string) bool {
|
||||
var path string
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
return path != ""
|
||||
}
|
||||
|
||||
func UpdateSong(songName string, fileName string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
|
||||
songName, fileName, path)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func FetchAllSongs() []SongData {
|
||||
rows, err := db.Dbpool.Query(db.Ctx,
|
||||
"SELECT song_name, path FROM song")
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var songDataList []SongData
|
||||
for rows.Next() {
|
||||
var songName string
|
||||
var path string
|
||||
|
||||
err := rows.Scan(&songName, &path)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
songDataList = append(songDataList, SongData{
|
||||
SongName: songName,
|
||||
Path: path,
|
||||
})
|
||||
}
|
||||
return songDataList
|
||||
}
|
||||
|
||||
func RemoveBrokenSongs(songs []SongData) {
|
||||
joined := ""
|
||||
for _, song := range songs {
|
||||
joined += "'" + song.Path + "',"
|
||||
}
|
||||
joined = strings.TrimSuffix(joined, ",")
|
||||
|
||||
_, err := db.Dbpool.Exec(db.Ctx, "DELETE FROM song where path in ($1)", joined)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
199
internal/db/dbHelper.go
Normal file
199
internal/db/dbHelper.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var Dbpool *pgxpool.Pool
|
||||
var Ctx = context.Background()
|
||||
|
||||
//go:embed "migrations/*.sql"
|
||||
var MigrationsFs embed.FS
|
||||
|
||||
func InitDB(host string, port string, user string, password string, dbname string) {
|
||||
|
||||
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+
|
||||
"password=%s dbname=%s sslmode=disable",
|
||||
host, port, user, password, dbname)
|
||||
|
||||
fmt.Println(psqlInfo)
|
||||
|
||||
var err error
|
||||
Dbpool, err = pgxpool.New(Ctx, psqlInfo)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var success string
|
||||
err = Dbpool.QueryRow(Ctx, "select 'Successfully connected!'").Scan(&success)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
Testf()
|
||||
fmt.Println(success)
|
||||
}
|
||||
|
||||
func CloseDb() {
|
||||
fmt.Println("Closing connection to database")
|
||||
Dbpool.Close()
|
||||
}
|
||||
|
||||
func Testf() {
|
||||
rows, dbErr := Dbpool.Query(Ctx, "select game_name from game")
|
||||
if dbErr != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
for rows.Next() {
|
||||
var gameName string
|
||||
dbErr = rows.Scan(&gameName)
|
||||
if dbErr != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
||||
}
|
||||
_, _ = fmt.Fprintf(os.Stderr, "%v\n", gameName)
|
||||
}
|
||||
}
|
||||
|
||||
func ResetGameIdSeq() {
|
||||
_, err := Dbpool.Query(Ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func Migrate_db(host string, port string, user string, password string, dbname string) {
|
||||
migrationInfo := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
user, password, host, port, dbname)
|
||||
|
||||
fmt.Println("Migration Info: ", migrationInfo)
|
||||
|
||||
db, err := sql.Open("postgres", migrationInfo)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
files, err := iofs.New(MigrationsFs, "migrations")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
m, err := migrate.NewWithInstance("iofs", files, "postgres", driver)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
/*m, err := migrate.NewWithDatabaseInstance(
|
||||
"file://./db/migrations/",
|
||||
"postgres", driver)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}*/
|
||||
|
||||
version, _, err := m.Version()
|
||||
if err != nil {
|
||||
log.Println("Migration version err: ", err)
|
||||
}
|
||||
|
||||
fmt.Println("Migration version before: ", version)
|
||||
|
||||
err = m.Force(1)
|
||||
//err = m.Up() // or m.Steps(2) if you want to explicitly set the number of migrations to run
|
||||
if err != nil {
|
||||
log.Println("Force err: ", err)
|
||||
}
|
||||
|
||||
err = m.Migrate(2)
|
||||
//err = m.Up() // or m.Steps(2) if you want to explicitly set the number of migrations to run
|
||||
if err != nil {
|
||||
log.Println("Migration err: ", err)
|
||||
}
|
||||
|
||||
versionAfter, _, err := m.Version()
|
||||
if err != nil {
|
||||
log.Println("Migration version err: ", err)
|
||||
}
|
||||
|
||||
fmt.Println("Migration version after: ", versionAfter)
|
||||
|
||||
fmt.Println("Migration done")
|
||||
|
||||
db.Close()
|
||||
}
|
||||
|
||||
// Health checks the health of the database connection by pinging the database.
|
||||
// It returns a map with keys indicating various health statistics.
|
||||
func Health() map[string]string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats := make(map[string]string)
|
||||
|
||||
// Ping the database
|
||||
//err := s.db.PingContext(ctx)
|
||||
err := Dbpool.Ping(ctx)
|
||||
if err != nil {
|
||||
stats["status"] = "down"
|
||||
stats["error"] = fmt.Sprintf("db down: %v", err)
|
||||
log.Fatalf("db down: %v", err) // Log the error and terminate the program
|
||||
return stats
|
||||
}
|
||||
|
||||
// Database is up, add more statistics
|
||||
stats["status"] = "up"
|
||||
stats["message"] = "It's healthy"
|
||||
|
||||
// Get database stats (like open connections, in use, idle, etc.)
|
||||
//dbStats := s.db.Stats()
|
||||
dbStats := Dbpool.Stat()
|
||||
//stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
|
||||
stats["open_connections"] = strconv.Itoa(int(dbStats.NewConnsCount()))
|
||||
//stats["in_use"] = strconv.Itoa(dbStats.InUse)
|
||||
stats["in_use"] = strconv.Itoa(int(dbStats.AcquiredConns()))
|
||||
//stats["idle"] = strconv.Itoa(dbStats.Idle)
|
||||
stats["idle"] = strconv.Itoa(int(dbStats.IdleConns()))
|
||||
//stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
|
||||
stats["wait_count"] = strconv.FormatInt(dbStats.AcquireCount(), 10)
|
||||
//stats["wait_duration"] = dbStats.WaitDuration.String()
|
||||
stats["wait_duration"] = dbStats.AcquireDuration().String()
|
||||
//stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
|
||||
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleDestroyCount(), 10)
|
||||
//stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
|
||||
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeDestroyCount(), 10)
|
||||
|
||||
// Evaluate stats to provide a health message
|
||||
if int(dbStats.NewConnsCount()) > 40 { // Assuming 50 is the max for this example
|
||||
stats["message"] = "The database is experiencing heavy load."
|
||||
}
|
||||
|
||||
if dbStats.AcquireCount() > 1000 {
|
||||
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
|
||||
}
|
||||
|
||||
if dbStats.MaxIdleDestroyCount() > int64(dbStats.NewConnsCount())/2 {
|
||||
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
|
||||
}
|
||||
|
||||
if dbStats.MaxLifetimeDestroyCount() > int64(dbStats.NewConnsCount())/2 {
|
||||
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern."
|
||||
}
|
||||
|
||||
return stats
|
||||
}
|
||||
4
internal/db/migrations/000001_create_tables.down.sql
Normal file
4
internal/db/migrations/000001_create_tables.down.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
DROP TABLE game;
|
||||
DROP TABLE song;
|
||||
DROP TABLE song_list;
|
||||
DROP TABLE vgmq;
|
||||
43
internal/db/migrations/000001_create_tables.up.sql
Normal file
43
internal/db/migrations/000001_create_tables.up.sql
Normal file
@@ -0,0 +1,43 @@
|
||||
CREATE TABLE game (
|
||||
id serial4 NOT NULL,
|
||||
game_name varchar NOT NULL,
|
||||
added timestamp NOT NULL,
|
||||
deleted timestamp NULL,
|
||||
last_changed timestamp NULL,
|
||||
"path" varchar NOT NULL,
|
||||
times_played int4 DEFAULT 0 NULL,
|
||||
last_played timestamp NULL,
|
||||
number_of_songs int4 NULL,
|
||||
hash varchar NULL,
|
||||
CONSTRAINT game_pkey PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
CREATE TABLE song (
|
||||
game_id int4 NOT NULL,
|
||||
song_name varchar NOT NULL,
|
||||
"path" varchar NOT NULL,
|
||||
times_played int4 DEFAULT 0 NULL,
|
||||
hash varchar NULL,
|
||||
file_name varchar NULL,
|
||||
CONSTRAINT song_pkey PRIMARY KEY (game_id, path)
|
||||
);
|
||||
|
||||
CREATE TABLE vgmq (
|
||||
song_no int4 NOT NULL,
|
||||
"path" varchar(50) NULL,
|
||||
clue varchar(200) NULL,
|
||||
answered bool DEFAULT false NOT NULL,
|
||||
answer varchar(50) NULL,
|
||||
CONSTRAINT vgmq_pk PRIMARY KEY (song_no)
|
||||
);
|
||||
CREATE UNIQUE INDEX vgmq_song_no_uindex ON vgmq USING btree (song_no);
|
||||
|
||||
CREATE TABLE song_list (
|
||||
match_date date NOT NULL,
|
||||
match_id int4 NOT NULL,
|
||||
song_no int4 NOT NULL,
|
||||
game_name varchar(50) NULL,
|
||||
song_name varchar(50) NULL,
|
||||
CONSTRAINT song_list_pkey PRIMARY KEY (match_date, match_id, song_no)
|
||||
);
|
||||
CREATE INDEX song_list_game_name_idx ON song_list USING btree (game_name);
|
||||
@@ -0,0 +1,3 @@
|
||||
Alter table game
|
||||
alter column number_of_songs set null,
|
||||
alter column hash set null;
|
||||
19
internal/db/migrations/000002_adjust_fields_for_sqlc.up.sql
Normal file
19
internal/db/migrations/000002_adjust_fields_for_sqlc.up.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
BEGIN;
|
||||
UPDATE game
|
||||
SET number_of_songs = 0
|
||||
WHERE number_of_songs IS NULL;
|
||||
UPDATE game
|
||||
SET hash = ''
|
||||
WHERE hash IS NULL;
|
||||
UPDATE song
|
||||
SET hash = ''
|
||||
WHERE hash IS NULL;
|
||||
COMMIT;
|
||||
BEGIN;
|
||||
Alter table game
|
||||
alter column number_of_songs set not null,
|
||||
alter column number_of_songs set default 0,
|
||||
ALTER COLUMN hash SET NOT NULL;
|
||||
ALTER TABLE song
|
||||
ALTER COLUMN hash SET NOT NULL;
|
||||
COMMIT;
|
||||
5
internal/db/migrations/000003_fix_times_played.down.sql
Normal file
5
internal/db/migrations/000003_fix_times_played.down.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
Alter table game
|
||||
alter column times_played set null;
|
||||
|
||||
Alter table song
|
||||
alter column times_played set null;
|
||||
5
internal/db/migrations/000003_fix_times_played.up.sql
Normal file
5
internal/db/migrations/000003_fix_times_played.up.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
Alter table game
|
||||
alter column times_played set not null;
|
||||
|
||||
Alter table song
|
||||
alter column times_played set not null;
|
||||
49
internal/db/queries/game.sql
Normal file
49
internal/db/queries/game.sql
Normal file
@@ -0,0 +1,49 @@
|
||||
-- name: ResetGameIdSeq :one
|
||||
SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);
|
||||
|
||||
-- name: GetGameNameById :one
|
||||
SELECT game_name FROM game WHERE id = $1;
|
||||
|
||||
-- name: GetGameById :one
|
||||
SELECT *
|
||||
FROM game
|
||||
WHERE id = $1
|
||||
AND deleted IS NULL;
|
||||
|
||||
-- name: SetGameDeletionDate :exec
|
||||
UPDATE game SET deleted=now() WHERE deleted IS NULL;
|
||||
|
||||
-- name: ClearGames :exec
|
||||
DELETE FROM game;
|
||||
|
||||
-- name: UpdateGameName :exec
|
||||
UPDATE game SET game_name=sqlc.arg(name), path=sqlc.arg(path), last_changed=now() WHERE id=sqlc.arg(id);
|
||||
|
||||
-- name: UpdateGameHash :exec
|
||||
UPDATE game SET hash=sqlc.arg(hash), last_changed=now() WHERE id=sqlc.arg(id);
|
||||
|
||||
-- name: RemoveDeletionDate :exec
|
||||
UPDATE game SET deleted=NULL WHERE id=$1;
|
||||
|
||||
-- name: GetIdByGameName :one
|
||||
SELECT id FROM game WHERE game_name = $1;
|
||||
|
||||
-- name: InsertGame :one
|
||||
INSERT INTO game (game_name, path, hash, added) VALUES ($1, $2, $3, now()) returning id;
|
||||
|
||||
-- name: InsertGameWithExistingId :exec
|
||||
INSERT INTO game (id, game_name, path, hash, added) VALUES ($1, $2, $3, $4, now());
|
||||
|
||||
-- name: FindAllGames :many
|
||||
SELECT *
|
||||
FROM game
|
||||
WHERE deleted IS NULL
|
||||
ORDER BY game_name;
|
||||
|
||||
-- name: GetAllGamesIncludingDeleted :many
|
||||
SELECT *
|
||||
FROM game
|
||||
ORDER BY game_name;
|
||||
|
||||
-- name: AddGamePlayed :exec
|
||||
UPDATE game SET times_played = times_played + 1, last_played = now() WHERE id = $1;
|
||||
41
internal/db/queries/song.sql
Normal file
41
internal/db/queries/song.sql
Normal file
@@ -0,0 +1,41 @@
|
||||
-- name: ClearSongs :exec
|
||||
DELETE FROM song;
|
||||
|
||||
-- name: ClearSongsByGameId :exec
|
||||
DELETE FROM song WHERE game_id = $1;
|
||||
|
||||
-- name: AddSong :exec
|
||||
INSERT INTO song(game_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5);
|
||||
|
||||
-- name: CheckSong :one
|
||||
SELECT COUNT(*) FROM song WHERE path = $1;
|
||||
|
||||
-- name: CheckSongWithHash :one
|
||||
SELECT COUNT(*) FROM song WHERE hash = $1;
|
||||
|
||||
-- name: GetSongWithHash :one
|
||||
SELECT * FROM song WHERE hash = $1;
|
||||
|
||||
-- name: UpdateSong :exec
|
||||
UPDATE song SET song_name=$1, file_name=$2, path=$3 where hash=$4;
|
||||
|
||||
-- name: AddHashToSong :exec
|
||||
UPDATE song SET hash=$1 where path=$2;
|
||||
|
||||
-- name: FindSongsFromGame :many
|
||||
SELECT *
|
||||
FROM song
|
||||
WHERE game_id = $1;
|
||||
|
||||
-- name: AddSongPlayed :exec
|
||||
UPDATE song SET times_played = times_played + 1
|
||||
WHERE game_id = $1 AND song_name = $2;
|
||||
|
||||
-- name: FetchAllSongs :many
|
||||
SELECT * FROM song;
|
||||
|
||||
-- name: RemoveBrokenSong :exec
|
||||
DELETE FROM song WHERE path = $1;
|
||||
|
||||
-- name: RemoveBrokenSongs :exec
|
||||
DELETE FROM song where path = any (sqlc.slice('paths'));
|
||||
9
internal/db/queries/song_list.sql
Normal file
9
internal/db/queries/song_list.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
-- name: InsertSongInList :exec
|
||||
INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name)
|
||||
VALUES ($1, $2, $3, $4, $5);
|
||||
|
||||
-- name: GetSongList :many
|
||||
SELECT *
|
||||
FROM song_list
|
||||
WHERE match_date = $1
|
||||
ORDER BY song_no DESC;
|
||||
0
internal/db/queries/vgmq.sql
Normal file
0
internal/db/queries/vgmq.sql
Normal file
32
internal/db/repository/db.go
Normal file
32
internal/db/repository/db.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
246
internal/db/repository/game.sql.go
Normal file
246
internal/db/repository/game.sql.go
Normal file
@@ -0,0 +1,246 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: game.sql
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const addGamePlayed = `-- name: AddGamePlayed :exec
|
||||
UPDATE game SET times_played = times_played + 1, last_played = now() WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) AddGamePlayed(ctx context.Context, id int32) error {
|
||||
_, err := q.db.Exec(ctx, addGamePlayed, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const clearGames = `-- name: ClearGames :exec
|
||||
DELETE FROM game
|
||||
`
|
||||
|
||||
func (q *Queries) ClearGames(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, clearGames)
|
||||
return err
|
||||
}
|
||||
|
||||
const findAllGames = `-- name: FindAllGames :many
|
||||
SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash
|
||||
FROM game
|
||||
WHERE deleted IS NULL
|
||||
ORDER BY game_name
|
||||
`
|
||||
|
||||
func (q *Queries) FindAllGames(ctx context.Context) ([]Game, error) {
|
||||
rows, err := q.db.Query(ctx, findAllGames)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Game
|
||||
for rows.Next() {
|
||||
var i Game
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.GameName,
|
||||
&i.Added,
|
||||
&i.Deleted,
|
||||
&i.LastChanged,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.LastPlayed,
|
||||
&i.NumberOfSongs,
|
||||
&i.Hash,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getAllGamesIncludingDeleted = `-- name: GetAllGamesIncludingDeleted :many
|
||||
SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash
|
||||
FROM game
|
||||
ORDER BY game_name
|
||||
`
|
||||
|
||||
func (q *Queries) GetAllGamesIncludingDeleted(ctx context.Context) ([]Game, error) {
|
||||
rows, err := q.db.Query(ctx, getAllGamesIncludingDeleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Game
|
||||
for rows.Next() {
|
||||
var i Game
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.GameName,
|
||||
&i.Added,
|
||||
&i.Deleted,
|
||||
&i.LastChanged,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.LastPlayed,
|
||||
&i.NumberOfSongs,
|
||||
&i.Hash,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getGameById = `-- name: GetGameById :one
|
||||
SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash
|
||||
FROM game
|
||||
WHERE id = $1
|
||||
AND deleted IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) GetGameById(ctx context.Context, id int32) (Game, error) {
|
||||
row := q.db.QueryRow(ctx, getGameById, id)
|
||||
var i Game
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.GameName,
|
||||
&i.Added,
|
||||
&i.Deleted,
|
||||
&i.LastChanged,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.LastPlayed,
|
||||
&i.NumberOfSongs,
|
||||
&i.Hash,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getGameNameById = `-- name: GetGameNameById :one
|
||||
SELECT game_name FROM game WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetGameNameById(ctx context.Context, id int32) (string, error) {
|
||||
row := q.db.QueryRow(ctx, getGameNameById, id)
|
||||
var game_name string
|
||||
err := row.Scan(&game_name)
|
||||
return game_name, err
|
||||
}
|
||||
|
||||
const getIdByGameName = `-- name: GetIdByGameName :one
|
||||
SELECT id FROM game WHERE game_name = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetIdByGameName(ctx context.Context, gameName string) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, getIdByGameName, gameName)
|
||||
var id int32
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const insertGame = `-- name: InsertGame :one
|
||||
INSERT INTO game (game_name, path, hash, added) VALUES ($1, $2, $3, now()) returning id
|
||||
`
|
||||
|
||||
type InsertGameParams struct {
|
||||
GameName string `json:"game_name"`
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGame(ctx context.Context, arg InsertGameParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, insertGame, arg.GameName, arg.Path, arg.Hash)
|
||||
var id int32
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const insertGameWithExistingId = `-- name: InsertGameWithExistingId :exec
|
||||
INSERT INTO game (id, game_name, path, hash, added) VALUES ($1, $2, $3, $4, now())
|
||||
`
|
||||
|
||||
type InsertGameWithExistingIdParams struct {
|
||||
ID int32 `json:"id"`
|
||||
GameName string `json:"game_name"`
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGameWithExistingId(ctx context.Context, arg InsertGameWithExistingIdParams) error {
|
||||
_, err := q.db.Exec(ctx, insertGameWithExistingId,
|
||||
arg.ID,
|
||||
arg.GameName,
|
||||
arg.Path,
|
||||
arg.Hash,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const removeDeletionDate = `-- name: RemoveDeletionDate :exec
|
||||
UPDATE game SET deleted=NULL WHERE id=$1
|
||||
`
|
||||
|
||||
func (q *Queries) RemoveDeletionDate(ctx context.Context, id int32) error {
|
||||
_, err := q.db.Exec(ctx, removeDeletionDate, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const resetGameIdSeq = `-- name: ResetGameIdSeq :one
|
||||
SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1)
|
||||
`
|
||||
|
||||
func (q *Queries) ResetGameIdSeq(ctx context.Context) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, resetGameIdSeq)
|
||||
var setval int64
|
||||
err := row.Scan(&setval)
|
||||
return setval, err
|
||||
}
|
||||
|
||||
const setGameDeletionDate = `-- name: SetGameDeletionDate :exec
|
||||
UPDATE game SET deleted=now() WHERE deleted IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) SetGameDeletionDate(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, setGameDeletionDate)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateGameHash = `-- name: UpdateGameHash :exec
|
||||
UPDATE game SET hash=$1, last_changed=now() WHERE id=$2
|
||||
`
|
||||
|
||||
type UpdateGameHashParams struct {
|
||||
Hash string `json:"hash"`
|
||||
ID int32 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateGameHash(ctx context.Context, arg UpdateGameHashParams) error {
|
||||
_, err := q.db.Exec(ctx, updateGameHash, arg.Hash, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateGameName = `-- name: UpdateGameName :exec
|
||||
UPDATE game SET game_name=$1, path=$2, last_changed=now() WHERE id=$3
|
||||
`
|
||||
|
||||
type UpdateGameNameParams struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
ID int32 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateGameName(ctx context.Context, arg UpdateGameNameParams) error {
|
||||
_, err := q.db.Exec(ctx, updateGameName, arg.Name, arg.Path, arg.ID)
|
||||
return err
|
||||
}
|
||||
47
internal/db/repository/models.go
Normal file
47
internal/db/repository/models.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
ID int32 `json:"id"`
|
||||
GameName string `json:"game_name"`
|
||||
Added time.Time `json:"added"`
|
||||
Deleted *time.Time `json:"deleted"`
|
||||
LastChanged *time.Time `json:"last_changed"`
|
||||
Path string `json:"path"`
|
||||
TimesPlayed int32 `json:"times_played"`
|
||||
LastPlayed *time.Time `json:"last_played"`
|
||||
NumberOfSongs int32 `json:"number_of_songs"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
type Song struct {
|
||||
GameID int32 `json:"game_id"`
|
||||
SongName string `json:"song_name"`
|
||||
Path string `json:"path"`
|
||||
TimesPlayed int32 `json:"times_played"`
|
||||
Hash string `json:"hash"`
|
||||
FileName *string `json:"file_name"`
|
||||
}
|
||||
|
||||
type SongList struct {
|
||||
MatchDate time.Time `json:"match_date"`
|
||||
MatchID int32 `json:"match_id"`
|
||||
SongNo int32 `json:"song_no"`
|
||||
GameName *string `json:"game_name"`
|
||||
SongName *string `json:"song_name"`
|
||||
}
|
||||
|
||||
type Vgmq struct {
|
||||
SongNo int32 `json:"song_no"`
|
||||
Path *string `json:"path"`
|
||||
Clue *string `json:"clue"`
|
||||
Answered bool `json:"answered"`
|
||||
Answer *string `json:"answer"`
|
||||
}
|
||||
223
internal/db/repository/song.sql.go
Normal file
223
internal/db/repository/song.sql.go
Normal file
@@ -0,0 +1,223 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: song.sql
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const addHashToSong = `-- name: AddHashToSong :exec
|
||||
UPDATE song SET hash=$1 where path=$2
|
||||
`
|
||||
|
||||
type AddHashToSongParams struct {
|
||||
Hash string `json:"hash"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (q *Queries) AddHashToSong(ctx context.Context, arg AddHashToSongParams) error {
|
||||
_, err := q.db.Exec(ctx, addHashToSong, arg.Hash, arg.Path)
|
||||
return err
|
||||
}
|
||||
|
||||
const addSong = `-- name: AddSong :exec
|
||||
INSERT INTO song(game_id, song_name, path, file_name, hash) VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
type AddSongParams struct {
|
||||
GameID int32 `json:"game_id"`
|
||||
SongName string `json:"song_name"`
|
||||
Path string `json:"path"`
|
||||
FileName *string `json:"file_name"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func (q *Queries) AddSong(ctx context.Context, arg AddSongParams) error {
|
||||
_, err := q.db.Exec(ctx, addSong,
|
||||
arg.GameID,
|
||||
arg.SongName,
|
||||
arg.Path,
|
||||
arg.FileName,
|
||||
arg.Hash,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const addSongPlayed = `-- name: AddSongPlayed :exec
|
||||
UPDATE song SET times_played = times_played + 1
|
||||
WHERE game_id = $1 AND song_name = $2
|
||||
`
|
||||
|
||||
type AddSongPlayedParams struct {
|
||||
GameID int32 `json:"game_id"`
|
||||
SongName string `json:"song_name"`
|
||||
}
|
||||
|
||||
func (q *Queries) AddSongPlayed(ctx context.Context, arg AddSongPlayedParams) error {
|
||||
_, err := q.db.Exec(ctx, addSongPlayed, arg.GameID, arg.SongName)
|
||||
return err
|
||||
}
|
||||
|
||||
const checkSong = `-- name: CheckSong :one
|
||||
SELECT COUNT(*) FROM song WHERE path = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CheckSong(ctx context.Context, path string) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, checkSong, path)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const checkSongWithHash = `-- name: CheckSongWithHash :one
|
||||
SELECT COUNT(*) FROM song WHERE hash = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CheckSongWithHash(ctx context.Context, hash string) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, checkSongWithHash, hash)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const clearSongs = `-- name: ClearSongs :exec
|
||||
DELETE FROM song
|
||||
`
|
||||
|
||||
func (q *Queries) ClearSongs(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, clearSongs)
|
||||
return err
|
||||
}
|
||||
|
||||
const clearSongsByGameId = `-- name: ClearSongsByGameId :exec
|
||||
DELETE FROM song WHERE game_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) ClearSongsByGameId(ctx context.Context, gameID int32) error {
|
||||
_, err := q.db.Exec(ctx, clearSongsByGameId, gameID)
|
||||
return err
|
||||
}
|
||||
|
||||
const fetchAllSongs = `-- name: FetchAllSongs :many
|
||||
SELECT game_id, song_name, path, times_played, hash, file_name FROM song
|
||||
`
|
||||
|
||||
func (q *Queries) FetchAllSongs(ctx context.Context) ([]Song, error) {
|
||||
rows, err := q.db.Query(ctx, fetchAllSongs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Song
|
||||
for rows.Next() {
|
||||
var i Song
|
||||
if err := rows.Scan(
|
||||
&i.GameID,
|
||||
&i.SongName,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const findSongsFromGame = `-- name: FindSongsFromGame :many
|
||||
SELECT game_id, song_name, path, times_played, hash, file_name
|
||||
FROM song
|
||||
WHERE game_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) FindSongsFromGame(ctx context.Context, gameID int32) ([]Song, error) {
|
||||
rows, err := q.db.Query(ctx, findSongsFromGame, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Song
|
||||
for rows.Next() {
|
||||
var i Song
|
||||
if err := rows.Scan(
|
||||
&i.GameID,
|
||||
&i.SongName,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getSongWithHash = `-- name: GetSongWithHash :one
|
||||
SELECT game_id, song_name, path, times_played, hash, file_name FROM song WHERE hash = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSongWithHash(ctx context.Context, hash string) (Song, error) {
|
||||
row := q.db.QueryRow(ctx, getSongWithHash, hash)
|
||||
var i Song
|
||||
err := row.Scan(
|
||||
&i.GameID,
|
||||
&i.SongName,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.Hash,
|
||||
&i.FileName,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const removeBrokenSong = `-- name: RemoveBrokenSong :exec
|
||||
DELETE FROM song WHERE path = $1
|
||||
`
|
||||
|
||||
func (q *Queries) RemoveBrokenSong(ctx context.Context, path string) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSong, path)
|
||||
return err
|
||||
}
|
||||
|
||||
const removeBrokenSongs = `-- name: RemoveBrokenSongs :exec
|
||||
DELETE FROM song where path = any ($1)
|
||||
`
|
||||
|
||||
func (q *Queries) RemoveBrokenSongs(ctx context.Context, paths []string) error {
|
||||
_, err := q.db.Exec(ctx, removeBrokenSongs, paths)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateSong = `-- name: UpdateSong :exec
|
||||
UPDATE song SET song_name=$1, file_name=$2, path=$3 where hash=$4
|
||||
`
|
||||
|
||||
type UpdateSongParams struct {
|
||||
SongName string `json:"song_name"`
|
||||
FileName *string `json:"file_name"`
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSong(ctx context.Context, arg UpdateSongParams) error {
|
||||
_, err := q.db.Exec(ctx, updateSong,
|
||||
arg.SongName,
|
||||
arg.FileName,
|
||||
arg.Path,
|
||||
arg.Hash,
|
||||
)
|
||||
return err
|
||||
}
|
||||
68
internal/db/repository/song_list.sql.go
Normal file
68
internal/db/repository/song_list.sql.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: song_list.sql
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const getSongList = `-- name: GetSongList :many
|
||||
SELECT match_date, match_id, song_no, game_name, song_name
|
||||
FROM song_list
|
||||
WHERE match_date = $1
|
||||
ORDER BY song_no DESC
|
||||
`
|
||||
|
||||
func (q *Queries) GetSongList(ctx context.Context, matchDate time.Time) ([]SongList, error) {
|
||||
rows, err := q.db.Query(ctx, getSongList, matchDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SongList
|
||||
for rows.Next() {
|
||||
var i SongList
|
||||
if err := rows.Scan(
|
||||
&i.MatchDate,
|
||||
&i.MatchID,
|
||||
&i.SongNo,
|
||||
&i.GameName,
|
||||
&i.SongName,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertSongInList = `-- name: InsertSongInList :exec
|
||||
INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
type InsertSongInListParams struct {
|
||||
MatchDate time.Time `json:"match_date"`
|
||||
MatchID int32 `json:"match_id"`
|
||||
SongNo int32 `json:"song_no"`
|
||||
GameName *string `json:"game_name"`
|
||||
SongName *string `json:"song_name"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertSongInList(ctx context.Context, arg InsertSongInListParams) error {
|
||||
_, err := q.db.Exec(ctx, insertSongInList,
|
||||
arg.MatchDate,
|
||||
arg.MatchID,
|
||||
arg.SongNo,
|
||||
arg.GameName,
|
||||
arg.SongName,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
func SendSong(ctx echo.Context, Filename string) error {
|
||||
fmt.Println("Client requests: " + Filename)
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(Filename)
|
||||
if err != nil {
|
||||
//File not found, send 404
|
||||
//http.Error(ctx.Writer, "Song not found.", 404)
|
||||
return ctx.String(http.StatusNotFound, "Song not found.")
|
||||
}
|
||||
defer func(openFile *os.File) {
|
||||
_ = openFile.Close()
|
||||
}(openFile) //Close after function return
|
||||
|
||||
//File is found, create and send the correct headers
|
||||
|
||||
//Get the Content-Type of the file
|
||||
//Create a buffer to store the header of the file in
|
||||
FileHeader := make([]byte, 512)
|
||||
//Copy the headers into the FileHeader buffer
|
||||
_, _ = openFile.Read(FileHeader)
|
||||
//Get content type of file
|
||||
//FileContentType := http.DetectContentType(FileHeader)
|
||||
|
||||
//Get the file size
|
||||
FileStat, _ := openFile.Stat() //Get info from file
|
||||
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
|
||||
|
||||
//Send the headers
|
||||
//writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
|
||||
ctx.Response().Header().Set("Content-Type", "audio/mpeg")
|
||||
ctx.Response().Header().Set("Content-Length", FileSize)
|
||||
ctx.Response().Header().Set("Expires", "Tue, 03 Jul 2001 06:00:00 GMT")
|
||||
ctx.Response().Header().Set("Last-Modified", time.Now().String()+" GMT")
|
||||
ctx.Response().Header().Set("Cache-Control", "no-cache, no-store, private, max-age=0")
|
||||
ctx.Response().Header().Set("Pragma", "no-cache")
|
||||
ctx.Response().Header().Set("X-Accel-Expires", "0")
|
||||
|
||||
var etagHeaders = []string{
|
||||
"ETag",
|
||||
"If-Modified-Since",
|
||||
"If-Match",
|
||||
"If-None-Match",
|
||||
"If-Range",
|
||||
"If-Unmodified-Since",
|
||||
}
|
||||
|
||||
for _, v := range etagHeaders {
|
||||
if ctx.Request().Header.Get(v) != "" {
|
||||
ctx.Request().Header.Del(v)
|
||||
}
|
||||
}
|
||||
|
||||
//Send the file
|
||||
//We read 512 bytes from the file already, so we reset the offset back to 0
|
||||
_, _ = openFile.Seek(0, 0)
|
||||
//_, _ = io.Copy(ctx.Writer, openFile) //'Copy' the file to the client
|
||||
return ctx.Stream(http.StatusOK, "audio/mpeg", openFile)
|
||||
}
|
||||
@@ -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