Save hashed PW. Simplify model update. Bugfixes & improvements. Working user-settings & games.
This commit is contained in:
3
.idea/inspectionProfiles/Project_Default.xml
generated
3
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -4,11 +4,12 @@
|
|||||||
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
|
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
<option name="myValues">
|
<option name="myValues">
|
||||||
<value>
|
<value>
|
||||||
<list size="4">
|
<list size="5">
|
||||||
<item index="0" class="java.lang.String" itemvalue="hx-get" />
|
<item index="0" class="java.lang.String" itemvalue="hx-get" />
|
||||||
<item index="1" class="java.lang.String" itemvalue="hx-target" />
|
<item index="1" class="java.lang.String" itemvalue="hx-target" />
|
||||||
<item index="2" class="java.lang.String" itemvalue="hx-vals" />
|
<item index="2" class="java.lang.String" itemvalue="hx-vals" />
|
||||||
<item index="3" class="java.lang.String" itemvalue="hx-post" />
|
<item index="3" class="java.lang.String" itemvalue="hx-post" />
|
||||||
|
<item index="4" class="java.lang.String" itemvalue="hx-trigger" />
|
||||||
</list>
|
</list>
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
|
|||||||
2
.idea/modules.xml
generated
2
.idea/modules.xml
generated
@@ -2,7 +2,7 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<modules>
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/InfrantrySkillCalculator.iml" filepath="$PROJECT_DIR$/.idea/InfrantrySkillCalculator.iml" />
|
<module fileurl="file://$PROJECT_DIR$/.idea/InfantrySkillCalculator.iml" filepath="$PROJECT_DIR$/.idea/InfantrySkillCalculator.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
23
auth.go
23
auth.go
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/controllers"
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/models"
|
||||||
|
"InfantrySkillCalculator/utils"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -34,14 +35,7 @@ func getUserPassword(username string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var hashedPW string
|
return user.Password, nil
|
||||||
hashedPW, err := hashPassword(user.Password)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return hashedPW, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hashPassword(password string) (string, error) {
|
func hashPassword(password string) (string, error) {
|
||||||
@@ -54,8 +48,13 @@ func hashPassword(password string) (string, error) {
|
|||||||
|
|
||||||
func AuthRequired() gin.HandlerFunc {
|
func AuthRequired() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth || !controllers.IsUserEnabled(session.Values["username"].(string)) {
|
||||||
|
session.Options.MaxAge = -1
|
||||||
|
err := session.Save(c.Request, c.Writer)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
c.Redirect(http.StatusFound, "/login")
|
c.Redirect(http.StatusFound, "/login")
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddClanInput struct {
|
type AddClanInput struct {
|
||||||
@@ -66,8 +65,6 @@ func AddClan(c *gin.Context) {
|
|||||||
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)
|
models.DB.Create(&clan)
|
||||||
|
|
||||||
//UpdateClanTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, clan)
|
c.JSON(http.StatusOK, clan)
|
||||||
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, "Added clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
_, err := fmt.Fprintf(utils.GinWriter, "Added clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
||||||
@@ -90,45 +87,30 @@ func GetClanByID(c *gin.Context) {
|
|||||||
|
|
||||||
// UpdateClanByID PATCH /clan/:id
|
// UpdateClanByID PATCH /clan/:id
|
||||||
func UpdateClanByID(c *gin.Context) {
|
func UpdateClanByID(c *gin.Context) {
|
||||||
var clan models.Clan
|
|
||||||
if err := FindClanByID(&clan, c).Error; err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var input UpdateClanInput
|
var input UpdateClanInput
|
||||||
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()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := "Updating clan '" + clan.Name + "'#" + strconv.FormatUint(uint64(clan.ID), 10)
|
res := models.DB.Model(&models.Clan{}).
|
||||||
if clan.Name != input.Name {
|
Where("id = ?", c.Param("id")).
|
||||||
msg += " (new: '" + input.Name + "')"
|
Updates(map[string]interface{}{
|
||||||
}
|
"Name": input.Name,
|
||||||
msg += " with tag '" + clan.Tag + "'"
|
"Tag": input.Tag,
|
||||||
if clan.Tag != input.Tag {
|
"KeepUpdated": input.KeepUpdated,
|
||||||
msg += " (new: '" + input.Tag + "')"
|
})
|
||||||
}
|
if res.Error != nil {
|
||||||
msg += " with updating '" + strconv.FormatBool(clan.KeepUpdated) + "'"
|
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
||||||
if clan.KeepUpdated != input.KeepUpdated {
|
return
|
||||||
msg += " (new: '" + strconv.FormatBool(input.KeepUpdated) + "')"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
c.JSON(http.StatusOK, nil)
|
||||||
|
|
||||||
|
_, err := fmt.Fprintf(utils.GinWriter, "Updated clan '"+input.Name+"' with tag '"+input.Tag+"'\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Model(&clan).Updates(map[string]interface{}{
|
|
||||||
"Name": input.Name,
|
|
||||||
"Tag": input.Tag,
|
|
||||||
"KeepUpdated": input.KeepUpdated,
|
|
||||||
})
|
|
||||||
|
|
||||||
//UpdateClanTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, clan)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteClanByID DELETE /clan/:id
|
// DeleteClanByID DELETE /clan/:id
|
||||||
@@ -141,8 +123,6 @@ func DeleteClanByID(c *gin.Context) {
|
|||||||
|
|
||||||
models.DB.Delete(&clan)
|
models.DB.Delete(&clan)
|
||||||
|
|
||||||
//UpdateClanTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, "Deleted clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
_, err := fmt.Fprintf(utils.GinWriter, "Deleted clan '"+clan.Name+"' with tag '"+clan.Tag+"'\n")
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -25,6 +26,21 @@ func GetGames(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, games)
|
c.JSON(http.StatusOK, games)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGamesHTML GET /game_html
|
||||||
|
func GetGamesHTML(c *gin.Context) {
|
||||||
|
var games []models.Game
|
||||||
|
models.DB.Find(&games)
|
||||||
|
|
||||||
|
var htmlOptions string
|
||||||
|
htmlOptions = `<option disabled selected value>Auswählen...</option>`
|
||||||
|
for _, game := range games {
|
||||||
|
htmlOptions += fmt.Sprintf(`<option value="%d">%s</option>`, game.ID, game.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Header("Content-Type", "text/html")
|
||||||
|
c.String(http.StatusOK, htmlOptions)
|
||||||
|
}
|
||||||
|
|
||||||
// GetGameByID GET /game/:id
|
// GetGameByID GET /game/:id
|
||||||
func GetGameByID(c *gin.Context) {
|
func GetGameByID(c *gin.Context) {
|
||||||
var game models.Game
|
var game models.Game
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddPlayerInput struct {
|
type AddPlayerInput struct {
|
||||||
@@ -70,8 +69,6 @@ func AddPlayer(c *gin.Context) {
|
|||||||
player = models.Player{Name: input.Name, ClanID: input.ClanID}
|
player = models.Player{Name: input.Name, ClanID: input.ClanID}
|
||||||
models.DB.Create(&player)
|
models.DB.Create(&player)
|
||||||
|
|
||||||
//UpdatePlayerTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, player)
|
c.JSON(http.StatusOK, player)
|
||||||
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, "Added player '"+player.Name+"'\n")
|
_, err := fmt.Fprintf(utils.GinWriter, "Added player '"+player.Name+"'\n")
|
||||||
@@ -105,46 +102,29 @@ func GetPlayerIDByName(c *gin.Context) {
|
|||||||
|
|
||||||
// UpdatePlayerByID PATCH /player/:id
|
// UpdatePlayerByID PATCH /player/:id
|
||||||
func UpdatePlayerByID(c *gin.Context) {
|
func UpdatePlayerByID(c *gin.Context) {
|
||||||
player := FindPlayerByID(utils.StringToUint(c.Param("id")))
|
|
||||||
if player == nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var input UpdatePlayerInput
|
var input UpdatePlayerInput
|
||||||
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()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if player.Name != input.Name {
|
res := models.DB.Model(&models.Player{}).
|
||||||
if err := FindPlayerByName(&player, c).Error; err == nil {
|
Where("id = ?", utils.StringToUint(c.Param("id"))).
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Player with this name already exists!"})
|
Updates(map[string]interface{}{
|
||||||
return
|
"Name": input.Name,
|
||||||
}
|
"ClanID": input.ClanID,
|
||||||
|
})
|
||||||
|
if res.Error != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := "Updating player '" + player.Name + "' #" + strconv.FormatUint(uint64(player.ID), 10)
|
c.JSON(http.StatusOK, nil)
|
||||||
if player.Name != input.Name {
|
|
||||||
msg += " (new: '" + input.Name + "')"
|
_, err := fmt.Fprintf(utils.GinWriter, "Updated player '"+input.Name+"'\n")
|
||||||
}
|
|
||||||
msg += " with clan #" + utils.UintToString(player.ClanID)
|
|
||||||
if player.ClanID != input.ClanID {
|
|
||||||
msg += " (new: #" + utils.UintToString(input.ClanID) + ")"
|
|
||||||
}
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Model(&player).Updates(map[string]interface{}{
|
|
||||||
"Name": input.Name,
|
|
||||||
"ClanID": input.ClanID,
|
|
||||||
})
|
|
||||||
|
|
||||||
//UpdatePlayerTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, player)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePlayerByID DELETE /player/:id
|
// DeletePlayerByID DELETE /player/:id
|
||||||
@@ -157,8 +137,6 @@ func DeletePlayerByID(c *gin.Context) {
|
|||||||
|
|
||||||
models.DB.Delete(&player)
|
models.DB.Delete(&player)
|
||||||
|
|
||||||
//UpdatePlayerTimestamp()
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, true)
|
c.JSON(http.StatusOK, true)
|
||||||
|
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, "Deleted player '"+player.Name+"'\n")
|
_, err := fmt.Fprintf(utils.GinWriter, "Deleted player '"+player.Name+"'\n")
|
||||||
|
|||||||
35
controllers/user_controller.go
Normal file
35
controllers/user_controller.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"InfantrySkillCalculator/models"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
|
||||||
|
user := models.User{Username: username, Password: hashedPassword, Enabled: enabled}
|
||||||
|
models.DB.Create(&user)
|
||||||
|
|
||||||
|
err := models.DB.Model(&models.ActivationCode{}).
|
||||||
|
Where("code = ?", usedCode).
|
||||||
|
Update("Used", true).Error
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var bf2042 models.Game
|
||||||
|
models.DB.Where("tag = ?", "BF2042").First(&bf2042)
|
||||||
|
userSettings := models.UserSettings{
|
||||||
|
Username: username,
|
||||||
|
ActiveGameID: bf2042.ID,
|
||||||
|
SquadColors: true,
|
||||||
|
CalcMedian: false,
|
||||||
|
UseCache: true,
|
||||||
|
}
|
||||||
|
models.DB.Create(&userSettings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsUserEnabled(username string) bool {
|
||||||
|
var user models.User
|
||||||
|
models.DB.Where("username = ?", username).First(&user)
|
||||||
|
return user.Enabled
|
||||||
|
}
|
||||||
62
controllers/user_settings_controller.go
Normal file
62
controllers/user_settings_controller.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"InfantrySkillCalculator/models"
|
||||||
|
"InfantrySkillCalculator/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateSettingsInput struct {
|
||||||
|
ActiveGameID uint `json:"active_game_id"`
|
||||||
|
SquadColors bool `json:"squad_colors"`
|
||||||
|
CalcMedian bool `json:"calc_median"`
|
||||||
|
UseCache bool `json:"use_cache"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSettings GET /settings
|
||||||
|
func GetSettings(c *gin.Context) {
|
||||||
|
var settings models.UserSettings
|
||||||
|
|
||||||
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
|
var username string
|
||||||
|
username, _ = session.Values["username"].(string)
|
||||||
|
|
||||||
|
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sanitizedSettings := map[string]interface{}{
|
||||||
|
"active_game_id": settings.ActiveGameID,
|
||||||
|
"squad_colors": settings.SquadColors,
|
||||||
|
"calc_median": settings.CalcMedian,
|
||||||
|
"use_cache": settings.UseCache,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, sanitizedSettings)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateSettings PATCH /settings
|
||||||
|
func UpdateSettings(c *gin.Context) {
|
||||||
|
var settings models.UserSettings
|
||||||
|
|
||||||
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
|
var username string
|
||||||
|
username, _ = session.Values["username"].(string)
|
||||||
|
|
||||||
|
if err := models.DB.Where("username = ?", username).First(&settings).Error; err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No settings available!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var input UpdateSettingsInput
|
||||||
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
models.DB.Model(&settings).Updates(input)
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, nil)
|
||||||
|
}
|
||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module InfrantrySkillCalculator
|
module InfantrySkillCalculator
|
||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
|
|||||||
23
main.go
23
main.go
@@ -1,21 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/controllers"
|
"InfantrySkillCalculator/controllers"
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/models"
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
_ "github.com/gorilla/sessions"
|
_ "github.com/gorilla/sessions"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var store = sessions.NewCookieStore([]byte("f0q0qew0!)§(ds9713lsda231"))
|
|
||||||
|
|
||||||
const LoginSessionName = "session"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//gin.SetMode(gin.ReleaseMode) // uncomment upon release
|
//gin.SetMode(gin.ReleaseMode) // uncomment upon release
|
||||||
|
|
||||||
@@ -33,7 +28,11 @@ func main() {
|
|||||||
f, _ := os.OpenFile("isc_rest.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
f, _ := os.OpenFile("isc_rest.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
|
||||||
utils.GinWriter = io.MultiWriter(f, os.Stdout)
|
utils.GinWriter = io.MultiWriter(f, os.Stdout)
|
||||||
router.Use(
|
router.Use(
|
||||||
gin.LoggerWithWriter(utils.GinWriter, "/update"),
|
gin.LoggerWithWriter(utils.GinWriter, "/static"),
|
||||||
|
gin.Recovery(),
|
||||||
|
)
|
||||||
|
protected.Use(
|
||||||
|
gin.LoggerWithWriter(utils.GinWriter),
|
||||||
gin.Recovery(),
|
gin.Recovery(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,6 +61,12 @@ func main() {
|
|||||||
protected.PATCH("/player/:id", controllers.UpdatePlayerByID)
|
protected.PATCH("/player/:id", controllers.UpdatePlayerByID)
|
||||||
protected.DELETE("/player/:id", controllers.DeletePlayerByID)
|
protected.DELETE("/player/:id", controllers.DeletePlayerByID)
|
||||||
|
|
||||||
|
protected.GET("/game", controllers.GetGames)
|
||||||
|
protected.GET("/game_html", controllers.GetGamesHTML)
|
||||||
|
|
||||||
|
protected.GET("/settings", controllers.GetSettings)
|
||||||
|
protected.PATCH("/settings", controllers.UpdateSettings)
|
||||||
|
|
||||||
log.Println("Running on 8000...")
|
log.Println("Running on 8000...")
|
||||||
log.Fatal(router.Run(":8000"))
|
log.Fatal(router.Run(":8000"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ type Game struct {
|
|||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Tag string `json:"tag" binding:"required"`
|
Tag string `json:"tag" binding:"required"`
|
||||||
Settings []GameSetting `json:"settings" gorm:"foreignKey:GameID"`
|
Settings []GameSetting `json:"settings" gorm:"foreignKey:ID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameSetting struct {
|
type GameSetting struct {
|
||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
GameID uint `json:"game_id"`
|
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
WeaponCategory string `json:"weapon_category"`
|
WeaponCategory string `json:"weapon_category"`
|
||||||
Value float64 `json:"value" binding:"required"`
|
Value float64 `json:"value" binding:"required"`
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/utils"
|
"InfantrySkillCalculator/utils"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
@@ -51,11 +51,23 @@ func ConnectDatabase() {
|
|||||||
err = database.AutoMigrate(&Game{})
|
err = database.AutoMigrate(&Game{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
} else {
|
||||||
|
var game Game
|
||||||
|
if err := database.First(&game).Error; err != nil {
|
||||||
|
database.Create(&Game{Name: "Battlefield V", Tag: "BFV"})
|
||||||
|
database.Create(&Game{Name: "Battlefield 2042", Tag: "BF2042"})
|
||||||
|
log.Println("Created first games")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = database.AutoMigrate(&MetricSetting{})
|
err = database.AutoMigrate(&MetricSetting{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
err = database.AutoMigrate(&UserSettings{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
DB = database
|
DB = database
|
||||||
}
|
}
|
||||||
|
|||||||
12
models/user_settings.go
Normal file
12
models/user_settings.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type UserSettings struct {
|
||||||
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
User User `json:"user" gorm:"foreignKey:Username;references:Username"`
|
||||||
|
ActiveGameID uint `json:"active_game_id"`
|
||||||
|
ActiveGame Game `json:"active_game" gorm:"foreignKey:ID;references:ActiveGameID"`
|
||||||
|
SquadColors bool `json:"squad_colors" default:"1"`
|
||||||
|
CalcMedian bool `json:"calc_median" default:"0"`
|
||||||
|
UseCache bool `json:"use_cache" default:"1"`
|
||||||
|
}
|
||||||
25
pages.go
25
pages.go
@@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"InfrantrySkillCalculator/models"
|
"InfantrySkillCalculator/controllers"
|
||||||
|
"InfantrySkillCalculator/models"
|
||||||
|
"InfantrySkillCalculator/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
@@ -44,7 +46,7 @@ func mainPage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loginPage(c *gin.Context) {
|
func loginPage(c *gin.Context) {
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
|
|
||||||
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
||||||
c.Redirect(http.StatusFound, "/")
|
c.Redirect(http.StatusFound, "/")
|
||||||
@@ -71,7 +73,7 @@ func loginPost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
session.Values["authenticated"] = true
|
session.Values["authenticated"] = true
|
||||||
session.Values["username"] = username
|
session.Values["username"] = username
|
||||||
err := session.Save(c.Request, c.Writer)
|
err := session.Save(c.Request, c.Writer)
|
||||||
@@ -85,7 +87,7 @@ func loginPost(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func logout(c *gin.Context) {
|
func logout(c *gin.Context) {
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
session.Values["authenticated"] = false
|
session.Values["authenticated"] = false
|
||||||
err := session.Save(c.Request, c.Writer)
|
err := session.Save(c.Request, c.Writer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -97,7 +99,7 @@ func logout(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func registerPage(c *gin.Context) {
|
func registerPage(c *gin.Context) {
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
|
|
||||||
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
||||||
c.Redirect(http.StatusFound, "/")
|
c.Redirect(http.StatusFound, "/")
|
||||||
@@ -125,13 +127,18 @@ func registerPost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := models.User{Username: username, Password: password, Enabled: true}
|
hashedPassword, err := hashPassword(password)
|
||||||
models.DB.Create(&user)
|
if err != nil {
|
||||||
|
c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Fehler beim Registrieren!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
session, _ := store.Get(c.Request, LoginSessionName)
|
controllers.CreateUser(username, hashedPassword, true, code)
|
||||||
|
|
||||||
|
session, _ := utils.Store.Get(c.Request, utils.LoginSessionName)
|
||||||
session.Values["authenticated"] = true
|
session.Values["authenticated"] = true
|
||||||
session.Values["username"] = username
|
session.Values["username"] = username
|
||||||
err := session.Save(c.Request, c.Writer)
|
err = session.Save(c.Request, c.Writer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, nil)
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
const submitButton = addPlayerModal.querySelector('button[name="submit"]');
|
const submitButton = addPlayerModal.querySelector('button[name="submit"]');
|
||||||
const playerName = addPlayerModal.querySelector('#playerName');
|
const playerName = addPlayerModal.querySelector('#playerName');
|
||||||
const clanName = addPlayerModal.querySelector('#playerClanName');
|
const clanName = addPlayerModal.querySelector('#playerClanName');
|
||||||
const errorDiv = editPlayerModal.querySelector('.error-message');
|
const errorDiv = addPlayerModal.querySelector('.error-message');
|
||||||
const homeClanListIndex = document.getElementById('home-clan').selectedIndex;
|
const homeClanListIndex = document.getElementById('home-clan').selectedIndex;
|
||||||
const oppClanListIndex = document.getElementById('opponent-clan').selectedIndex;
|
const oppClanListIndex = document.getElementById('opponent-clan').selectedIndex;
|
||||||
|
|
||||||
@@ -67,6 +67,12 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Hinzufügen fehlgeschlagen!\nSpielername existiert möglichweise bereits.');
|
||||||
|
}
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const sameClan = homeClanListIndex === oppClanListIndex;
|
const sameClan = homeClanListIndex === oppClanListIndex;
|
||||||
if (playerList.id === 'home-player-list' || sameClan)
|
if (playerList.id === 'home-player-list' || sameClan)
|
||||||
|
|||||||
@@ -64,6 +64,12 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Änderung fehlgeschlagen!\nSpielername existiert möglichweise bereits.');
|
||||||
|
}
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const sameClan = homeClanListIndex === oppClanListIndex;
|
const sameClan = homeClanListIndex === oppClanListIndex;
|
||||||
if (playerList.id === 'home-player-list' || sameClan)
|
if (playerList.id === 'home-player-list' || sameClan)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
<label class="col-form-label col-form-label-lg" for="games">Aktuelles Spiel:</label>
|
<label class="col-form-label col-form-label-lg" for="games">Aktuelles Spiel:</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<select class="form-select form-select-lg" id="games">
|
<select class="form-select form-select-lg" id="games" hx-get="/game_html" hx-target="#games" hx-trigger="load">
|
||||||
<!-- fill dynamically with available games -->
|
<!-- fill dynamically with available games -->
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
@@ -41,7 +41,79 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script lang="javascript">
|
<script lang="javascript">
|
||||||
// TBD
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const settingsModal = document.getElementById('settingsModal');
|
||||||
|
const settingsModalBS = new bootstrap.Modal('#settingsModal');
|
||||||
|
const submitButton = settingsModal.querySelector('button[name="submit"]');
|
||||||
|
let submitSettingsHandler = null;
|
||||||
|
|
||||||
|
const games = document.getElementById('games');
|
||||||
|
const squadColors = document.getElementById('settingsSquadColors');
|
||||||
|
const calcMedian = document.getElementById('settingsCalcMedian');
|
||||||
|
const useCache = document.getElementById('settingsUseCache');
|
||||||
|
|
||||||
|
function createSubmitSettingsHandler() {
|
||||||
|
return function submitSettingsHandler(e) {
|
||||||
|
let activeGameId = games.options[games.selectedIndex].value;
|
||||||
|
if (activeGameId === '') {
|
||||||
|
alert('Bitte wähle ein Spiel aus.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activeGameId = parseInt(activeGameId);
|
||||||
|
|
||||||
|
fetch("/settings", {
|
||||||
|
method: "PATCH",
|
||||||
|
body: JSON.stringify({
|
||||||
|
active_game_id: activeGameId,
|
||||||
|
squad_colors: squadColors.checked,
|
||||||
|
calc_median: calcMedian.checked,
|
||||||
|
use_cache: useCache.checked
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Server error');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
settingsModalBS.hide();
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
alert('Fehler beim Speichern der Einstellungen: ' + error.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (settingsModal) {
|
||||||
|
settingsModal.addEventListener('show.bs.modal', event => {
|
||||||
|
submitSettingsHandler = createSubmitSettingsHandler();
|
||||||
|
submitButton.addEventListener('click', submitSettingsHandler);
|
||||||
|
|
||||||
|
fetch("/settings", {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
console.log(result);
|
||||||
|
games.selectedIndex = games.querySelector(`option[value="${result['active_game_id']}"]`).index;
|
||||||
|
squadColors.checked = result['squad_colors'];
|
||||||
|
calcMedian.value = result['calc_median'];
|
||||||
|
useCache.value = result['use_cache'];
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
alert('Fehler beim Laden der Einstellungen: ' + error.message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
7
utils/session.go
Normal file
7
utils/session.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "github.com/gorilla/sessions"
|
||||||
|
|
||||||
|
var Store = sessions.NewCookieStore([]byte("f0q0qew0!)§(ds9713lsda231"))
|
||||||
|
|
||||||
|
const LoginSessionName = "session"
|
||||||
Reference in New Issue
Block a user