Entire logging rework. Full error handling. Small improvements.
This commit is contained in:
4
auth.go
4
auth.go
@@ -30,7 +30,7 @@ func getUserPassword(username string) (string, error) {
|
|||||||
|
|
||||||
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[AUTH] Failed to get password for user %s: %s", username, err)
|
||||||
}
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ func AdminAuthRequired() gin.HandlerFunc {
|
|||||||
|
|
||||||
func redirectToLogin(c *gin.Context) {
|
func redirectToLogin(c *gin.Context) {
|
||||||
if err := session.InvalidateSession(c); err != nil {
|
if err := session.InvalidateSession(c); err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[AUTH] Failed to invalidate session: %s", err)
|
||||||
}
|
}
|
||||||
c.Redirect(http.StatusFound, "/login")
|
c.Redirect(http.StatusFound, "/login")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
|
|||||||
@@ -24,12 +24,14 @@ func GetCacheByPlayerID(c *gin.Context) {
|
|||||||
game, err := GetActiveGame(c)
|
game, err := GetActiveGame(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||||
|
utils.Logger.Warnf("[CACHE] No active game available! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := models.PlayerCache.GetScore(playerId, game.Tag)
|
val, err := models.PlayerCache.GetScore(playerId, game.Tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Warnf("[CACHE] Record not found! Error: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, val)
|
c.JSON(http.StatusOK, val)
|
||||||
}
|
}
|
||||||
@@ -38,20 +40,24 @@ func GetCacheByPlayerID(c *gin.Context) {
|
|||||||
// AddCache POST /cache
|
// AddCache POST /cache
|
||||||
func AddCache(c *gin.Context) {
|
func AddCache(c *gin.Context) {
|
||||||
var input AddCacheInput
|
var input AddCacheInput
|
||||||
|
var game models.Game
|
||||||
|
|
||||||
if err := c.BindJSON(&input); err != nil {
|
if err := c.BindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Warnf("[CACHE] Failed to bind JSON! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var game models.Game
|
if err := models.DB.Where("tag = ?", input.GameTag).First(&game).Error; err != nil {
|
||||||
if err := FindGameByTag(&game, input.GameTag).Error; err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Game not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Game not found!"})
|
||||||
|
utils.Logger.Warnf("[CACHE] Game not found! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := models.PlayerCache.SetScore(input.PlayerID, input.GameTag, input.Score)
|
err := models.PlayerCache.SetScore(input.PlayerID, input.GameTag, input.Score)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
||||||
|
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +73,7 @@ func UpdateCacheByPlayerID(c *gin.Context) {
|
|||||||
err := models.PlayerCache.SetScore(playerID, gameTag, score)
|
err := models.PlayerCache.SetScore(playerID, gameTag, score)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
||||||
|
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
@@ -79,6 +86,7 @@ func DeleteCacheByPlayerID(c *gin.Context) {
|
|||||||
|
|
||||||
if err := models.PlayerCache.DeleteScore(playerID, gameTag); err != nil {
|
if err := models.PlayerCache.DeleteScore(playerID, gameTag); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Warnf("[CACHE] Cache deletion failed! Error: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
@@ -88,13 +96,14 @@ func DeleteCacheByPlayerID(c *gin.Context) {
|
|||||||
func DeleteAllCaches(c *gin.Context) {
|
func DeleteAllCaches(c *gin.Context) {
|
||||||
if err := models.PlayerCache.PurgeCache(); err != nil {
|
if err := models.PlayerCache.PurgeCache(); err != nil {
|
||||||
c.String(http.StatusBadRequest, err.Error())
|
c.String(http.StatusBadRequest, err.Error())
|
||||||
|
utils.Logger.Warnf("[CACHE] Cache purge failed! Error: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.String(http.StatusOK, "Purged all caches!")
|
c.String(http.StatusOK, "Purged all caches!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateCacheAfterExpiry(key string) {
|
func UpdateCacheAfterExpiry(key string) {
|
||||||
utils.Logger.Println("[KeepUpdated] Cache expired: " + key)
|
utils.Logger.Infof("[KeepUpdated] Updating cache for key %s", key)
|
||||||
|
|
||||||
playerId, err := models.PlayerCache.GetPlayerIdFromCacheKey(key)
|
playerId, err := models.PlayerCache.GetPlayerIdFromCacheKey(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,19 +116,20 @@ func UpdateCacheAfterExpiry(key string) {
|
|||||||
Where("id = ?", playerId).
|
Where("id = ?", playerId).
|
||||||
First(&player).Error; err != nil {
|
First(&player).Error; err != nil {
|
||||||
|
|
||||||
utils.Logger.Println("Failed to find player: " + err.Error())
|
utils.Logger.Errorf("[KeepUpdated] Failed to find player with ID %d! Error: %s", playerId, err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if player.Clan.KeepUpdated {
|
if player.Clan.KeepUpdated {
|
||||||
score := CalcPlayerScore(player.Name, "BF2042")
|
score := CalcPlayerScore(player.Name, "BF2042")
|
||||||
if score == score && score != -1 { // not NaN
|
if score == score && score != -1 { // not NaN
|
||||||
if err := models.PlayerCache.SetScore(player.ID, "BF2042", score); err != nil {
|
if err := models.PlayerCache.SetScore(player.ID, "BF2042", score); err != nil {
|
||||||
utils.Logger.Println("Failed to update cache: " + err.Error())
|
utils.Logger.Warnf("[KeepUpdated] Failed to update cache for player %s! Error: %s", player.Name, err.Error())
|
||||||
} else {
|
} else {
|
||||||
utils.Logger.Println("[KeepUpdated] Updated cache for player " + player.Name)
|
utils.Logger.Infof("[KeepUpdated] Updated cache for player %s", player.Name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
utils.Logger.Println("[KeepUpdated] Received NaN for player " + player.Name)
|
utils.Logger.Warnf("[KeepUpdated] Failed to calculate score for player %s", player.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,11 @@ type UpdateClanInput struct {
|
|||||||
// GetAllClans GET /clan
|
// GetAllClans GET /clan
|
||||||
func GetAllClans(c *gin.Context) {
|
func GetAllClans(c *gin.Context) {
|
||||||
var clans []models.Clan
|
var clans []models.Clan
|
||||||
models.DB.Find(&clans)
|
if err := models.DB.Find(&clans).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while getting all clans: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, clans)
|
c.JSON(http.StatusOK, clans)
|
||||||
}
|
}
|
||||||
@@ -33,7 +37,11 @@ func GetAllClans(c *gin.Context) {
|
|||||||
// GetAllClansHTML GET /clans_html
|
// GetAllClansHTML GET /clans_html
|
||||||
func GetAllClansHTML(c *gin.Context) {
|
func GetAllClansHTML(c *gin.Context) {
|
||||||
var clans []models.Clan
|
var clans []models.Clan
|
||||||
models.DB.Find(&clans)
|
if err := models.DB.Find(&clans).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while getting all clans: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var htmlOptions string
|
var htmlOptions string
|
||||||
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
||||||
@@ -48,34 +56,34 @@ func GetAllClansHTML(c *gin.Context) {
|
|||||||
// AddClan POST /clan
|
// AddClan POST /clan
|
||||||
func AddClan(c *gin.Context) {
|
func AddClan(c *gin.Context) {
|
||||||
var input AddClanInput
|
var input AddClanInput
|
||||||
|
var clan models.Clan
|
||||||
|
|
||||||
if err := c.BindJSON(&input); err != nil {
|
if err := c.BindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
utils.Logger.Errorf("[CLAN] Error while binding JSON: %s", err.Error())
|
||||||
}
|
|
||||||
|
|
||||||
var clan models.Clan
|
|
||||||
if err := FindClanByName(&clan, input.Name).Error; err == nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Clan with this name already exists!"})
|
|
||||||
return
|
|
||||||
} else if err := FindClanByTag(&clan, input.Tag).Error; err == nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Clan with this tag already exists!"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
clan = models.Clan{Name: input.Name, Tag: input.Tag, KeepUpdated: input.KeepUpdated}
|
clan = models.Clan{Name: input.Name, Tag: input.Tag, KeepUpdated: input.KeepUpdated}
|
||||||
models.DB.Create(&clan)
|
|
||||||
|
if err := models.DB.Create(&clan).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while adding clan '[%s] %s': %s", input.Tag, input.Name, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, clan)
|
c.JSON(http.StatusOK, clan)
|
||||||
|
|
||||||
utils.Logger.Infof("Added clan '%s' with tag '%s'", clan.Name, clan.Tag)
|
utils.Logger.Infof("[CLAN] Added clan: [%s] %s", clan.Tag, clan.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClanByID GET /clan/:id
|
// GetClanByID GET /clan/:id
|
||||||
func GetClanByID(c *gin.Context) {
|
func GetClanByID(c *gin.Context) {
|
||||||
var clan models.Clan
|
var clan models.Clan
|
||||||
|
|
||||||
if err := FindClanByID(&clan, c).Error; err != nil {
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&clan).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while getting clan by ID: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,41 +93,57 @@ func GetClanByID(c *gin.Context) {
|
|||||||
// UpdateClanByID PATCH /clan/:id
|
// UpdateClanByID PATCH /clan/:id
|
||||||
func UpdateClanByID(c *gin.Context) {
|
func UpdateClanByID(c *gin.Context) {
|
||||||
var input UpdateClanInput
|
var input UpdateClanInput
|
||||||
|
var clan models.Clan
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&input); err != nil {
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while binding JSON: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res := models.DB.Model(&models.Clan{}).
|
if err := models.DB.First(&clan, c.Param("id")).Error; err != nil {
|
||||||
Where("id = ?", c.Param("id")).
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
Updates(map[string]interface{}{
|
utils.Logger.Errorf("[CLAN] Error while getting clan by ID: %s", err.Error())
|
||||||
"Name": input.Name,
|
return
|
||||||
"Tag": input.Tag,
|
}
|
||||||
"KeepUpdated": input.KeepUpdated,
|
|
||||||
})
|
oldValues := models.Clan{
|
||||||
if res.Error != nil {
|
Name: clan.Name,
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
Tag: clan.Tag,
|
||||||
|
KeepUpdated: clan.KeepUpdated,
|
||||||
|
}
|
||||||
|
|
||||||
|
clan.Name = input.Name
|
||||||
|
clan.Tag = input.Tag
|
||||||
|
clan.KeepUpdated = input.KeepUpdated
|
||||||
|
|
||||||
|
if err := models.DB.Save(&clan).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while updating clan '%s': %s", input.Name, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
|
|
||||||
utils.Logger.Infof("Updated clan '%s' with tag '%s'", input.Name, input.Tag)
|
utils.Logger.Infof("[CLAN] Updated clan: [%s] %s -> [%s] %s", oldValues.Tag, oldValues.Name, clan.Tag, clan.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteClanByID DELETE /clan/:id
|
// DeleteClanByID DELETE /clan/:id
|
||||||
func DeleteClanByID(c *gin.Context) {
|
func DeleteClanByID(c *gin.Context) {
|
||||||
var clan models.Clan
|
var clan models.Clan
|
||||||
if err := FindClanByID(&clan, c).Error; err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
if err := models.DB.Clauses(clause.Returning{}).
|
||||||
|
Where("id = ?", c.Param("id")).
|
||||||
|
Delete(&clan).Error; err != nil {
|
||||||
|
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while deleting clan '%s': %s", clan.Name, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Delete(&clan)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
|
|
||||||
utils.Logger.Infof("Deleted clan '%s' with tag '%s'", clan.Name, clan.Tag)
|
utils.Logger.Infof("[CLAN] Deleted clan: [%s] %s", clan.Tag, clan.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAllClans DELETE /admin/clan
|
// DeleteAllClans DELETE /admin/clan
|
||||||
@@ -129,21 +153,12 @@ func DeleteAllClans(c *gin.Context) {
|
|||||||
Session(&gorm.Session{AllowGlobalUpdate: true}).
|
Session(&gorm.Session{AllowGlobalUpdate: true}).
|
||||||
Clauses(clause.Returning{}).
|
Clauses(clause.Returning{}).
|
||||||
Delete(&clans).Error; err != nil {
|
Delete(&clans).Error; err != nil {
|
||||||
|
|
||||||
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
|
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
|
||||||
|
utils.Logger.Errorf("[CLAN] Error while purging all clans: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(clans)))+" clans!")
|
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(clans)))+" clans!")
|
||||||
}
|
utils.Logger.Infof("[CLAN] Purged %d clans!", len(clans))
|
||||||
|
|
||||||
func FindClanByName(out interface{}, name string) *gorm.DB {
|
|
||||||
return models.DB.Where("name = ?", name).First(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindClanByTag(out interface{}, tag string) *gorm.DB {
|
|
||||||
return models.DB.Where("tag = ?", tag).First(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindClanByID(out interface{}, c *gin.Context) *gorm.DB {
|
|
||||||
return models.DB.Where("id = ?", c.Param("id")).First(out)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"InfantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateCode POST /code
|
// CreateCode POST /code
|
||||||
@@ -12,6 +13,7 @@ func CreateCode(c *gin.Context) {
|
|||||||
userRole, ok := c.GetPostForm("user_role")
|
userRole, ok := c.GetPostForm("user_role")
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusBadRequest, "Missing user role")
|
c.String(http.StatusBadRequest, "Missing user role")
|
||||||
|
utils.Logger.Error("Missing user role")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var role models.Role
|
var role models.Role
|
||||||
@@ -24,14 +26,18 @@ func CreateCode(c *gin.Context) {
|
|||||||
role = models.ReaderRole
|
role = models.ReaderRole
|
||||||
default:
|
default:
|
||||||
c.String(http.StatusInternalServerError, "Invalid user role: "+userRole)
|
c.String(http.StatusInternalServerError, "Invalid user role: "+userRole)
|
||||||
|
utils.Logger.Error("Invalid user role: " + userRole)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newCode := utils.GenerateActivationCode()
|
newCode := utils.GenerateActivationCode()
|
||||||
newCodeObj := models.ActivationCode{Code: newCode, UserRole: role}
|
newCodeObj := models.ActivationCode{Code: newCode, UserRole: role}
|
||||||
if err := models.DB.Create(&newCodeObj).Error; err != nil {
|
if err := models.DB.Create(&newCodeObj).Error; err != nil {
|
||||||
c.String(http.StatusInternalServerError, "Failed to create new code: "+err.Error())
|
c.String(http.StatusInternalServerError, "Failed to create new code: "+err.Error())
|
||||||
|
utils.Logger.Error("Failed to create new code: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.String(http.StatusOK, "Activation code for role '"+string(role)+"': "+newCode)
|
c.String(http.StatusOK, "Activation code for role '"+string(role)+"': "+newCode)
|
||||||
|
utils.Logger.Info("Activation code for role '" + string(role) + "' created: " + newCode[:4] + strings.Repeat("*", 28))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"InfantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
|
"InfantrySkillCalculator/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm/clause"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,7 +22,11 @@ type UpdateGameInput struct {
|
|||||||
// GetGames GET /game
|
// GetGames GET /game
|
||||||
func GetGames(c *gin.Context) {
|
func GetGames(c *gin.Context) {
|
||||||
var games []models.Game
|
var games []models.Game
|
||||||
models.DB.Find(&games)
|
if err := models.DB.Find(&games).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, games)
|
c.JSON(http.StatusOK, games)
|
||||||
}
|
}
|
||||||
@@ -29,7 +34,11 @@ func GetGames(c *gin.Context) {
|
|||||||
// GetGamesHTML GET /game_html
|
// GetGamesHTML GET /game_html
|
||||||
func GetGamesHTML(c *gin.Context) {
|
func GetGamesHTML(c *gin.Context) {
|
||||||
var games []models.Game
|
var games []models.Game
|
||||||
models.DB.Find(&games)
|
if err := models.DB.Find(&games).Error; err != nil {
|
||||||
|
c.String(http.StatusBadRequest, "")
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var htmlOptions string
|
var htmlOptions string
|
||||||
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
||||||
@@ -46,6 +55,7 @@ func GetGameByID(c *gin.Context) {
|
|||||||
var game models.Game
|
var game models.Game
|
||||||
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,11 +67,16 @@ func AddGame(c *gin.Context) {
|
|||||||
var input AddGameInput
|
var input AddGameInput
|
||||||
if err := c.BindJSON(&input); err != nil {
|
if err := c.BindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
game := models.Game{Name: input.Name, Tag: input.Tag}
|
game := models.Game{Name: input.Name, Tag: input.Tag}
|
||||||
models.DB.Create(&game)
|
if err := models.DB.Create(&game).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, game)
|
c.JSON(http.StatusOK, game)
|
||||||
}
|
}
|
||||||
@@ -71,12 +86,14 @@ func UpdateGameByID(c *gin.Context) {
|
|||||||
var game models.Game
|
var game models.Game
|
||||||
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[GAME] Record not found: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var input UpdateGameInput
|
var input UpdateGameInput
|
||||||
if err := c.ShouldBindJSON(&input); err != nil {
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[GAME] Could not parse input: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,20 +105,13 @@ func UpdateGameByID(c *gin.Context) {
|
|||||||
// DeleteGameByID DELETE /game/:id
|
// DeleteGameByID DELETE /game/:id
|
||||||
func DeleteGameByID(c *gin.Context) {
|
func DeleteGameByID(c *gin.Context) {
|
||||||
var game models.Game
|
var game models.Game
|
||||||
if err := models.DB.Where("id = ?", c.Param("id")).First(&game).Error; err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
if err := models.DB.Clauses(clause.Returning{}).Delete(&game).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[GAME] Error while deleting game: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Delete(&game)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
}
|
utils.Logger.Infof("[GAME] Deleted game: %s", game.Name)
|
||||||
|
|
||||||
func FindGame(out interface{}, c *gin.Context) *gorm.DB {
|
|
||||||
return models.DB.Where("id = ?", c.Param("id")).First(&out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindGameByTag(out interface{}, tag string) *gorm.DB {
|
|
||||||
return models.DB.Where("tag = ?", tag).First(out)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ var gameMetrics models.GameMetrics
|
|||||||
func LoadMetrics() {
|
func LoadMetrics() {
|
||||||
f, err := os.Open("./config/metrics.json")
|
f, err := os.Open("./config/metrics.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal("Failed to open metrics.json: ", err)
|
utils.Logger.Fatal("[METRICS] Failed to open metrics.json: ", err)
|
||||||
}
|
}
|
||||||
defer func(f *os.File) {
|
defer func(f *os.File) {
|
||||||
_ = f.Close()
|
_ = f.Close()
|
||||||
@@ -21,12 +21,12 @@ func LoadMetrics() {
|
|||||||
|
|
||||||
data, err := io.ReadAll(f)
|
data, err := io.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal("Failed to read metrics.json: ", err)
|
utils.Logger.Fatal("[METRICS] Failed to read metrics.json: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var metrics models.GameMetrics
|
var metrics models.GameMetrics
|
||||||
if err := json.Unmarshal(data, &metrics); err != nil {
|
if err := json.Unmarshal(data, &metrics); err != nil {
|
||||||
utils.Logger.Fatal("Failed to deserialize metrics.json: ", err)
|
utils.Logger.Fatal("[METRICS] Failed to deserialize metrics.json: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gameMetrics = metrics
|
gameMetrics = metrics
|
||||||
|
|||||||
@@ -24,7 +24,11 @@ type UpdatePlayerInput struct {
|
|||||||
// GetAllPlayers GET /player
|
// GetAllPlayers GET /player
|
||||||
func GetAllPlayers(c *gin.Context) {
|
func GetAllPlayers(c *gin.Context) {
|
||||||
var players []models.Player
|
var players []models.Player
|
||||||
models.DB.Find(&players)
|
if err := models.DB.Find(&players).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Record not found! Error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, players)
|
c.JSON(http.StatusOK, players)
|
||||||
}
|
}
|
||||||
@@ -38,14 +42,14 @@ func GetPlayersByClanHTML(c *gin.Context) {
|
|||||||
Find(&players).Error; err != nil {
|
Find(&players).Error; err != nil {
|
||||||
|
|
||||||
c.String(http.StatusBadRequest, "")
|
c.String(http.StatusBadRequest, "")
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Errorf("[PLAYER] Clan not found! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
game, err := GetActiveGame(c)
|
game, err := GetActiveGame(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "")
|
c.String(http.StatusBadRequest, "")
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Errorf("[PLAYER] No active game not found! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +61,8 @@ func GetPlayersByClanHTML(c *gin.Context) {
|
|||||||
scores, err := models.PlayerCache.GetScores(playerIDs, game.Tag)
|
scores, err := models.PlayerCache.GetScores(playerIDs, game.Tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "")
|
c.String(http.StatusBadRequest, "")
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Errorf("[PLAYER] Could not get scores! Error: %s", err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userRole := GetUserRoleByCtx(c)
|
userRole := GetUserRoleByCtx(c)
|
||||||
@@ -83,92 +88,95 @@ func GetPlayersByClanHTML(c *gin.Context) {
|
|||||||
|
|
||||||
err = utils.PlayerItemTemplate.Execute(c.Writer, data)
|
err = utils.PlayerItemTemplate.Execute(c.Writer, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Errorf("[PLAYER] Could not execute template! Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPlayer POST /player
|
// AddPlayer POST /player
|
||||||
func AddPlayer(c *gin.Context) {
|
func AddPlayer(c *gin.Context) {
|
||||||
var input AddPlayerInput
|
var input AddPlayerInput
|
||||||
|
var player models.Player
|
||||||
|
|
||||||
if err := c.BindJSON(&input); err != nil {
|
if err := c.BindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
utils.Logger.Errorf("[PLAYER] Could not bind JSON! Error: %s", err.Error())
|
||||||
}
|
|
||||||
|
|
||||||
var player models.Player
|
|
||||||
if err := models.DB.Where("name = ?", input.Name).First(&player).Error; err == nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Player with this name already exists!"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
player = models.Player{Name: input.Name, ClanID: input.ClanID}
|
player = models.Player{Name: input.Name, ClanID: input.ClanID}
|
||||||
models.DB.Create(&player)
|
if err := models.DB.Create(&player); err.Error != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error.Error()})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not create player! Error: %s", err.Error.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, player)
|
c.JSON(http.StatusOK, player)
|
||||||
|
|
||||||
utils.Logger.Infof("Added player '%s'", input.Name)
|
utils.Logger.Infof("[PLAYER] Added player '%s' in clan %d", input.Name, input.ClanID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlayerByID GET /player/:id
|
// GetPlayerByID GET /player/:id
|
||||||
func GetPlayerByID(c *gin.Context) {
|
func GetPlayerByID(c *gin.Context) {
|
||||||
player := FindPlayerByID(utils.StringToUint(c.Param("id")))
|
var player models.Player
|
||||||
if player == nil {
|
|
||||||
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&player).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not find player! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, player)
|
c.JSON(http.StatusOK, player)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlayerIDByName GET /playerid/:name
|
|
||||||
func GetPlayerIDByName(c *gin.Context) {
|
|
||||||
var player models.Player
|
|
||||||
|
|
||||||
if err := FindPlayerByName(&player, c).Error; err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, player.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdatePlayerByID PATCH /player/:id
|
// UpdatePlayerByID PATCH /player/:id
|
||||||
func UpdatePlayerByID(c *gin.Context) {
|
func UpdatePlayerByID(c *gin.Context) {
|
||||||
var input UpdatePlayerInput
|
var input UpdatePlayerInput
|
||||||
|
var player models.Player
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&input); err != nil {
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not bind JSON! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res := models.DB.Model(&models.Player{}).
|
if err := models.DB.First(&player, c.Param("id")).Error; err != nil {
|
||||||
Where("id = ?", utils.StringToUint(c.Param("id"))).
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
||||||
Updates(map[string]interface{}{
|
utils.Logger.Errorf("[PLAYER] Could not find player! Error: %s", err.Error())
|
||||||
"Name": input.Name,
|
return
|
||||||
"ClanID": input.ClanID,
|
}
|
||||||
})
|
|
||||||
if res.Error != nil {
|
oldValues := models.Player{
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
Name: player.Name,
|
||||||
|
ClanID: player.ClanID,
|
||||||
|
}
|
||||||
|
|
||||||
|
player.Name = input.Name
|
||||||
|
player.ClanID = input.ClanID
|
||||||
|
|
||||||
|
if err := models.DB.Save(&player).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not update player! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
|
|
||||||
utils.Logger.Infof("Updated player '%s'", input.Name)
|
utils.Logger.Infof("[PLAYER] Updated player: %s -> %s", oldValues.Name, player.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePlayerByID DELETE /player/:id
|
// DeletePlayerByID DELETE /player/:id
|
||||||
func DeletePlayerByID(c *gin.Context) {
|
func DeletePlayerByID(c *gin.Context) {
|
||||||
player := FindPlayerByID(utils.StringToUint(c.Param("id")))
|
var player models.Player
|
||||||
if player == nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
if err := models.DB.Clauses(clause.Returning{}).Delete(&player).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not delete player! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Delete(&player)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
|
|
||||||
utils.Logger.Infof("Deleted player '%s'", player.Name)
|
utils.Logger.Infof("[PLAYER] Deleted player: %s", player.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAllPlayers DELETE /admin/player
|
// DeleteAllPlayers DELETE /admin/player
|
||||||
@@ -178,22 +186,12 @@ func DeleteAllPlayers(c *gin.Context) {
|
|||||||
Session(&gorm.Session{AllowGlobalUpdate: true}).
|
Session(&gorm.Session{AllowGlobalUpdate: true}).
|
||||||
Clauses(clause.Returning{}).
|
Clauses(clause.Returning{}).
|
||||||
Delete(&players).Error; err != nil {
|
Delete(&players).Error; err != nil {
|
||||||
|
|
||||||
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
|
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
|
||||||
|
utils.Logger.Errorf("[PLAYER] Could not purge players! Error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(players)))+" players!")
|
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(players)))+" players!")
|
||||||
}
|
utils.Logger.Infof("[PLAYER] Purged %d players!", len(players))
|
||||||
|
|
||||||
func FindPlayerByID(id uint) *models.Player {
|
|
||||||
var res models.Player
|
|
||||||
if err := models.DB.Where("id = ?", id).First(&res).Error; err != nil {
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return &res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindPlayerByName(out interface{}, c *gin.Context) *gorm.DB {
|
|
||||||
return models.DB.Where("name = ?", c.Param("name")).First(out)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,20 +15,17 @@ import (
|
|||||||
// GetScoreByPlayerID GET /score/:player_id
|
// GetScoreByPlayerID GET /score/:player_id
|
||||||
func GetScoreByPlayerID(c *gin.Context) {
|
func GetScoreByPlayerID(c *gin.Context) {
|
||||||
var player models.Player
|
var player models.Player
|
||||||
var playerId = utils.StringToUint(c.Param("player_id"))
|
|
||||||
|
|
||||||
if err := models.DB.
|
|
||||||
Model(&models.Player{}).
|
|
||||||
Where("id = ?", playerId).
|
|
||||||
First(&player).Error; err != nil {
|
|
||||||
|
|
||||||
|
if err := models.DB.Where("id = ?", c.Param("player_id")).First(&player).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Player not found!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Player not found!"})
|
||||||
|
utils.Logger.Errorf("[SCORE] Player not found: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
game, err := GetActiveGame(c)
|
game, err := GetActiveGame(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||||
|
utils.Logger.Errorf("[SCORE] No active game available: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +34,8 @@ func GetScoreByPlayerID(c *gin.Context) {
|
|||||||
score = CalcPlayerScore(player.Name, game.Tag)
|
score = CalcPlayerScore(player.Name, game.Tag)
|
||||||
if score == score && score != -1 { // not NaN
|
if score == score && score != -1 { // not NaN
|
||||||
if err := models.PlayerCache.SetScore(player.ID, game.Tag, score); err != nil {
|
if err := models.PlayerCache.SetScore(player.ID, game.Tag, score); err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Errorf("[SCORE] Failed to cache player score: %s", err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,19 +46,20 @@ func GetScoreByPlayerID(c *gin.Context) {
|
|||||||
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusBadRequest, "Invalid request!")
|
c.JSON(http.StatusBadRequest, "Invalid request!")
|
||||||
|
utils.Logger.Warnf("[SCORE] Invalid request! Player: %s, Game: %s | Score: %f", player.Name, game.Tag, score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetScoreByPlayerName POST /score/:player_name
|
// GetScoreByPlayerName POST /score/:player_name
|
||||||
func GetScoreByPlayerName(c *gin.Context) {
|
func GetScoreByPlayerName(c *gin.Context) {
|
||||||
playerName := c.Param("player_name")
|
|
||||||
game, err := GetActiveGame(c)
|
game, err := GetActiveGame(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||||
|
utils.Logger.Errorf("[SCORE] No active game available: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
score := CalcPlayerScore(playerName, game.Tag)
|
score := CalcPlayerScore(c.Param("player_name"), game.Tag)
|
||||||
|
|
||||||
if score != score || score == -1 { // NaN
|
if score != score || score == -1 { // NaN
|
||||||
c.String(http.StatusNotFound, "Spieler nicht gefunden!")
|
c.String(http.StatusNotFound, "Spieler nicht gefunden!")
|
||||||
@@ -68,18 +67,19 @@ func GetScoreByPlayerName(c *gin.Context) {
|
|||||||
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
||||||
} else {
|
} else {
|
||||||
c.String(http.StatusBadRequest, "Ungültige Abfrage!")
|
c.String(http.StatusBadRequest, "Ungültige Abfrage!")
|
||||||
|
utils.Logger.Warnf("[SCORE] Invalid request! Player: %s, Game: %s | Score: %f", c.Param("player_name"), game.Tag, score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalcPlayerScore(playerName string, gameTag string) float32 {
|
func CalcPlayerScore(playerName string, gameTag string) float32 {
|
||||||
if gameTag != "BF5" && gameTag != "BF2042" {
|
if gameTag != "BF5" && gameTag != "BF2042" {
|
||||||
utils.Logger.Errorf("Invalid game tag '%s'", gameTag)
|
utils.Logger.Errorf("[SCORE] Invalid game tag '%s'", gameTag)
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
gameMetrics := GetGameMetric(gameTag)
|
gameMetrics := GetGameMetric(gameTag)
|
||||||
if gameMetrics == nil {
|
if gameMetrics == nil {
|
||||||
utils.Logger.Errorf("No game metrics specified for '%s'", gameTag)
|
utils.Logger.Errorf("[SCORE] No game metrics specified for '%s'", gameTag)
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ func CalcPlayerScore(playerName string, gameTag string) float32 {
|
|||||||
|
|
||||||
req, err := http.NewRequest("GET", reqUri, nil)
|
req, err := http.NewRequest("GET", reqUri, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Errorf("Failed to create request: %s", err)
|
utils.Logger.Errorf("[SCORE] Failed to create request: %s", err.Error())
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ func CalcPlayerScore(playerName string, gameTag string) float32 {
|
|||||||
|
|
||||||
res, err := c.Do(req)
|
res, err := c.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Errorf("Failed to send request: %s", err)
|
utils.Logger.Errorf("[SCORE] Failed to send request: %s", err.Error())
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,16 +113,18 @@ func CalcPlayerScore(playerName string, gameTag string) float32 {
|
|||||||
_ = Body.Close()
|
_ = Body.Close()
|
||||||
}(res.Body)
|
}(res.Body)
|
||||||
if res.StatusCode == 404 {
|
if res.StatusCode == 404 {
|
||||||
utils.Logger.Errorf("User '%s' does not exist!", playerName)
|
utils.Logger.Errorf("[SCORE] User '%s' does not exist!", playerName)
|
||||||
return -1
|
return -1
|
||||||
} else if res.StatusCode != 200 {
|
} else if res.StatusCode != 200 {
|
||||||
utils.Logger.Fatalf("Status code error: %d %s", res.StatusCode, res.Status)
|
utils.Logger.Errorf("[SCORE] Status code error: %d %s", res.StatusCode, res.Status)
|
||||||
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
var data models.TrackerWeaponJSON
|
var data models.TrackerWeaponJSON
|
||||||
var body, _ = io.ReadAll(res.Body)
|
var body, _ = io.ReadAll(res.Body)
|
||||||
if err := json.Unmarshal(body, &data); err != nil {
|
if err := json.Unmarshal(body, &data); err != nil {
|
||||||
utils.Logger.Fatalf("Failed to deserialize tracker API response: %s", err)
|
utils.Logger.Errorf("Failed to deserialize tracker API response: %s", err)
|
||||||
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.SliceStable(data.Weapons, func(i, j int) bool { return data.Weapons[i].Kills > data.Weapons[j].Kills })
|
sort.SliceStable(data.Weapons, func(i, j int) bool { return data.Weapons[i].Kills > data.Weapons[j].Kills })
|
||||||
@@ -141,7 +143,7 @@ func CalcPlayerScore(playerName string, gameTag string) float32 {
|
|||||||
|
|
||||||
weaponMetrics := FindWeaponMetric(gameMetrics.WeaponMetrics, weapon.Type)
|
weaponMetrics := FindWeaponMetric(gameMetrics.WeaponMetrics, weapon.Type)
|
||||||
if weaponMetrics == nil {
|
if weaponMetrics == nil {
|
||||||
utils.Logger.Error("No weapon metrics specified for '" + gameTag + "', WType '" + weapon.Type + "'! Skipping...")
|
utils.Logger.Errorf("[SCORE] No weapon metrics specified for '%s'", weapon.Type)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,39 +9,31 @@ import (
|
|||||||
|
|
||||||
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
|
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
|
||||||
user := models.User{Username: username, Password: hashedPassword, Enabled: enabled}
|
user := models.User{Username: username, Password: hashedPassword, Enabled: enabled}
|
||||||
err := models.DB.Create(&user).Error
|
var code models.ActivationCode
|
||||||
if err != nil {
|
var bf2042 models.Game
|
||||||
utils.Logger.Fatalf("Error while creating user: %v", err)
|
|
||||||
|
if err := models.DB.Create(&user).Error; err != nil {
|
||||||
|
utils.Logger.Fatalf("[USER] Error while creating user: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var code models.ActivationCode
|
if err := models.DB.Where("code = ?", usedCode).First(&code).Error; err != nil {
|
||||||
err = models.DB.
|
utils.Logger.Fatalf("[USER] Error while getting activation code: %s", err.Error())
|
||||||
Model(&models.ActivationCode{}).
|
|
||||||
Where("code = ?", usedCode).
|
|
||||||
First(&code).Error
|
|
||||||
if err != nil {
|
|
||||||
utils.Logger.Fatalf("Error while getting activation code: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code.UsedForUsername = username
|
code.UsedForUsername = username
|
||||||
err = models.DB.Save(&code).Error
|
if err := models.DB.Save(&code).Error; err != nil {
|
||||||
if err != nil {
|
utils.Logger.Fatalf("[USER] Error while updating activation code: %s", err.Error())
|
||||||
utils.Logger.Fatalf("Error while updating activation code: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user.UserRole = code.UserRole
|
user.UserRole = code.UserRole
|
||||||
err = models.DB.Save(&user).Error
|
if err := models.DB.Save(&user).Error; err != nil {
|
||||||
if err != nil {
|
utils.Logger.Fatalf("[USER] Error while updating user role: %s", err.Error())
|
||||||
utils.Logger.Fatalf("Error while updating user role: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var bf2042 models.Game
|
if err := models.DB.Where("tag = ?", "BF2042").First(&bf2042).Error; err != nil {
|
||||||
err = models.DB.
|
utils.Logger.Fatalf("[USER] Error while getting game: %v", err)
|
||||||
Where("tag = ?", "BF2042").
|
|
||||||
First(&bf2042).Error
|
|
||||||
if err != nil {
|
|
||||||
utils.Logger.Fatalf("Error while getting game: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
userSettings := models.UserSettings{
|
userSettings := models.UserSettings{
|
||||||
Username: username,
|
Username: username,
|
||||||
ActiveGameID: bf2042.ID,
|
ActiveGameID: bf2042.ID,
|
||||||
@@ -49,38 +41,50 @@ func CreateUser(username string, hashedPassword string, enabled bool, usedCode s
|
|||||||
CalcMedian: false,
|
CalcMedian: false,
|
||||||
UseCache: true,
|
UseCache: true,
|
||||||
}
|
}
|
||||||
models.DB.Create(&userSettings)
|
|
||||||
|
if err := models.DB.Create(&userSettings).Error; err != nil {
|
||||||
|
utils.Logger.Fatalf("[USER] Error while creating user settings: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Logger.Infof("[USER] Created user: %s", username)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsUserEnabled(username string) bool {
|
func IsUserEnabled(username string) bool {
|
||||||
var user models.User
|
var user models.User
|
||||||
models.DB.Where("username = ?", username).First(&user)
|
|
||||||
|
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||||
|
utils.Logger.Fatalf("[USER] Error while getting user: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return user.Enabled
|
return user.Enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserRole(username string) models.Role {
|
func GetUserRole(username string) models.Role {
|
||||||
var user models.User
|
var user models.User
|
||||||
err := models.DB.Where("username = ?", username).First(&user).Error
|
|
||||||
if err != nil {
|
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[USER] Error while getting user: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.UserRole
|
return user.UserRole
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserRoleByCtx(c *gin.Context) models.Role {
|
func GetUserRoleByCtx(c *gin.Context) models.Role {
|
||||||
username, ok := session.GetUsername(c)
|
username, ok := session.GetUsername(c)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return models.ReaderRole
|
return models.ReaderRole
|
||||||
}
|
}
|
||||||
|
|
||||||
return GetUserRole(username)
|
return GetUserRole(username)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsUserAdmin(username string) bool {
|
func IsUserAdmin(username string) bool {
|
||||||
var user models.User
|
var user models.User
|
||||||
err := models.DB.Where("username = ?", username).First(&user).Error
|
|
||||||
if err != nil {
|
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[USER] Error while getting user: %s", err.Error())
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.UserRole == models.AdminRole
|
return user.UserRole == models.AdminRole
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"InfantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
|
"InfantrySkillCalculator/utils"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -22,11 +23,13 @@ func GetSettings(c *gin.Context) {
|
|||||||
username, ok := session.GetUsername(c)
|
username, ok := session.GetUsername(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,16 +51,19 @@ func GetActiveGame(c *gin.Context) (models.Game, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
err := errors.New("not logged in")
|
err := errors.New("not logged in")
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
|
||||||
return models.Game{}, err
|
return models.Game{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
|
||||||
return models.Game{}, err
|
return models.Game{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.DB.Where("id = ?", settings.ActiveGameID).First(&game).Error; err != nil {
|
if err := models.DB.Where("id = ?", settings.ActiveGameID).First(&game).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] No active game available for user %s", username)
|
||||||
return models.Game{}, err
|
return models.Game{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,21 +77,28 @@ func UpdateSettings(c *gin.Context) {
|
|||||||
username, ok := session.GetUsername(c)
|
username, ok := session.GetUsername(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Not logged in!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] User not logged in: %s", username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] No settings available for user %s", username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var input UpdateSettingsInput
|
var input UpdateSettingsInput
|
||||||
if err := c.ShouldBindJSON(&input); err != nil {
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] Failed to bind JSON: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Model(&settings).Updates(input)
|
if err := models.DB.Model(&settings).Updates(input).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
utils.Logger.Errorf("[SETTINGS] Failed to update settings: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
27
main.go
27
main.go
@@ -16,7 +16,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
f, _ := os.OpenFile("isc_rest.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
f, err := os.OpenFile("isc_rest.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||||
|
if err != nil {
|
||||||
|
utils.Logger.Fatalf("[MAIN] Error opening log file: %v", err)
|
||||||
|
}
|
||||||
ginWriter := io.MultiWriter(f, os.Stdout)
|
ginWriter := io.MultiWriter(f, os.Stdout)
|
||||||
logger := logrus.New()
|
logger := logrus.New()
|
||||||
logger.SetOutput(ginWriter)
|
logger.SetOutput(ginWriter)
|
||||||
@@ -26,7 +29,6 @@ func init() {
|
|||||||
})
|
})
|
||||||
utils.Logger = logger
|
utils.Logger = logger
|
||||||
|
|
||||||
var err error
|
|
||||||
utils.MainPageTemplates, err = template.ParseFiles(
|
utils.MainPageTemplates, err = template.ParseFiles(
|
||||||
"./templates/index.html",
|
"./templates/index.html",
|
||||||
"./templates/components/home_clan_bar.html",
|
"./templates/components/home_clan_bar.html",
|
||||||
@@ -45,7 +47,7 @@ func init() {
|
|||||||
"./templates/components/header.html",
|
"./templates/components/header.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatalf("[MAIN] Error parsing main page templates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.LoginPageTemplates, err = template.ParseFiles(
|
utils.LoginPageTemplates, err = template.ParseFiles(
|
||||||
@@ -53,7 +55,7 @@ func init() {
|
|||||||
"./templates/components/header.html",
|
"./templates/components/header.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatalf("[MAIN] Error parsing login page templates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.RegisterPageTemplates, err = template.ParseFiles(
|
utils.RegisterPageTemplates, err = template.ParseFiles(
|
||||||
@@ -61,14 +63,14 @@ func init() {
|
|||||||
"./templates/components/header.html",
|
"./templates/components/header.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatalf("[MAIN] Error parsing register page templates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.PlayerItemTemplate, err = template.ParseFiles(
|
utils.PlayerItemTemplate, err = template.ParseFiles(
|
||||||
"./templates/shards/player_list_item.html",
|
"./templates/shards/player_list_item.html",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatalf("[MAIN] Error parsing player list item template: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
controllers.LoadMetrics()
|
controllers.LoadMetrics()
|
||||||
@@ -92,9 +94,9 @@ func customLogrusLogger(logger *logrus.Logger) gin.HandlerFunc {
|
|||||||
username = "Anonymous"
|
username = "Anonymous"
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof("[GIN] %3d | %10v | %15s | %15s | %-7s | %s",
|
logger.Infof("[GIN] %3d | %10.3fms | %15s | %15s | %-7s | %s",
|
||||||
c.Writer.Status(),
|
c.Writer.Status(),
|
||||||
elapsed,
|
float64(elapsed.Microseconds())/1000.0,
|
||||||
c.ClientIP(),
|
c.ClientIP(),
|
||||||
username,
|
username,
|
||||||
c.Request.Method,
|
c.Request.Method,
|
||||||
@@ -111,7 +113,7 @@ func main() {
|
|||||||
router := gin.New()
|
router := gin.New()
|
||||||
err := router.SetTrustedProxies([]string{"127.0.0.1"})
|
err := router.SetTrustedProxies([]string{"127.0.0.1"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[MAIN] Error setting trusted proxies: %v", err)
|
||||||
}
|
}
|
||||||
router.LoadHTMLGlob("templates/**/*")
|
router.LoadHTMLGlob("templates/**/*")
|
||||||
reader := router.Group("/")
|
reader := router.Group("/")
|
||||||
@@ -128,7 +130,7 @@ func main() {
|
|||||||
if err := models.DB.First(&code).Error; err != nil {
|
if err := models.DB.First(&code).Error; err != nil {
|
||||||
firstCode := utils.GenerateActivationCode()
|
firstCode := utils.GenerateActivationCode()
|
||||||
models.DB.Create(&models.ActivationCode{Code: firstCode, UserRole: models.AdminRole})
|
models.DB.Create(&models.ActivationCode{Code: firstCode, UserRole: models.AdminRole})
|
||||||
utils.Logger.Println("Created first activation code with ADMIN role:\n" + firstCode)
|
utils.Logger.Println("[MAIN] Created first activation code with ADMIN role:\n" + firstCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
router.Static("/static", "./static")
|
router.Static("/static", "./static")
|
||||||
@@ -161,7 +163,6 @@ func main() {
|
|||||||
reader.GET("/players", controllers.GetAllPlayers)
|
reader.GET("/players", controllers.GetAllPlayers)
|
||||||
reader.GET("/players_html", controllers.GetPlayersByClanHTML)
|
reader.GET("/players_html", controllers.GetPlayersByClanHTML)
|
||||||
reader.GET("/player/:id", controllers.GetPlayerByID)
|
reader.GET("/player/:id", controllers.GetPlayerByID)
|
||||||
reader.GET("/playerid/:name", controllers.GetPlayerIDByName)
|
|
||||||
author.POST("/player", controllers.AddPlayer)
|
author.POST("/player", controllers.AddPlayer)
|
||||||
author.PATCH("/player/:id", controllers.UpdatePlayerByID)
|
author.PATCH("/player/:id", controllers.UpdatePlayerByID)
|
||||||
author.DELETE("/player/:id", controllers.DeletePlayerByID)
|
author.DELETE("/player/:id", controllers.DeletePlayerByID)
|
||||||
@@ -182,6 +183,6 @@ func main() {
|
|||||||
admin.DELETE("/purge_clans", controllers.DeleteAllClans)
|
admin.DELETE("/purge_clans", controllers.DeleteAllClans)
|
||||||
admin.POST("/create_code", controllers.CreateCode)
|
admin.POST("/create_code", controllers.CreateCode)
|
||||||
|
|
||||||
utils.Logger.Println("Running on 8000...")
|
utils.Logger.Println("[MAIN] Running on 8000...")
|
||||||
utils.Logger.Fatal(router.Run(":8000"))
|
utils.Logger.Fatalf("[MAIN] %v", router.Run(":8000"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package models
|
|||||||
|
|
||||||
type Clan struct {
|
type Clan struct {
|
||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name" gorm:"unique"`
|
||||||
Tag string `json:"tag"`
|
Tag string `json:"tag" gorm:"unique"`
|
||||||
KeepUpdated bool `json:"keep_updated"`
|
KeepUpdated bool `json:"keep_updated"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,40 +21,40 @@ func ConnectDatabase() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to connect to database! " + err.Error())
|
utils.Logger.Fatal("[SETUP] Failed to connect to database! " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = database.AutoMigrate(&Clan{})
|
err = database.AutoMigrate(&Clan{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
}
|
}
|
||||||
err = database.AutoMigrate(&Player{})
|
err = database.AutoMigrate(&Player{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
}
|
}
|
||||||
err = database.AutoMigrate(&User{})
|
err = database.AutoMigrate(&User{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
}
|
}
|
||||||
err = database.AutoMigrate(&ActivationCode{})
|
err = database.AutoMigrate(&ActivationCode{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
}
|
}
|
||||||
err = database.AutoMigrate(&Game{})
|
err = database.AutoMigrate(&Game{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
var game Game
|
var game Game
|
||||||
if err := database.First(&game).Error; err != nil {
|
if err := database.First(&game).Error; err != nil {
|
||||||
database.Create(&Game{Name: "Battlefield V", Tag: "BFV"})
|
database.Create(&Game{Name: "Battlefield V", Tag: "BFV"})
|
||||||
database.Create(&Game{Name: "Battlefield 2042", Tag: "BF2042"})
|
database.Create(&Game{Name: "Battlefield 2042", Tag: "BF2042"})
|
||||||
utils.Logger.Println("Created first games")
|
utils.Logger.Println("[SETUP] Created first games")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = database.AutoMigrate(&UserSettings{})
|
err = database.AutoMigrate(&UserSettings{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatal("[SETUP] Failed to migrate database! " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
DB = database
|
DB = database
|
||||||
@@ -69,6 +69,6 @@ func ConnectCache(playerCacheLifetime time.Duration, expireCallback func(key str
|
|||||||
PlayerCache = cache.NewPlayerCache(address, playerCacheLifetime, expireCallback)
|
PlayerCache = cache.NewPlayerCache(address, playerCacheLifetime, expireCallback)
|
||||||
if err := PlayerCache.Connect(); err != nil {
|
if err := PlayerCache.Connect(); err != nil {
|
||||||
PlayerCache = nil
|
PlayerCache = nil
|
||||||
utils.Logger.Fatal("Failed to connect to Redis! " + err.Error())
|
utils.Logger.Fatal("[SETUP] Failed to connect to Redis! " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
pages.go
12
pages.go
@@ -21,7 +21,7 @@ func mainPage(c *gin.Context) {
|
|||||||
|
|
||||||
err := utils.MainPageTemplates.Execute(c.Writer, data)
|
err := utils.MainPageTemplates.Execute(c.Writer, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[MAIN] Error while executing template: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ func loginPage(c *gin.Context) {
|
|||||||
|
|
||||||
err := utils.LoginPageTemplates.Execute(c.Writer, nil)
|
err := utils.LoginPageTemplates.Execute(c.Writer, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[LOGIN] Error while executing template: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,11 +43,13 @@ func loginPost(c *gin.Context) {
|
|||||||
|
|
||||||
if !checkUserCredentials(username, password) {
|
if !checkUserCredentials(username, password) {
|
||||||
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültige Logindaten!"})
|
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültige Logindaten!"})
|
||||||
|
utils.Logger.Warnf("[LOGIN] User %s tried to login with invalid credentials", username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := session.SetLoginSession(username, c); err != nil {
|
if err := session.SetLoginSession(username, c); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, nil)
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
utils.Logger.Errorf("[LOGIN] Error while setting login session: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +60,7 @@ func loginPost(c *gin.Context) {
|
|||||||
func logout(c *gin.Context) {
|
func logout(c *gin.Context) {
|
||||||
if err := session.InvalidateSession(c); err != nil {
|
if err := session.InvalidateSession(c); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, nil)
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
utils.Logger.Errorf("[LOGOUT] Error while invalidating session: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +75,7 @@ func registerPage(c *gin.Context) {
|
|||||||
|
|
||||||
err := utils.RegisterPageTemplates.Execute(c.Writer, nil)
|
err := utils.RegisterPageTemplates.Execute(c.Writer, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Logger.Fatal(err)
|
utils.Logger.Fatalf("[REGISTER] Error while executing template: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,12 +86,14 @@ func registerPost(c *gin.Context) {
|
|||||||
|
|
||||||
if !isValidCode(code) {
|
if !isValidCode(code) {
|
||||||
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültiger Aktivierungscode!"})
|
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültiger Aktivierungscode!"})
|
||||||
|
utils.Logger.Warnf("[REGISTER] User %s tried to register with invalid code %s", username, code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := hashPassword(password)
|
hashedPassword, err := hashPassword(password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Fehler beim Registrieren!"})
|
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Fehler beim Registrieren!"})
|
||||||
|
utils.Logger.Errorf("[REGISTER] Error while hashing password: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +101,7 @@ func registerPost(c *gin.Context) {
|
|||||||
|
|
||||||
if err := session.SetLoginSession(username, c); err != nil {
|
if err := session.SetLoginSession(username, c); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, nil)
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
|
utils.Logger.Errorf("[REGISTER] Error while setting login session: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ function showSingleCalcResultDialog(xhr, reqPath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
title: 'Abfrage für ' + reqPath.replace("/score/", ''),
|
title: 'Abfrage für "' + reqPath.replace("/score/", '') + '"',
|
||||||
text: "Score: " + xhr.response,
|
text: "Score: " + xhr.response,
|
||||||
icon: icon,
|
icon: icon,
|
||||||
customClass: swalClasses
|
customClass: swalClasses
|
||||||
|
|||||||
@@ -86,7 +86,7 @@
|
|||||||
if (reqPath.startsWith("/admin/")) {
|
if (reqPath.startsWith("/admin/")) {
|
||||||
showAdminActionExecutedDialog(xhr, method);
|
showAdminActionExecutedDialog(xhr, method);
|
||||||
} else if (reqPath.startsWith("/score/") && method === "post") {
|
} else if (reqPath.startsWith("/score/") && method === "post") {
|
||||||
showSingleCalcResultDialog(xhr, method);
|
showSingleCalcResultDialog(xhr, reqPath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ func UintToString(val uint) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func StringToUint(val string) uint {
|
func StringToUint(val string) uint {
|
||||||
res, _ := strconv.ParseUint(val, 10, 16)
|
res, err := strconv.ParseUint(val, 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
Logger.Warnf("StringToUint error for %s: %s", val, err.Error())
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return uint(res)
|
return uint(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,6 +23,10 @@ func FloatToString(val float32) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func StringToFloat(val string) float32 {
|
func StringToFloat(val string) float32 {
|
||||||
res, _ := strconv.ParseFloat(val, 32)
|
res, err := strconv.ParseFloat(val, 32)
|
||||||
|
if err != nil {
|
||||||
|
Logger.Warnf("StringToFloat error for %s: %s", val, err.Error())
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return float32(res)
|
return float32(res)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
func GenerateActivationCode() string {
|
func GenerateActivationCode() string {
|
||||||
bytes := make([]byte, 16)
|
bytes := make([]byte, 16)
|
||||||
if _, err := rand.Read(bytes); err != nil {
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
panic(err) // Handle the error appropriately in production
|
Logger.Fatalf("Error generating activation code: %v", err)
|
||||||
}
|
}
|
||||||
return hex.EncodeToString(bytes)
|
return hex.EncodeToString(bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user