Add clan-purge and player-purge. Optimized performance with template-init.

This commit is contained in:
MaxJa4
2024-01-20 17:14:47 +01:00
parent e5d13f2270
commit 9e3e2723d3
6 changed files with 94 additions and 59 deletions

View File

@@ -4,13 +4,14 @@
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="6">
<list size="7">
<item index="0" class="java.lang.String" itemvalue="hx-get" />
<item index="1" class="java.lang.String" itemvalue="hx-target" />
<item index="2" class="java.lang.String" itemvalue="hx-vals" />
<item index="3" class="java.lang.String" itemvalue="hx-post" />
<item index="4" class="java.lang.String" itemvalue="hx-trigger" />
<item index="5" class="java.lang.String" itemvalue="hx-swap" />
<item index="6" class="java.lang.String" itemvalue="hx-delete" />
</list>
</value>
</option>

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"log"
"net/http"
)
@@ -131,6 +132,20 @@ func DeleteClanByID(c *gin.Context) {
}
}
// DeleteAllClans DELETE /admin/clan
func DeleteAllClans(c *gin.Context) {
var clans []models.Clan
if err := models.DB.
Session(&gorm.Session{AllowGlobalUpdate: true}).
Clauses(clause.Returning{}).
Delete(&clans).Error; err != nil {
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
return
}
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(clans)))+" clans!")
}
func FindClanByName(out interface{}, name string) *gorm.DB {
return models.DB.Where("name = ?", name).First(out)
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"log"
"net/http"
"os"
@@ -145,6 +146,20 @@ func DeletePlayerByID(c *gin.Context) {
}
}
// DeleteAllPlayers DELETE /admin/player
func DeleteAllPlayers(c *gin.Context) {
var players []models.Player
if err := models.DB.
Session(&gorm.Session{AllowGlobalUpdate: true}).
Clauses(clause.Returning{}).
Delete(&players).Error; err != nil {
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
return
}
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(players)))+" players!")
}
func FindPlayerByID(id uint) *models.Player {
var res models.Player
if err := models.DB.Where("id = ?", id).First(&res).Error; err != nil {

48
main.go
View File

@@ -6,11 +6,55 @@ import (
"InfantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
_ "github.com/gorilla/sessions"
"html/template"
"io"
"log"
"os"
)
var mainPageTemplates *template.Template
var loginPageTemplates *template.Template
var registerPageTemplates *template.Template
func init() {
var err error
mainPageTemplates, err = template.ParseFiles(
"./templates/index.html",
"./templates/components/home_clan_bar.html",
"./templates/components/opp_clan_bar.html",
"./templates/components/home_player_list.html",
"./templates/components/opp_player_list.html",
"./templates/components/bottom_controls.html",
"./templates/modals/delete_clan.html",
"./templates/modals/add_clan.html",
"./templates/modals/edit_clan.html",
"./templates/modals/add_player.html",
"./templates/modals/delete_player.html",
"./templates/modals/edit_player.html",
"./templates/modals/settings.html",
"./templates/components/header.html",
)
if err != nil {
log.Fatal(err)
}
loginPageTemplates, err = template.ParseFiles(
"./templates/login.html",
"./templates/components/header.html",
)
if err != nil {
log.Fatal(err)
}
registerPageTemplates, err = template.ParseFiles(
"./templates/register.html",
"./templates/components/header.html",
)
if err != nil {
log.Fatal(err)
}
}
func main() {
//gin.SetMode(gin.ReleaseMode) // uncomment upon release
@@ -73,7 +117,9 @@ func main() {
protected.GET("/settings", controllers.GetSettings)
protected.PATCH("/settings", controllers.UpdateSettings)
admin.GET("/clear_cache", controllers.DeleteAllCaches)
admin.DELETE("/clear_cache", controllers.DeleteAllCaches)
admin.DELETE("/purge_players", controllers.DeleteAllPlayers)
admin.DELETE("/purge_clans", controllers.DeleteAllClans)
admin.POST("/create_code", controllers.CreateCode)
log.Println("Running on 8000...")

View File

@@ -4,38 +4,16 @@ import (
"InfantrySkillCalculator/controllers"
"InfantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
)
func mainPage(c *gin.Context) {
files := []string{
"./templates/index.html",
"./templates/components/home_clan_bar.html",
"./templates/components/opp_clan_bar.html",
"./templates/components/home_player_list.html",
"./templates/components/opp_player_list.html",
"./templates/components/bottom_controls.html",
"./templates/modals/delete_clan.html",
"./templates/modals/add_clan.html",
"./templates/modals/edit_clan.html",
"./templates/modals/add_player.html",
"./templates/modals/delete_player.html",
"./templates/modals/edit_player.html",
"./templates/modals/settings.html",
"./templates/components/header.html",
}
tmpl, err := template.ParseFiles(files...)
if err != nil {
log.Fatal(err)
}
data := map[string]interface{}{
"isAdmin": isUserAdmin(c),
}
err = tmpl.Execute(c.Writer, data)
err := mainPageTemplates.Execute(c.Writer, data)
if err != nil {
log.Fatal(err)
}
@@ -49,12 +27,7 @@ func loginPage(c *gin.Context) {
return
}
tmpl, err := template.ParseFiles([]string{"./templates/login.html", "./templates/components/header.html"}...)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(c.Writer, nil)
err := loginPageTemplates.Execute(c.Writer, nil)
if err != nil {
log.Fatal(err)
}
@@ -102,12 +75,7 @@ func registerPage(c *gin.Context) {
return
}
tmpl, err := template.ParseFiles([]string{"./templates/register.html", "./templates/components/header.html"}...)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(c.Writer, nil)
err := registerPageTemplates.Execute(c.Writer, nil)
if err != nil {
log.Fatal(err)
}

View File

@@ -17,54 +17,40 @@
<ul class="dropdown-menu">
<li><h6 class="dropdown-header">ACCOUNTS</h6></li>
<li>
<button class="dropdown-item fs-5 text-info-emphasis fst-italic"
<button class="dropdown-item fs-5 text-info-emphasis"
hx-post="/admin/create_code" hx-trigger="confirmed" hx-swap="none"
onclick="createCodeDialog(this)">
Create Code
</button>
</li>
<li>
<button class="dropdown-item fs-5 text-info-emphasis fst-italic"
hx-get="/admin/reset_password" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
Reset Password
</button>
</li>
<li>
<button class="dropdown-item fs-5 text-info-emphasis fst-italic"
hx-get="/admin/disable_account" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
Disable Account
</button>
</li>
<li><hr class="dropdown-divider"></li>
<li><h6 class="dropdown-header">TOOLS</h6></li>
<li>
<button class="dropdown-item fs-5 text-warning-emphasis"
hx-get="/admin/clear_cache" hx-trigger="confirmed" hx-swap="none"
hx-delete="/admin/clear_cache" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
Clear Cache
</button>
</li>
<li>
<button class="dropdown-item fs-5 text-warning-emphasis fst-italic"
<button class="dropdown-item fs-5 text-warning-emphasis visually-hidden"
hx-get="/admin/update_cache" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
onclick="alert('Not implemented yet')">
Update Cache
</button>
</li>
<li><hr class="dropdown-divider"></li>
<li><h6 class="dropdown-header">DANGER ZONE</h6></li>
<li>
<button class="dropdown-item fs-5 text-danger-emphasis fst-italic"
hx-get="/admin/purge_players" hx-trigger="confirmed" hx-swap="none"
<button class="dropdown-item fs-5 text-danger-emphasis"
hx-delete="/admin/purge_players" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
Purge Players
</button>
</li>
<li>
<button class="dropdown-item fs-5 text-danger-emphasis fst-italic"
hx-get="/admin/purge_cache" hx-trigger="confirmed" hx-swap="none"
<button class="dropdown-item fs-5 text-danger-emphasis"
hx-delete="/admin/purge_clans" hx-trigger="confirmed" hx-swap="none"
onclick="confirmAndTrigger(this)">
Purge Clans
</button>
@@ -95,6 +81,10 @@
title: 'Action executed',
text: event.detail.xhr.response,
icon: event.detail.xhr.status === 200 ? 'success' : 'error'
}).then(() => {
if (event.detail.requestConfig.verb === "delete") {
location.reload();
}
});
}
});