Fix MC Caching sync.
Some checks are pending
ci/woodpecker/push/build Pipeline is pending

Add timestamp.
Fix up parametrisation utilities.
This commit is contained in:
Captain ALM 2023-08-13 23:01:29 +01:00
parent 8c929c43f9
commit 761cc5f2e8
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
4 changed files with 50 additions and 23 deletions

View File

@ -4,9 +4,13 @@ listen:
webMethod: "http"
identify: true
serve:
dataStorage: ""
domains: []
rangeSupported: true
enableGoInfoPage: true
cacheSettings:
enableTemplateCaching: false
enableTemplateCachePurge: false
enableContentsCaching: true
enableContentsCachePurge: true
maxAge: 3600

View File

@ -26,6 +26,7 @@ func NewPage(dataStore string, cacheTemplates bool) *Page {
DataStore: dataStore,
StoredDataMutex: sdm,
PageTemplateMutex: ptm,
CacheMCMutex: &sync.Mutex{},
}
return pageToReturn
}
@ -40,6 +41,7 @@ type Page struct {
LastModifiedTemplate time.Time
CachedMC *MC
CollectedMCExpiry time.Time
CacheMCMutex *sync.Mutex
}
func (p *Page) GetPath() string {
@ -55,17 +57,6 @@ func (p *Page) GetLastModified() time.Time {
}
func (p *Page) GetCacheIDExtension(urlParameters url.Values) string {
toReturn := p.getNonThemedCleanQuery(urlParameters)
if toReturn != "" {
toReturn += "&"
}
if urlParameters.Has("light") {
toReturn += "light"
}
return strings.TrimRight(toReturn, "&")
}
func (p *Page) getNonThemedCleanQuery(urlParameters url.Values) string {
toReturn := ""
if urlParameters.Has("players") {
toReturn += "players&"
@ -74,7 +65,10 @@ func (p *Page) getNonThemedCleanQuery(urlParameters url.Values) string {
toReturn += "mods&"
}
if urlParameters.Has("extended") {
toReturn += "extended"
toReturn += "extended&"
}
if urlParameters.Has("dark") {
toReturn += "dark"
}
return strings.TrimRight(toReturn, "&")
}
@ -90,14 +84,27 @@ func (p *Page) GetContents(urlParameters url.Values) (contentType string, conten
}
theMarshal := &Marshal{
Data: *theData,
Light: urlParameters.Has("light"),
Dark: urlParameters.Has("dark"),
PlayersShown: urlParameters.Has("players"),
ModsShown: urlParameters.Has("mods"),
ExtendedShown: urlParameters.Has("extended"),
Parameters: template.URL(p.getNonThemedCleanQuery(urlParameters)),
Online: true,
}
theMarshal.Queried = p.GetMC(theData, theMarshal)
theBuffer := &io.BufferedWriter{}
err = theTemplate.ExecuteTemplate(theBuffer, templateName, theMarshal)
if err != nil {
return "text/plain", []byte("Cannot Get Page.\r\n" + err.Error()), false
}
return "text/html", theBuffer.Data, true
}
func (p *Page) GetMC(theData *DataYaml, theMarshal *Marshal) MC {
var theMC MC
var err error
defer p.CacheMCMutex.Unlock()
p.CacheMCMutex.Lock()
if time.Now().After(p.CollectedMCExpiry) || time.Now().Equal(p.CollectedMCExpiry) {
theMC, err = theMarshal.NewMC()
if err == nil {
@ -115,13 +122,7 @@ func (p *Page) GetContents(urlParameters url.Values) (contentType string, conten
theMC = *p.CachedMC
}
}
theMarshal.Queried = theMC
theBuffer := &io.BufferedWriter{}
err = theTemplate.ExecuteTemplate(theBuffer, templateName, theMarshal)
if err != nil {
return "text/plain", []byte("Cannot Get Page.\r\n" + err.Error()), false
}
return "text/html", theBuffer.Data, true
return theMC
}
func (p *Page) PurgeTemplate() {

View File

@ -2,9 +2,11 @@ package index
import (
"html/template"
"time"
)
type MC struct {
Timestamp time.Time
Version *string
ProtocolVersion *int64
Address string

View File

@ -7,6 +7,7 @@ import (
"github.com/mcstatus-io/mcutil/response"
"html/template"
"strings"
"time"
)
type Marshal struct {
@ -15,8 +16,7 @@ type Marshal struct {
PlayersShown bool
ModsShown bool
ExtendedShown bool
Parameters template.URL
Light bool
Dark bool
Online bool
}
@ -39,6 +39,7 @@ func (m Marshal) NewMC() (MC, error) {
return MC{}, err
}
return MC{
Timestamp: time.Now(),
Version: &r.Version.NameClean,
ProtocolVersion: &r.Version.Protocol,
Address: m.Data.MCAddress,
@ -67,6 +68,7 @@ func (m Marshal) NewMC() (MC, error) {
return MC{}, err
}
return MC{
Timestamp: time.Now(),
Version: &r.Version.NameClean,
ProtocolVersion: &r.Version.Protocol,
Address: m.Data.MCAddress,
@ -95,6 +97,7 @@ func (m Marshal) NewMC() (MC, error) {
return MC{}, err
}
return MC{
Timestamp: time.Now(),
Version: r.Version,
ProtocolVersion: r.ProtocolVersion,
Address: m.Data.MCAddress,
@ -200,3 +203,20 @@ func (m Marshal) CollectIPv4Port(port *uint16) uint16 {
}
return *port
}
func (m *Marshal) ToggleQuery(option string) string {
toReturn := ""
if (!m.Dark && option == "dark") || (m.Dark && option != "dark") {
toReturn += "dark&"
}
if (!m.ModsShown && option == "mods") || (m.ModsShown && option != "mods") {
toReturn += "mods&"
}
if (!m.ExtendedShown && option == "extended") || (m.ExtendedShown && option != "extended") {
toReturn += "extended&"
}
if (!m.PlayersShown && option == "players") || (m.PlayersShown && option != "players") {
toReturn += "players"
}
return strings.TrimRight(toReturn, "&")
}