diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-24 12:52:38 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-24 12:52:38 -0400 |
| commit | 268fcf7e6b671d4959a12111d5abf553bf0a201b (patch) | |
| tree | 6f5c46c9a765478eaae78d66f4b9aefc953bbd1a /page/index.go | |
| parent | d36b6a55c8fa4400cd39de717443e110e990a8a3 (diff) | |
| download | go-website-268fcf7e6b671d4959a12111d5abf553bf0a201b.tar.gz go-website-268fcf7e6b671d4959a12111d5abf553bf0a201b.tar.xz | |
Redis caching. Linter config and cleanup.
Diffstat (limited to 'page/index.go')
| -rw-r--r-- | page/index.go | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/page/index.go b/page/index.go index f9f2df5..425bf04 100644 --- a/page/index.go +++ b/page/index.go @@ -8,27 +8,32 @@ import ( "time" ) -var index map[string]PageList -var indexMu sync.RWMutex +var ( + index map[string]PageList + indexMu sync.RWMutex +) // RebuildIndex can be called in order to rebuild the entire website -// index +// index. func (p *Page) RebuildIndex() error { indexMu.Lock() index = nil indexMu.Unlock() + _, err := p.Index() + return err } -// Index returns a map of all pages in the current directory seperated into +// Index returns a map of all pages in the current directory separated into // their respective tags If a Page has multiple tags it will be listed under // each. -// Pages are located by their Suffix, default being ".md" +// Pages are located by their Suffix, default being ".md". func (p *Page) Index() (map[string]PageList, error) { indexMu.RLock() if index != nil && CacheIndex { indexMu.RUnlock() + return index, nil } indexMu.RUnlock() @@ -36,30 +41,29 @@ func (p *Page) Index() (map[string]PageList, error) { out := make(map[string]PageList) - filepath.Walk(filepath.Dir("."), + _ = filepath.Walk(filepath.Dir("."), func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() && strings.HasSuffix(info.Name(), Suffix) { - p2 := NewPage(strings.ReplaceAll(path, Suffix, "")) err = p2.Read() if err != nil { Logger.Println("Error encountered: ", err) + return err } - for tag, _ := range p2.Tags { + for tag := range p2.Tags { if _, ok := out[tag]; !ok { out[tag] = []*Page{p2} } else { out[tag] = append(out[tag], p2) } } - } return nil @@ -72,6 +76,7 @@ func (p *Page) Index() (map[string]PageList, error) { return out, nil } +// Time fetches the time.Time from the Date field. func (p *Page) Time() time.Time { return p.Date.Time } |
