diff --git a/config.example.yml b/config.example.yml index e49631e..813b282 100644 --- a/config.example.yml +++ b/config.example.yml @@ -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 diff --git a/pageHandler/pages/index/index-page.go b/pageHandler/pages/index/index-page.go index dc3e8ed..1554c51 100644 --- a/pageHandler/pages/index/index-page.go +++ b/pageHandler/pages/index/index-page.go @@ -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() { diff --git a/pageHandler/pages/index/mc.go b/pageHandler/pages/index/mc.go index 0543435..5aa307c 100644 --- a/pageHandler/pages/index/mc.go +++ b/pageHandler/pages/index/mc.go @@ -2,9 +2,11 @@ package index import ( "html/template" + "time" ) type MC struct { + Timestamp time.Time Version *string ProtocolVersion *int64 Address string diff --git a/pageHandler/pages/index/template-marshal.go b/pageHandler/pages/index/template-marshal.go index e368d4e..808e6eb 100644 --- a/pageHandler/pages/index/template-marshal.go +++ b/pageHandler/pages/index/template-marshal.go @@ -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, "&") +}