diff --git a/cmd/wappcityuni/main.go b/cmd/wappcityuni/main.go index bfa643e..cd0257c 100644 --- a/cmd/wappcityuni/main.go +++ b/cmd/wappcityuni/main.go @@ -78,6 +78,10 @@ func main() { if err != nil { log.Fatalln("Failed to parse config.yml:", err) } + err = configFile.Close() + if err != nil { + log.Println("Failed to close config file.") + } //Server definitions: var webServer *http.Server diff --git a/pageHandler/go-info-page.go b/pageHandler/go-info-page.go index febb109..4b665a4 100644 --- a/pageHandler/go-info-page.go +++ b/pageHandler/go-info-page.go @@ -25,9 +25,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool DataStore: dataStore, PageTemplateMutex: ptm, } - if !cacheTemplates { - _, _ = pageToReturn.getPageTemplate() - } return pageToReturn } @@ -87,7 +84,6 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin if err != nil { return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false } - theBuffer := &io.BufferedWriter{} var regPages []string var cacPages []string env := make([]string, 0) @@ -99,6 +95,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin regPages = make([]string, len(gipg.Handler.PageProviders)) cacPages = make([]string, gipg.Handler.GetNumberOfCachedPages()) } + theBuffer := &io.BufferedWriter{} err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{ FullOutput: urlParameters.Has("full"), RegisteredPages: regPages, diff --git a/pageHandler/pages/index/data.go b/pageHandler/pages/index/data.go index 086ef83..81afea0 100644 --- a/pageHandler/pages/index/data.go +++ b/pageHandler/pages/index/data.go @@ -1,6 +1,12 @@ package index type DataYaml struct { - About AboutYaml `yaml:"about"` - Entries []EntryYaml `yaml:"entries"` + HomeLink string `yaml:"homeLink"` + PortfolioLink string `yaml:"portfolioLink"` + CSSBaseURL string `yaml:"cssBaseURL"` + CSSLightURL string `yaml:"cssLightURL"` + CSSDarkURL string `yaml:"cssDarkURL"` + JScriptURL string `yaml:"jScriptURL"` + About AboutYaml `yaml:"about"` + Entries []EntryYaml `yaml:"entries"` } diff --git a/pageHandler/pages/index/entry.go b/pageHandler/pages/index/entry.go index fa65084..06c85bd 100644 --- a/pageHandler/pages/index/entry.go +++ b/pageHandler/pages/index/entry.go @@ -16,7 +16,6 @@ type EntryYaml struct { VideoContentType string `yaml:"videoContentType"` ThumbnailLocations []string `yaml:"thumbnailLocations"` ImageLocations []string `yaml:"imageLocations"` - Links []string `yaml:"links"` } func (ey EntryYaml) GetStartDate() string { diff --git a/pageHandler/pages/index/index-page.go b/pageHandler/pages/index/index-page.go index 1aa52ae..6166354 100644 --- a/pageHandler/pages/index/index-page.go +++ b/pageHandler/pages/index/index-page.go @@ -1,6 +1,8 @@ package index import ( + "golang.captainalm.com/cityuni-webserver/utils/io" + "gopkg.in/yaml.v3" "html/template" "net/url" "os" @@ -11,6 +13,7 @@ import ( ) const templateName = "index.go.html" +const yamlName = "index.go.yml" func NewPage(dataStore string, cacheTemplates bool) *Page { var ptm *sync.Mutex @@ -19,19 +22,20 @@ func NewPage(dataStore string, cacheTemplates bool) *Page { } pageToReturn := &Page{ DataStore: dataStore, + StoredDataMutex: &sync.Mutex{}, PageTemplateMutex: ptm, } - if !cacheTemplates { - _, _ = pageToReturn.getPageTemplate() - } return pageToReturn } type Page struct { - DataStore string - PageTemplateMutex *sync.Mutex - PageTemplate *template.Template - LastModified time.Time + DataStore string + StoredDataMutex *sync.Mutex + StoredData *DataYaml + LastModifiedData time.Time + PageTemplateMutex *sync.Mutex + PageTemplate *template.Template + LastModifiedTemplate time.Time } func (p *Page) GetPath() string { @@ -39,29 +43,51 @@ func (p *Page) GetPath() string { } func (p *Page) GetLastModified() time.Time { - return p.LastModified + if p.LastModifiedData.After(p.LastModifiedTemplate) { + return p.LastModifiedData + } else { + return p.LastModifiedTemplate + } } func (p *Page) GetCacheIDExtension(urlParameters url.Values) string { toReturn := "" if urlParameters.Has("order") { - if theParameter := strings.ToLower(urlParameters.Get("order")); - theParameter == "start" || theParameter == "end" || theParameter == "name" || theParameter == "duration" { + if theParameter := strings.ToLower(urlParameters.Get("order")); theParameter == "start" || theParameter == "end" || theParameter == "name" || theParameter == "duration" { toReturn += "order=" + theParameter + "&" } } if urlParameters.Has("sort") { - if theParameter := strings.ToLower(urlParameters.Get("sort")); - theParameter == "asc" || theParameter == "ascending" || theParameter == "desc" || theParameter == "descending" { - toReturn += "sort=" + theParameter + if theParameter := strings.ToLower(urlParameters.Get("sort")); theParameter == "asc" || theParameter == "ascending" || theParameter == "desc" || theParameter == "descending" { + toReturn += "sort=" + theParameter + "&" } } + if urlParameters.Has("light") { + toReturn += "light" + } return strings.TrimRight(toReturn, "&") } func (p *Page) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) { - //TODO implement me - panic("implement me") + theTemplate, err := p.getPageTemplate() + if err != nil { + return "text/plain", []byte("Cannot Get Index.\r\n" + err.Error()), false + } + theData, err := p.getPageData() + if err != nil { + return "text/plain", []byte("Cannot Get Data.\r\n" + err.Error()), false + } + theMarshal := &Marshal{ + Data: *theData, + Light: urlParameters.Has("light"), + } + //Set up sorting here + theBuffer := &io.BufferedWriter{} + err = theTemplate.ExecuteTemplate(theBuffer, templateName, theMarshal) + if err != nil { + return "text/plain", []byte("Cannot Get Page.\r\n" + err.Error()), false + } + return "text/html", theBuffer.Data, true } func (p *Page) PurgeTemplate() { @@ -70,6 +96,11 @@ func (p *Page) PurgeTemplate() { p.PageTemplate = nil p.PageTemplateMutex.Unlock() } + if p.StoredDataMutex != nil { + p.StoredDataMutex.Lock() + p.StoredData = nil + p.StoredDataMutex.Unlock() + } } func (p *Page) getPageTemplate() (*template.Template, error) { @@ -82,6 +113,11 @@ func (p *Page) getPageTemplate() (*template.Template, error) { if p.DataStore != "" { thePath = path.Join(p.DataStore, thePath) } + stat, err := os.Stat(thePath) + if err != nil { + return nil, err + } + p.LastModifiedTemplate = stat.ModTime() loadedData, err := os.ReadFile(thePath) if err != nil { return nil, err @@ -98,3 +134,42 @@ func (p *Page) getPageTemplate() (*template.Template, error) { return p.PageTemplate, nil } } + +func (p *Page) getPageData() (*DataYaml, error) { + if p.StoredDataMutex != nil { + p.StoredDataMutex.Lock() + defer p.StoredDataMutex.Unlock() + } + if p.StoredData == nil { + thePath := yamlName + if p.DataStore != "" { + thePath = path.Join(p.DataStore, thePath) + } + stat, err := os.Stat(thePath) + if err != nil { + return nil, err + } + p.LastModifiedData = stat.ModTime() + fileHandle, err := os.Open(thePath) + if err != nil { + return nil, err + } + dataYaml := &DataYaml{} + decoder := yaml.NewDecoder(fileHandle) + err = decoder.Decode(dataYaml) + if err != nil { + _ = fileHandle.Close() + return nil, err + } + err = fileHandle.Close() + if err != nil { + return nil, err + } + if p.StoredDataMutex != nil { + p.StoredData = dataYaml + } + return dataYaml, nil + } else { + return p.StoredData, nil + } +} diff --git a/pageHandler/pages/index/template-marshal.go b/pageHandler/pages/index/template-marshal.go index 4d5d5ec..afc91a0 100644 --- a/pageHandler/pages/index/template-marshal.go +++ b/pageHandler/pages/index/template-marshal.go @@ -8,6 +8,7 @@ type Marshal struct { OrderEndDate int8 OrderName int8 OrderDuration int8 + Light bool } func (m Marshal) GetEntries() (toReturn []EntryYaml) {