aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetter Rodhelind <petter.rodhelind@gmail.com>2021-02-22 02:00:40 +0100
committerPetter Rodhelind <petter.rodhelind@gmail.com>2021-02-22 02:00:40 +0100
commit385d1002e0ee46a0f2c3a6cfc9138e748a452df5 (patch)
tree32cda9eab6f571d8990c4985bde658a96f5c6da2
parent8db0c2658650ad677c90a42a61061a75af8f762e (diff)
downloadfbfeed-385d1002e0ee46a0f2c3a6cfc9138e748a452df5.tar.gz
fbfeed-385d1002e0ee46a0f2c3a6cfc9138e748a452df5.tar.bz2
fbfeed-385d1002e0ee46a0f2c3a6cfc9138e748a452df5.zip
DO NOT use global state for outputMode...HEADmaster
-rw-r--r--main.go28
1 files 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) {