diff --git a/Makefile b/Makefile index ae238a4..2074b0c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ ifeq ($(OS),Windows_NT) BIN := $(BIN).exe endif -.PHONY: build dev test clean +.PHONY: build dev test clean deploy build: mkdir -p dist/ @@ -31,7 +31,7 @@ clean: ${COMP_BIN} clean rm -r -f dist/ -deploy: +deploy: build sudo systemctl stop wappcityuni sudo cp "${BIN}" /usr/local/bin sudo systemctl start wappcityuni diff --git a/pageHandler/go-info-page.go b/pageHandler/go-info-page.go index 99da92d..febb109 100644 --- a/pageHandler/go-info-page.go +++ b/pageHandler/go-info-page.go @@ -23,7 +23,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool pageToReturn := &goInfoPage{ Handler: handlerIn, DataStore: dataStore, - CacheTemplate: cacheTemplates, PageTemplateMutex: ptm, } if !cacheTemplates { @@ -35,7 +34,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool type goInfoPage struct { Handler *PageHandler DataStore string - CacheTemplate bool PageTemplateMutex *sync.Mutex PageTemplate *template.Template } @@ -80,10 +78,6 @@ func (gipg *goInfoPage) GetPath() string { return "/goinfo.go" } -func (gipg *goInfoPage) GetSupportedURLParameters() []string { - return []string{"full"} -} - func (gipg *goInfoPage) GetLastModified() time.Time { return time.Now() } @@ -139,7 +133,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin } func (gipg *goInfoPage) PurgeTemplate() { - if gipg.CacheTemplate { + if gipg.PageTemplateMutex != nil { gipg.PageTemplateMutex.Lock() gipg.PageTemplate = nil gipg.PageTemplateMutex.Unlock() @@ -147,7 +141,7 @@ func (gipg *goInfoPage) PurgeTemplate() { } func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) { - if gipg.CacheTemplate { + if gipg.PageTemplateMutex != nil { gipg.PageTemplateMutex.Lock() defer gipg.PageTemplateMutex.Unlock() } @@ -164,7 +158,7 @@ func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) { if err != nil { return nil, err } - if gipg.CacheTemplate { + if gipg.PageTemplateMutex != nil { gipg.PageTemplate = tmpl } return tmpl, nil diff --git a/pageHandler/pages.go b/pageHandler/pages.go index 3b7197a..731017a 100644 --- a/pageHandler/pages.go +++ b/pageHandler/pages.go @@ -1,5 +1,7 @@ package pageHandler +import "golang.captainalm.com/cityuni-webserver/pageHandler/pages/index" + var providers map[string]PageProvider func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHandler) map[string]PageProvider { @@ -9,8 +11,8 @@ func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHand infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates) providers[infoPage.GetPath()] = infoPage //Go Information Page } - - //Add the providers in the pages sub package + indexPage := index.NewPage(dataStorage, cacheTemplates) + providers[indexPage.GetPath()] = indexPage } return providers } diff --git a/pageHandler/pages/index/about.go b/pageHandler/pages/index/about.go new file mode 100644 index 0000000..4379299 --- /dev/null +++ b/pageHandler/pages/index/about.go @@ -0,0 +1,23 @@ +package index + +import ( + "html/template" + "time" +) + +type AboutYaml struct { + Title string `yaml:"title"` + Content string `yaml:"content"` + ThumbnailLocation string `yaml:"thumbnailLocation"` + ImageLocation string `yaml:"imageLocation"` + BirthYear int `yaml:"birthYear"` + ContactEmail string `yaml:"contactEmail"` +} + +func (ay AboutYaml) GetContent() template.HTML { + return template.HTML(ay.Content) +} + +func (ay AboutYaml) GetAge() int { + return time.Now().Year() - ay.BirthYear - 1 +} diff --git a/pageHandler/pages/index/data.go b/pageHandler/pages/index/data.go new file mode 100644 index 0000000..086ef83 --- /dev/null +++ b/pageHandler/pages/index/data.go @@ -0,0 +1,6 @@ +package index + +type DataYaml struct { + About AboutYaml `yaml:"about"` + Entries []EntryYaml `yaml:"entries"` +} diff --git a/pageHandler/pages/index/entry.go b/pageHandler/pages/index/entry.go new file mode 100644 index 0000000..fa65084 --- /dev/null +++ b/pageHandler/pages/index/entry.go @@ -0,0 +1,48 @@ +package index + +import ( + "html/template" + "time" +) + +const dateFormat = "2006-01-02" + +type EntryYaml struct { + Name string `yaml:"name"` + Content string `yaml:"content"` + StartDate time.Time `yaml:"startDate"` + EndDate time.Time `yaml:"endDate"` + VideoLocation string `yaml:"videoLocation"` + VideoContentType string `yaml:"videoContentType"` + ThumbnailLocations []string `yaml:"thumbnailLocations"` + ImageLocations []string `yaml:"imageLocations"` + Links []string `yaml:"links"` +} + +func (ey EntryYaml) GetStartDate() string { + return ey.StartDate.Format(dateFormat) +} + +func (ey EntryYaml) GetEndDate() string { + if ey.EndDate.IsZero() { + return "" + } else { + return ey.EndDate.Format(dateFormat) + } +} + +func (ey EntryYaml) GetEndTime() time.Time { + if ey.EndDate.IsZero() { + return time.Now() + } else { + return ey.EndDate + } +} + +func (ey EntryYaml) GetContent() template.HTML { + return template.HTML(ey.Content) +} + +func (ey EntryYaml) GetDuration() time.Duration { + return ey.GetEndTime().Sub(ey.StartDate).Truncate(time.Second) +} diff --git a/pageHandler/pages/index/index-page.go b/pageHandler/pages/index/index-page.go new file mode 100644 index 0000000..1aa52ae --- /dev/null +++ b/pageHandler/pages/index/index-page.go @@ -0,0 +1,100 @@ +package index + +import ( + "html/template" + "net/url" + "os" + "path" + "strings" + "sync" + "time" +) + +const templateName = "index.go.html" + +func NewPage(dataStore string, cacheTemplates bool) *Page { + var ptm *sync.Mutex + if cacheTemplates { + ptm = &sync.Mutex{} + } + pageToReturn := &Page{ + DataStore: dataStore, + PageTemplateMutex: ptm, + } + if !cacheTemplates { + _, _ = pageToReturn.getPageTemplate() + } + return pageToReturn +} + +type Page struct { + DataStore string + PageTemplateMutex *sync.Mutex + PageTemplate *template.Template + LastModified time.Time +} + +func (p *Page) GetPath() string { + return "/index.go" +} + +func (p *Page) GetLastModified() time.Time { + return p.LastModified +} + +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" { + 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 + } + } + return strings.TrimRight(toReturn, "&") +} + +func (p *Page) GetContents(urlParameters url.Values) (contentType string, contents []byte, canCache bool) { + //TODO implement me + panic("implement me") +} + +func (p *Page) PurgeTemplate() { + if p.PageTemplateMutex != nil { + p.PageTemplateMutex.Lock() + p.PageTemplate = nil + p.PageTemplateMutex.Unlock() + } +} + +func (p *Page) getPageTemplate() (*template.Template, error) { + if p.PageTemplateMutex != nil { + p.PageTemplateMutex.Lock() + defer p.PageTemplateMutex.Unlock() + } + if p.PageTemplate == nil { + thePath := templateName + if p.DataStore != "" { + thePath = path.Join(p.DataStore, thePath) + } + loadedData, err := os.ReadFile(thePath) + if err != nil { + return nil, err + } + tmpl, err := template.New(templateName).Parse(string(loadedData)) + if err != nil { + return nil, err + } + if p.PageTemplateMutex != nil { + p.PageTemplate = tmpl + } + return tmpl, nil + } else { + return p.PageTemplate, nil + } +} diff --git a/pageHandler/pages/index/template-marshal.go b/pageHandler/pages/index/template-marshal.go new file mode 100644 index 0000000..4d5d5ec --- /dev/null +++ b/pageHandler/pages/index/template-marshal.go @@ -0,0 +1,56 @@ +package index + +import "sort" + +type Marshal struct { + Data DataYaml + OrderStartDate int8 + OrderEndDate int8 + OrderName int8 + OrderDuration int8 +} + +func (m Marshal) GetEntries() (toReturn []EntryYaml) { + toReturn = m.Data.Entries + if m.OrderStartDate > 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].StartDate.Before(toReturn[j].StartDate) + }) + } + if m.OrderStartDate < 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].StartDate.After(toReturn[j].StartDate) + }) + } + if m.OrderEndDate > 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].GetEndTime().Before(toReturn[j].GetEndTime()) + }) + } + if m.OrderEndDate < 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].GetEndTime().After(toReturn[j].GetEndTime()) + }) + } + if m.OrderName > 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].Name < toReturn[j].Name + }) + } + if m.OrderName < 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].Name > toReturn[j].Name + }) + } + if m.OrderDuration > 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].GetDuration() < toReturn[j].GetDuration() + }) + } + if m.OrderDuration < 0 { + sort.Slice(toReturn, func(i, j int) bool { + return toReturn[i].GetDuration() > toReturn[j].GetDuration() + }) + } + return toReturn +}