Carry on adding index.go page logic.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Fix logic in the info.go page.
This commit is contained in:
parent
f280becd89
commit
8a637da36a
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ type Marshal struct {
|
||||
OrderEndDate int8
|
||||
OrderName int8
|
||||
OrderDuration int8
|
||||
Light bool
|
||||
}
|
||||
|
||||
func (m Marshal) GetEntries() (toReturn []EntryYaml) {
|
||||
|
Loading…
Reference in New Issue
Block a user