Fix MC Caching sync.
ci/woodpecker/push/build Pipeline is pending Details

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" webMethod: "http"
identify: true identify: true
serve: serve:
dataStorage: ""
domains: []
rangeSupported: true rangeSupported: true
enableGoInfoPage: true enableGoInfoPage: true
cacheSettings: cacheSettings:
enableTemplateCaching: false
enableTemplateCachePurge: false
enableContentsCaching: true enableContentsCaching: true
enableContentsCachePurge: true enableContentsCachePurge: true
maxAge: 3600 maxAge: 3600

View File

@ -26,6 +26,7 @@ func NewPage(dataStore string, cacheTemplates bool) *Page {
DataStore: dataStore, DataStore: dataStore,
StoredDataMutex: sdm, StoredDataMutex: sdm,
PageTemplateMutex: ptm, PageTemplateMutex: ptm,
CacheMCMutex: &sync.Mutex{},
} }
return pageToReturn return pageToReturn
} }
@ -40,6 +41,7 @@ type Page struct {
LastModifiedTemplate time.Time LastModifiedTemplate time.Time
CachedMC *MC CachedMC *MC
CollectedMCExpiry time.Time CollectedMCExpiry time.Time
CacheMCMutex *sync.Mutex
} }
func (p *Page) GetPath() string { func (p *Page) GetPath() string {
@ -55,17 +57,6 @@ func (p *Page) GetLastModified() time.Time {
} }
func (p *Page) GetCacheIDExtension(urlParameters url.Values) string { 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 := "" toReturn := ""
if urlParameters.Has("players") { if urlParameters.Has("players") {
toReturn += "players&" toReturn += "players&"
@ -74,7 +65,10 @@ func (p *Page) getNonThemedCleanQuery(urlParameters url.Values) string {
toReturn += "mods&" toReturn += "mods&"
} }
if urlParameters.Has("extended") { if urlParameters.Has("extended") {
toReturn += "extended" toReturn += "extended&"
}
if urlParameters.Has("dark") {
toReturn += "dark"
} }
return strings.TrimRight(toReturn, "&") return strings.TrimRight(toReturn, "&")
} }
@ -90,14 +84,27 @@ func (p *Page) GetContents(urlParameters url.Values) (contentType string, conten
} }
theMarshal := &Marshal{ theMarshal := &Marshal{
Data: *theData, Data: *theData,
Light: urlParameters.Has("light"), Dark: urlParameters.Has("dark"),
PlayersShown: urlParameters.Has("players"), PlayersShown: urlParameters.Has("players"),
ModsShown: urlParameters.Has("mods"), ModsShown: urlParameters.Has("mods"),
ExtendedShown: urlParameters.Has("extended"), ExtendedShown: urlParameters.Has("extended"),
Parameters: template.URL(p.getNonThemedCleanQuery(urlParameters)),
Online: true, 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 theMC MC
var err error
defer p.CacheMCMutex.Unlock()
p.CacheMCMutex.Lock()
if time.Now().After(p.CollectedMCExpiry) || time.Now().Equal(p.CollectedMCExpiry) { if time.Now().After(p.CollectedMCExpiry) || time.Now().Equal(p.CollectedMCExpiry) {
theMC, err = theMarshal.NewMC() theMC, err = theMarshal.NewMC()
if err == nil { if err == nil {
@ -115,13 +122,7 @@ func (p *Page) GetContents(urlParameters url.Values) (contentType string, conten
theMC = *p.CachedMC theMC = *p.CachedMC
} }
} }
theMarshal.Queried = theMC return 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
} }
func (p *Page) PurgeTemplate() { func (p *Page) PurgeTemplate() {

View File

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

View File

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