Add initial index page code.
All checks were successful
continuous-integration/drone/push Build is passing

Fix Makefile stuff.
This commit is contained in:
Captain ALM 2022-07-16 22:20:56 +01:00
parent e22b4ad3ea
commit f280becd89
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
8 changed files with 242 additions and 13 deletions

View File

@ -13,7 +13,7 @@ ifeq ($(OS),Windows_NT)
BIN := $(BIN).exe BIN := $(BIN).exe
endif endif
.PHONY: build dev test clean .PHONY: build dev test clean deploy
build: build:
mkdir -p dist/ mkdir -p dist/
@ -31,7 +31,7 @@ clean:
${COMP_BIN} clean ${COMP_BIN} clean
rm -r -f dist/ rm -r -f dist/
deploy: deploy: build
sudo systemctl stop wappcityuni sudo systemctl stop wappcityuni
sudo cp "${BIN}" /usr/local/bin sudo cp "${BIN}" /usr/local/bin
sudo systemctl start wappcityuni sudo systemctl start wappcityuni

View File

@ -23,7 +23,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool
pageToReturn := &goInfoPage{ pageToReturn := &goInfoPage{
Handler: handlerIn, Handler: handlerIn,
DataStore: dataStore, DataStore: dataStore,
CacheTemplate: cacheTemplates,
PageTemplateMutex: ptm, PageTemplateMutex: ptm,
} }
if !cacheTemplates { if !cacheTemplates {
@ -35,7 +34,6 @@ func newGoInfoPage(handlerIn *PageHandler, dataStore string, cacheTemplates bool
type goInfoPage struct { type goInfoPage struct {
Handler *PageHandler Handler *PageHandler
DataStore string DataStore string
CacheTemplate bool
PageTemplateMutex *sync.Mutex PageTemplateMutex *sync.Mutex
PageTemplate *template.Template PageTemplate *template.Template
} }
@ -80,10 +78,6 @@ func (gipg *goInfoPage) GetPath() string {
return "/goinfo.go" return "/goinfo.go"
} }
func (gipg *goInfoPage) GetSupportedURLParameters() []string {
return []string{"full"}
}
func (gipg *goInfoPage) GetLastModified() time.Time { func (gipg *goInfoPage) GetLastModified() time.Time {
return time.Now() return time.Now()
} }
@ -139,7 +133,7 @@ func (gipg *goInfoPage) GetContents(urlParameters url.Values) (contentType strin
} }
func (gipg *goInfoPage) PurgeTemplate() { func (gipg *goInfoPage) PurgeTemplate() {
if gipg.CacheTemplate { if gipg.PageTemplateMutex != nil {
gipg.PageTemplateMutex.Lock() gipg.PageTemplateMutex.Lock()
gipg.PageTemplate = nil gipg.PageTemplate = nil
gipg.PageTemplateMutex.Unlock() gipg.PageTemplateMutex.Unlock()
@ -147,7 +141,7 @@ func (gipg *goInfoPage) PurgeTemplate() {
} }
func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) { func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) {
if gipg.CacheTemplate { if gipg.PageTemplateMutex != nil {
gipg.PageTemplateMutex.Lock() gipg.PageTemplateMutex.Lock()
defer gipg.PageTemplateMutex.Unlock() defer gipg.PageTemplateMutex.Unlock()
} }
@ -164,7 +158,7 @@ func (gipg *goInfoPage) getPageTemplate() (*template.Template, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if gipg.CacheTemplate { if gipg.PageTemplateMutex != nil {
gipg.PageTemplate = tmpl gipg.PageTemplate = tmpl
} }
return tmpl, nil return tmpl, nil

View File

@ -1,5 +1,7 @@
package pageHandler package pageHandler
import "golang.captainalm.com/cityuni-webserver/pageHandler/pages/index"
var providers map[string]PageProvider var providers map[string]PageProvider
func GetProviders(cacheTemplates bool, dataStorage string, pageHandler *PageHandler) 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) infoPage := newGoInfoPage(pageHandler, dataStorage, cacheTemplates)
providers[infoPage.GetPath()] = infoPage //Go Information Page providers[infoPage.GetPath()] = infoPage //Go Information Page
} }
indexPage := index.NewPage(dataStorage, cacheTemplates)
//Add the providers in the pages sub package providers[indexPage.GetPath()] = indexPage
} }
return providers return providers
} }

View File

@ -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
}

View File

@ -0,0 +1,6 @@
package index
type DataYaml struct {
About AboutYaml `yaml:"about"`
Entries []EntryYaml `yaml:"entries"`
}

View File

@ -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)
}

View File

@ -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
}
}

View File

@ -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
}