From 4da16c32ae52e5c0d21c792d4ee2ca9db1504c05 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 16 Sep 2023 13:50:06 +0100 Subject: [PATCH] Upgrade internals for page versitility. Fix a bug in goinfo --- conf/page.go | 19 +++++++++++++ conf/serve.go | 20 +++++++++++++ config.example.yml | 4 +++ goinfo.go.html | 4 +++ pageHandler/go-info-page.go | 2 ++ pageHandler/page-handler.go | 10 +++++-- pageHandler/pages.go | 16 ++++++++--- pageHandler/pages/index/index-page.go | 41 ++++++++++++++++----------- 8 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 conf/page.go diff --git a/conf/page.go b/conf/page.go new file mode 100644 index 0000000..1e0e001 --- /dev/null +++ b/conf/page.go @@ -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 +} diff --git a/conf/serve.go b/conf/serve.go index a034205..1d9c941 100644 --- a/conf/serve.go +++ b/conf/serve.go @@ -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 + } +} 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/goinfo.go.html b/goinfo.go.html index 740966a..e09f3a9 100644 --- a/goinfo.go.html +++ b/goinfo.go.html @@ -150,6 +150,10 @@ Memory Page Size {{ .PageSize }} + + System Time + {{ .CurrentTime }} +

diff --git a/pageHandler/go-info-page.go b/pageHandler/go-info-page.go index 4b665a4..922637a 100644 --- a/pageHandler/go-info-page.go +++ b/pageHandler/go-info-page.go @@ -45,6 +45,7 @@ func (gipg *goInfoPage) GetCacheIDExtension(urlParameters url.Values) string { type goInfoTemplateMarshal struct { FullOutput bool + CurrentTime time.Time RegisteredPages []string CachedPages []string ProcessID int @@ -98,6 +99,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin theBuffer := &io.BufferedWriter{} err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{ FullOutput: urlParameters.Has("full"), + CurrentTime: time.Now(), RegisteredPages: regPages, CachedPages: cacPages, ProcessID: os.Getpid(), diff --git a/pageHandler/page-handler.go b/pageHandler/page-handler.go index c222118..5ac117a 100644 --- a/pageHandler/page-handler.go +++ b/pageHandler/page-handler.go @@ -43,9 +43,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 } @@ -286,6 +286,9 @@ func (ph *PageHandler) GetRegisteredPages() []string { } func (ph *PageHandler) GetCachedPages() []string { + if ph.pageContentsCacheRWMutex == nil { + return make([]string, 0) + } ph.pageContentsCacheRWMutex.RLock() defer ph.pageContentsCacheRWMutex.RUnlock() pages := make([]string, len(ph.PageContentsCache)) @@ -298,6 +301,9 @@ func (ph *PageHandler) GetCachedPages() []string { } func (ph *PageHandler) GetNumberOfCachedPages() int { + if ph.pageContentsCacheRWMutex == nil { + return 0 + } ph.pageContentsCacheRWMutex.RLock() defer ph.pageContentsCacheRWMutex.RUnlock() return len(ph.PageContentsCache) diff --git a/pageHandler/pages.go b/pageHandler/pages.go index 731017a..a098896 100644 --- a/pageHandler/pages.go +++ b/pageHandler/pages.go @@ -1,18 +1,26 @@ 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 -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 } diff --git a/pageHandler/pages/index/index-page.go b/pageHandler/pages/index/index-page.go index 1196073..be9d16f 100644 --- a/pageHandler/pages/index/index-page.go +++ b/pageHandler/pages/index/index-page.go @@ -1,6 +1,7 @@ package index import ( + "errors" "golang.captainalm.com/cityuni-webserver/utils/io" "gopkg.in/yaml.v3" "html/template" @@ -12,10 +13,10 @@ import ( "time" ) +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 { @@ -23,7 +24,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, } @@ -31,7 +35,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 @@ -41,7 +48,7 @@ type Page struct { } func (p *Page) GetPath() string { - return "/index.go" + return p.PagePath } func (p *Page) GetLastModified() time.Time { @@ -129,16 +136,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 } @@ -161,13 +164,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) @@ -189,6 +197,7 @@ func (p *Page) getPageData() (*DataYaml, error) { p.StoredData = dataYaml } return dataYaml, nil + } else { return p.StoredData, nil }