From 385d1002e0ee46a0f2c3a6cfc9138e748a452df5 Mon Sep 17 00:00:00 2001 From: Petter Rodhelind Date: Mon, 22 Feb 2021 02:00:40 +0100 Subject: DO NOT use global state for outputMode... --- main.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index db1492e..1dd845c 100644 --- a/main.go +++ b/main.go @@ -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) { -- cgit v1.2.3