Carry on adding index.go page logic.
All checks were successful
continuous-integration/drone/push Build is passing

Fix logic in the info.go page.
This commit is contained in:
Captain ALM 2022-07-17 13:37:50 +01:00
parent f280becd89
commit 8a637da36a
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
6 changed files with 104 additions and 22 deletions

View File

@ -78,6 +78,10 @@ func main() {
if err != nil { if err != nil {
log.Fatalln("Failed to parse config.yml:", err) log.Fatalln("Failed to parse config.yml:", err)
} }
err = configFile.Close()
if err != nil {
log.Println("Failed to close config file.")
}
//Server definitions: //Server definitions:
var webServer *http.Server var webServer *http.Server

View File

@ -25,9 +25,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool
DataStore: dataStore, DataStore: dataStore,
PageTemplateMutex: ptm, PageTemplateMutex: ptm,
} }
if !cacheTemplates {
_, _ = pageToReturn.getPageTemplate()
}
return pageToReturn return pageToReturn
} }
@ -87,7 +84,6 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin
if err != nil { if err != nil {
return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false return "text/plain", []byte("Cannot Get Info.\r\n" + err.Error()), false
} }
theBuffer := &io.BufferedWriter{}
var regPages []string var regPages []string
var cacPages []string var cacPages []string
env := make([]string, 0) env := make([]string, 0)
@ -99,6 +95,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin
regPages = make([]string, len(gipg.Handler.PageProviders)) regPages = make([]string, len(gipg.Handler.PageProviders))
cacPages = make([]string, gipg.Handler.GetNumberOfCachedPages()) cacPages = make([]string, gipg.Handler.GetNumberOfCachedPages())
} }
theBuffer := &io.BufferedWriter{}
err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{ err = theTemplate.ExecuteTemplate(theBuffer, templateName, &goInfoTemplateMarshal{
FullOutput: urlParameters.Has("full"), FullOutput: urlParameters.Has("full"),
RegisteredPages: regPages, RegisteredPages: regPages,

View File

@ -1,6 +1,12 @@
package index package index
type DataYaml struct { type DataYaml struct {
About AboutYaml `yaml:"about"` HomeLink string `yaml:"homeLink"`
Entries []EntryYaml `yaml:"entries"` 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"`
} }

View File

@ -16,7 +16,6 @@ type EntryYaml struct {
VideoContentType string `yaml:"videoContentType"` VideoContentType string `yaml:"videoContentType"`
ThumbnailLocations []string `yaml:"thumbnailLocations"` ThumbnailLocations []string `yaml:"thumbnailLocations"`
ImageLocations []string `yaml:"imageLocations"` ImageLocations []string `yaml:"imageLocations"`
Links []string `yaml:"links"`
} }
func (ey EntryYaml) GetStartDate() string { func (ey EntryYaml) GetStartDate() string {

View File

@ -1,6 +1,8 @@
package index package index
import ( import (
"golang.captainalm.com/cityuni-webserver/utils/io"
"gopkg.in/yaml.v3"
"html/template" "html/template"
"net/url" "net/url"
"os" "os"
@ -11,6 +13,7 @@ import (
) )
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) *Page {
var ptm *sync.Mutex var ptm *sync.Mutex
@ -19,19 +22,20 @@ func NewPage(dataStore string, cacheTemplates bool) *Page {
} }
pageToReturn := &Page{ pageToReturn := &Page{
DataStore: dataStore, DataStore: dataStore,
StoredDataMutex: &sync.Mutex{},
PageTemplateMutex: ptm, PageTemplateMutex: ptm,
} }
if !cacheTemplates {
_, _ = pageToReturn.getPageTemplate()
}
return pageToReturn return pageToReturn
} }
type Page struct { type Page struct {
DataStore string DataStore string
PageTemplateMutex *sync.Mutex StoredDataMutex *sync.Mutex
PageTemplate *template.Template StoredData *DataYaml
LastModified time.Time LastModifiedData time.Time
PageTemplateMutex *sync.Mutex
PageTemplate *template.Template
LastModifiedTemplate time.Time
} }
func (p *Page) GetPath() string { func (p *Page) GetPath() string {
@ -39,29 +43,51 @@ func (p *Page) GetPath() string {
} }
func (p *Page) GetLastModified() time.Time { 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 { func (p *Page) GetCacheIDExtension(urlParameters url.Values) string {
toReturn := "" toReturn := ""
if urlParameters.Has("order") { if urlParameters.Has("order") {
if theParameter := strings.ToLower(urlParameters.Get("order")); if theParameter := strings.ToLower(urlParameters.Get("order")); theParameter == "start" || theParameter == "end" || theParameter == "name" || theParameter == "duration" {
theParameter == "start" || theParameter == "end" || theParameter == "name" || theParameter == "duration" {
toReturn += "order=" + theParameter + "&" toReturn += "order=" + theParameter + "&"
} }
} }
if urlParameters.Has("sort") { if urlParameters.Has("sort") {
if theParameter := strings.ToLower(urlParameters.Get("sort")); if theParameter := strings.ToLower(urlParameters.Get("sort")); theParameter == "asc" || theParameter == "ascending" || theParameter == "desc" || theParameter == "descending" {
theParameter == "asc" || theParameter == "ascending" || theParameter == "desc" || theParameter == "descending" { toReturn += "sort=" + theParameter + "&"
toReturn += "sort=" + theParameter
} }
} }
if urlParameters.Has("light") {
toReturn += "light"
}
return strings.TrimRight(toReturn, "&") return strings.TrimRight(toReturn, "&")
} }
func (p *Page) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) { func (p *Page) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) {
//TODO implement me theTemplate, err := p.getPageTemplate()
panic("implement me") 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() { func (p *Page) PurgeTemplate() {
@ -70,6 +96,11 @@ func (p *Page) PurgeTemplate() {
p.PageTemplate = nil p.PageTemplate = nil
p.PageTemplateMutex.Unlock() p.PageTemplateMutex.Unlock()
} }
if p.StoredDataMutex != nil {
p.StoredDataMutex.Lock()
p.StoredData = nil
p.StoredDataMutex.Unlock()
}
} }
func (p *Page) getPageTemplate() (*template.Template, error) { func (p *Page) getPageTemplate() (*template.Template, error) {
@ -82,6 +113,11 @@ func (p *Page) getPageTemplate() (*template.Template, error) {
if p.DataStore != "" { if p.DataStore != "" {
thePath = path.Join(p.DataStore, thePath) 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) loadedData, err := os.ReadFile(thePath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -98,3 +134,42 @@ func (p *Page) getPageTemplate() (*template.Template, error) {
return p.PageTemplate, nil 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
}
}

View File

@ -8,6 +8,7 @@ type Marshal struct {
OrderEndDate int8 OrderEndDate int8
OrderName int8 OrderName int8
OrderDuration int8 OrderDuration int8
Light bool
} }
func (m Marshal) GetEntries() (toReturn []EntryYaml) { func (m Marshal) GetEntries() (toReturn []EntryYaml) {