Make server more page versatile.
Some checks are pending
ci/woodpecker/push/build Pipeline is pending

Update page layout.
This commit is contained in:
Captain ALM 2023-09-09 15:57:11 +01:00
parent 487626d1b1
commit a159096c89
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
6 changed files with 87 additions and 34 deletions

19
conf/page.go Normal file
View File

@ -0,0 +1,19 @@
package conf
import "strings"
type PageYaml struct {
PageName string `yaml:"pageName"`
PagePath string `yaml:"pagePath"`
}
func (py PageYaml) GetPagePath() string {
toReturn := py.PagePath
if !strings.HasSuffix(toReturn, ".go") {
toReturn += ".go"
}
if !strings.HasPrefix(toReturn, "/") {
toReturn = "/" + toReturn
}
return toReturn
}

View File

@ -9,10 +9,13 @@ import (
type ServeYaml struct {
DataStorage string `yaml:"dataStorage"`
TemplateStorage string `yaml:"templateStorage"`
Domains []string `yaml:"domains"`
RangeSupported bool `yaml:"rangeSupported"`
EnableGoInfoPage bool `yaml:"enableGoInfoPage"`
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`
PageSettings []PageYaml `yaml:"pageSettings"`
YmlDataFallback bool `yaml:"ymlDataFallback"`
}
func (sy ServeYaml) GetDomainString() string {
@ -39,3 +42,20 @@ func (sy ServeYaml) GetDataStoragePath() string {
return sy.DataStorage
}
}
func (sy ServeYaml) GetTemplateStoragePath() string {
if sy.TemplateStorage == "" || !filepath.IsAbs(sy.TemplateStorage) {
wd, err := os.Getwd()
if err != nil {
return ""
} else {
if sy.TemplateStorage == "" {
return wd
} else {
return path.Join(wd, sy.TemplateStorage)
}
}
} else {
return sy.TemplateStorage
}
}

View File

@ -108,6 +108,10 @@
</tr>
{{ end }}
{{ end }}
</table>
</p>
<p>
<table>
{{ if .Data.AllowDisplayState }}
<tr>
<th>Server State</th>
@ -145,6 +149,10 @@
</tr>
{{ end }}
{{ end }}
</table>
</p>
<p>
<table>
{{ if and .Data.AllowPlayerCountDisplay (and .Queried.PlayerCount .Queried.MaxPlayers) }}
<tr>
<th>Player Count</th>
@ -206,28 +214,18 @@
<p>
<table>
<tr>
<th>Player List{{ if .Queried.PlayerCount }} ({{ .Queried.PlayerCount }}) {{ end }}</th>
<th>Player List{{ if .Queried.PlayerCount }} ({{ len (.Queried.GetPlayers .Data.ShowAnonymousPlayers) }}) {{ end }}</th>
</tr>
<tr>
<td class="centered"><a href="{{ .ToggleQuery "players" }}" title="Collapse Player List.">Collapse Player List</a></td>
</tr>
{{ if gt (len (.Queried.GetPlayers .Data.ShowAnonymousPlayers)) 0 }}
{{ if .Data.ShowAnonymousPlayers }}
{{ range .Queried.Players }}
{{ range .Queried.GetPlayers .Data.ShowAnonymousPlayers }}
<tr>
<td>{{ . }}</td>
</tr>
{{ end }}
{{ else }}
{{ range .Queried.Players }}
{{ if ne . "Anonymous Player" }}
<tr>
<td>{{ . }}</td>
</tr>
{{ end }}
{{ end }}
{{ end }}
{{ else }}
<tr>
<td>No Detectable Players.</td>
</tr>

View File

@ -44,9 +44,9 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
CacheSettings: config.CacheSettings,
}
if config.EnableGoInfoPage {
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage, toReturn)
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.GetDataStoragePath(), toReturn, config.GetTemplateStoragePath(), config.PageSettings, config.YmlDataFallback)
} else {
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.DataStorage, nil)
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.GetDataStoragePath(), nil, config.GetTemplateStoragePath(), config.PageSettings, config.YmlDataFallback)
}
return toReturn
}

View File

@ -1,18 +1,26 @@
package pageHandler
import "golang.captainalm.com/mc-webserver/pageHandler/pages/index"
import (
"golang.captainalm.com/mc-webserver/conf"
"golang.captainalm.com/mc-webserver/pageHandler/pages/index"
"strings"
)
var providers map[string]PageProvider
func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHandler) map[string]PageProvider {
func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHandler, templateStorage string, pageSettings []conf.PageYaml, ymlDataFallback bool) map[string]PageProvider {
if providers == nil {
providers = make(map[string]PageProvider)
if pageHandler != nil {
infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates)
providers[infoPage.GetPath()] = infoPage //Go Information Page
}
indexPage := index.NewPage(dataStorage, cacheTemplates)
providers[indexPage.GetPath()] = indexPage
for _, cpg := range pageSettings { //Register pages
if strings.EqualFold(cpg.PageName, index.PageName) {
indexPage := index.NewPage(dataStorage, cacheTemplates, templateStorage, cpg.GetPagePath(), ymlDataFallback)
providers[indexPage.GetPath()] = indexPage
}
}
}
return providers
}

View File

@ -1,6 +1,7 @@
package index
import (
"errors"
"html/template"
"net/url"
"os"
@ -13,10 +14,10 @@ import (
"gopkg.in/yaml.v3"
)
const PageName = "index"
const templateName = "index.go.html"
const yamlName = "index.go.yml"
func NewPage(dataStore string, cacheTemplates bool) *Page {
func NewPage(dataStore string, cacheTemplates bool, templateStore string, pagePath string, ymlDataFallback bool) *Page {
var ptm *sync.Mutex
var sdm *sync.Mutex
if cacheTemplates {
@ -24,7 +25,10 @@ func NewPage(dataStore string, cacheTemplates bool) *Page {
sdm = &sync.Mutex{}
}
pageToReturn := &Page{
DataStore: dataStore,
YMLDataFallback: ymlDataFallback,
PagePath: pagePath,
DataPath: path.Join(dataStore, pagePath),
TemplatePath: path.Join(templateStore, templateName),
StoredDataMutex: sdm,
PageTemplateMutex: ptm,
CacheMCMutex: &sync.Mutex{},
@ -33,7 +37,10 @@ func NewPage(dataStore string, cacheTemplates bool) *Page {
}
type Page struct {
DataStore string
YMLDataFallback bool
PagePath string
DataPath string
TemplatePath string
StoredDataMutex *sync.Mutex
StoredData *DataYaml
LastModifiedData time.Time
@ -47,7 +54,7 @@ type Page struct {
}
func (p *Page) GetPath() string {
return "/index.go"
return p.PagePath
}
func (p *Page) GetLastModified() time.Time {
@ -153,16 +160,12 @@ func (p *Page) getPageTemplate() (*template.Template, error) {
defer p.PageTemplateMutex.Unlock()
}
if p.PageTemplate == nil {
thePath := templateName
if p.DataStore != "" {
thePath = path.Join(p.DataStore, thePath)
}
stat, err := os.Stat(thePath)
stat, err := os.Stat(p.TemplatePath)
if err != nil {
return nil, err
}
p.LastModifiedTemplate = stat.ModTime()
loadedData, err := os.ReadFile(thePath)
loadedData, err := os.ReadFile(p.TemplatePath)
if err != nil {
return nil, err
}
@ -185,13 +188,18 @@ func (p *Page) getPageData() (*DataYaml, error) {
defer p.StoredDataMutex.Unlock()
}
if p.StoredData == nil {
thePath := yamlName
if p.DataStore != "" {
thePath = path.Join(p.DataStore, thePath)
}
thePath := p.DataPath
stat, err := os.Stat(thePath)
if err != nil {
return nil, err
if p.YMLDataFallback && errors.Is(err, os.ErrNotExist) {
thePath += ".yml"
stat, err = os.Stat(thePath)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
p.LastModifiedData = stat.ModTime()
fileHandle, err := os.Open(thePath)