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 { type ServeYaml struct {
DataStorage string `yaml:"dataStorage"` DataStorage string `yaml:"dataStorage"`
TemplateStorage string `yaml:"templateStorage"`
Domains []string `yaml:"domains"` Domains []string `yaml:"domains"`
RangeSupported bool `yaml:"rangeSupported"` RangeSupported bool `yaml:"rangeSupported"`
EnableGoInfoPage bool `yaml:"enableGoInfoPage"` EnableGoInfoPage bool `yaml:"enableGoInfoPage"`
CacheSettings CacheSettingsYaml `yaml:"cacheSettings"` CacheSettings CacheSettingsYaml `yaml:"cacheSettings"`
PageSettings []PageYaml `yaml:"pageSettings"`
YmlDataFallback bool `yaml:"ymlDataFallback"`
} }
func (sy ServeYaml) GetDomainString() string { func (sy ServeYaml) GetDomainString() string {
@ -39,3 +42,20 @@ func (sy ServeYaml) GetDataStoragePath() string {
return sy.DataStorage 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> </tr>
{{ end }} {{ end }}
{{ end }} {{ end }}
</table>
</p>
<p>
<table>
{{ if .Data.AllowDisplayState }} {{ if .Data.AllowDisplayState }}
<tr> <tr>
<th>Server State</th> <th>Server State</th>
@ -145,6 +149,10 @@
</tr> </tr>
{{ end }} {{ end }}
{{ end }} {{ end }}
</table>
</p>
<p>
<table>
{{ if and .Data.AllowPlayerCountDisplay (and .Queried.PlayerCount .Queried.MaxPlayers) }} {{ if and .Data.AllowPlayerCountDisplay (and .Queried.PlayerCount .Queried.MaxPlayers) }}
<tr> <tr>
<th>Player Count</th> <th>Player Count</th>
@ -206,28 +214,18 @@
<p> <p>
<table> <table>
<tr> <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>
<tr> <tr>
<td class="centered"><a href="{{ .ToggleQuery "players" }}" title="Collapse Player List.">Collapse Player List</a></td> <td class="centered"><a href="{{ .ToggleQuery "players" }}" title="Collapse Player List.">Collapse Player List</a></td>
</tr> </tr>
{{ if gt (len (.Queried.GetPlayers .Data.ShowAnonymousPlayers)) 0 }} {{ if gt (len (.Queried.GetPlayers .Data.ShowAnonymousPlayers)) 0 }}
{{ if .Data.ShowAnonymousPlayers }} {{ range .Queried.GetPlayers .Data.ShowAnonymousPlayers }}
{{ range .Queried.Players }}
<tr> <tr>
<td>{{ . }}</td> <td>{{ . }}</td>
</tr> </tr>
{{ end }} {{ end }}
{{ else }} {{ else }}
{{ range .Queried.Players }}
{{ if ne . "Anonymous Player" }}
<tr>
<td>{{ . }}</td>
</tr>
{{ end }}
{{ end }}
{{ end }}
{{ else }}
<tr> <tr>
<td>No Detectable Players.</td> <td>No Detectable Players.</td>
</tr> </tr>

View File

@ -44,9 +44,9 @@ func NewPageHandler(config conf.ServeYaml) *PageHandler {
CacheSettings: config.CacheSettings, CacheSettings: config.CacheSettings,
} }
if config.EnableGoInfoPage { 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 { } 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 return toReturn
} }

View File

@ -1,18 +1,26 @@
package pageHandler 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 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 { if providers == nil {
providers = make(map[string]PageProvider) providers = make(map[string]PageProvider)
if pageHandler != nil { if pageHandler != nil {
infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates) infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates)
providers[infoPage.GetPath()] = infoPage //Go Information Page providers[infoPage.GetPath()] = infoPage //Go Information Page
} }
indexPage := index.NewPage(dataStorage, cacheTemplates) for _, cpg := range pageSettings { //Register pages
providers[indexPage.GetPath()] = indexPage if strings.EqualFold(cpg.PageName, index.PageName) {
indexPage := index.NewPage(dataStorage, cacheTemplates, templateStorage, cpg.GetPagePath(), ymlDataFallback)
providers[indexPage.GetPath()] = indexPage
}
}
} }
return providers return providers
} }

View File

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