Upgrade internals for page versitility.
Some checks are pending
ci/woodpecker/push/build Pipeline is pending
Some checks are pending
ci/woodpecker/push/build Pipeline is pending
Fix a bug in goinfo
This commit is contained in:
parent
f60e04ada4
commit
4da16c32ae
19
conf/page.go
Normal file
19
conf/page.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -150,6 +150,10 @@
|
||||
<th>Memory Page Size</th>
|
||||
<td>{{ .PageSize }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>System Time</th>
|
||||
<td>{{ .CurrentTime }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</p>
|
||||
<p>
|
||||
|
@ -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(),
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
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
|
||||
}
|
||||
|
@ -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,14 +164,19 @@ 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 {
|
||||
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)
|
||||
if err != nil {
|
||||
@ -189,6 +197,7 @@ func (p *Page) getPageData() (*DataYaml, error) {
|
||||
p.StoredData = dataYaml
|
||||
}
|
||||
return dataYaml, nil
|
||||
|
||||
} else {
|
||||
return p.StoredData, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user