Upgrade internals for page versitility.
Some checks are pending
ci/woodpecker/push/build Pipeline is pending

Fix a bug in goinfo
This commit is contained in:
Captain ALM 2023-09-16 13:50:06 +01:00
parent f60e04ada4
commit 4da16c32ae
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
8 changed files with 94 additions and 22 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

@ -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

@ -150,6 +150,10 @@
<th>Memory Page Size</th> <th>Memory Page Size</th>
<td>{{ .PageSize }}</td> <td>{{ .PageSize }}</td>
</tr> </tr>
<tr>
<th>System Time</th>
<td>{{ .CurrentTime }}</td>
</tr>
</table> </table>
</p> </p>
<p> <p>

View File

@ -45,6 +45,7 @@ func (gipg *goInfoPage) GetCacheIDExtension(urlParameters url.Values) string {
type goInfoTemplateMarshal struct { type goInfoTemplateMarshal struct {
FullOutput bool FullOutput bool
CurrentTime time.Time
RegisteredPages []string RegisteredPages []string
CachedPages []string CachedPages []string
ProcessID int ProcessID int
@ -98,6 +99,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin
theBuffer := &io.BufferedWriter{} theBuffer := &io.BufferedWriter{}
err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{ err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{
FullOutput: urlParameters.Has("full"), FullOutput: urlParameters.Has("full"),
CurrentTime: time.Now(),
RegisteredPages: regPages, RegisteredPages: regPages,
CachedPages: cacPages, CachedPages: cacPages,
ProcessID: os.Getpid(), ProcessID: os.Getpid(),

View File

@ -43,9 +43,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
} }
@ -286,6 +286,9 @@ func (ph *PageHandler) GetRegisteredPages() []string {
} }
func (ph *PageHandler) GetCachedPages() []string { func (ph *PageHandler) GetCachedPages() []string {
if ph.pageContentsCacheRWMutex == nil {
return make([]string, 0)
}
ph.pageContentsCacheRWMutex.RLock() ph.pageContentsCacheRWMutex.RLock()
defer ph.pageContentsCacheRWMutex.RUnlock() defer ph.pageContentsCacheRWMutex.RUnlock()
pages := make([]string, len(ph.PageContentsCache)) pages := make([]string, len(ph.PageContentsCache))
@ -298,6 +301,9 @@ func (ph *PageHandler) GetCachedPages() []string {
} }
func (ph *PageHandler) GetNumberOfCachedPages() int { func (ph *PageHandler) GetNumberOfCachedPages() int {
if ph.pageContentsCacheRWMutex == nil {
return 0
}
ph.pageContentsCacheRWMutex.RLock() ph.pageContentsCacheRWMutex.RLock()
defer ph.pageContentsCacheRWMutex.RUnlock() defer ph.pageContentsCacheRWMutex.RUnlock()
return len(ph.PageContentsCache) return len(ph.PageContentsCache)

View File

@ -1,18 +1,26 @@
package pageHandler package pageHandler
import "golang.captainalm.com/cityuni-webserver/pageHandler/pages/index" import (
"golang.captainalm.com/cityuni-webserver/conf"
"golang.captainalm.com/cityuni-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"
"golang.captainalm.com/cityuni-webserver/utils/io" "golang.captainalm.com/cityuni-webserver/utils/io"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"html/template" "html/template"
@ -12,10 +13,10 @@ import (
"time" "time"
) )
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 {
@ -23,7 +24,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,
} }
@ -31,7 +35,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
@ -41,7 +48,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 {
@ -129,16 +136,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
} }
@ -161,13 +164,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)
@ -189,6 +197,7 @@ func (p *Page) getPageData() (*DataYaml, error) {
p.StoredData = dataYaml p.StoredData = dataYaml
} }
return dataYaml, nil return dataYaml, nil
} else { } else {
return p.StoredData, nil return p.StoredData, nil
} }