diff options
author | Petter Rodhelind <petter.rodhelind@gmail.com> | 2021-02-22 02:00:40 +0100 |
---|---|---|
committer | Petter Rodhelind <petter.rodhelind@gmail.com> | 2021-02-22 02:00:40 +0100 |
commit | 385d1002e0ee46a0f2c3a6cfc9138e748a452df5 (patch) | |
tree | 32cda9eab6f571d8990c4985bde658a96f5c6da2 /main.go | |
parent | 8db0c2658650ad677c90a42a61061a75af8f762e (diff) | |
download | fbfeed-master.tar.gz fbfeed-master.tar.bz2 fbfeed-master.zip |
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 28 |
1 files changed, 15 insertions, 13 deletions
@@ -10,9 +10,6 @@ import ( ) var ( - // outputMode is either HTML or RSS - outputMode string - // cache will hold all previous network calls and re-use within set timeframe cache safeCache @@ -20,8 +17,12 @@ var ( cacheTimeout float64 ) +const ( + outputHTML = iota + outputRSS +) + func init() { - outputMode = "html" cache = safeCache{v: make(map[string]*channel)} cacheTimeout = 60 * 15 // seconds } @@ -40,12 +41,12 @@ type channel struct { Items []*post } -func (c *channel) String() string { +func (c *channel) Print(outputMode int) string { var template string switch outputMode { - case "html": + case outputHTML: template = htmlRoot - case "rss": + case outputRSS: template = rssRoot } @@ -57,7 +58,7 @@ func (c *channel) String() string { var items string for i := range c.Items { - item := c.Items[i].String() + item := c.Items[i].Print(outputMode) item = strings.Replace(item, "{{link}}", c.Link, -1) items += item } @@ -77,12 +78,12 @@ type image struct { Caption string } -func (p *post) String() string { +func (p *post) Print(outputMode int) string { var template string switch outputMode { - case "html": + case outputHTML: template = htmlItem - case "rss": + case outputRSS: template = rssItem } @@ -123,9 +124,10 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if path[0] == '@' { path = path[1:] } + outputMode := outputHTML if strings.HasSuffix(path, ".rss") { path = strings.TrimSuffix(path, ".rss") - outputMode = "rss" + outputMode = outputRSS } c, ok := cache.v[path] @@ -150,7 +152,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { cache.Unlock() } - fmt.Fprintf(w, "%s\n", c.String()) + fmt.Fprintf(w, "%s\n", c.Print(outputMode)) } func fetch(path string) (c *channel, err error) { |