From 8db0c2658650ad677c90a42a61061a75af8f762e Mon Sep 17 00:00:00 2001 From: Petter Rodhelind Date: Mon, 22 Feb 2021 01:07:06 +0100 Subject: Add a semi-hidden feature of GET /.cache. --- main.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 402ec8f..db1492e 100644 --- a/main.go +++ b/main.go @@ -102,39 +102,47 @@ func (p *post) String() string { type handler struct{} func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/favicon.ico" { - return - } + path := r.URL.Path[1:] - if r.URL.Path == "/" { + switch path { + case "": http.Error(w, fmt.Sprintf("usage: %s/@facebookGroupName", r.Host), 400) return + case "favicon.ico": + return + case ".cache": + var s string + for k, v := range cache.v { + s += v.Time.Format("2006-01-02 15:04:05") + s += "\t" + k + "\n" + } + fmt.Fprintf(w, "%s", s) + return } - group := r.URL.Path[1:] - if group[0] == '@' { - group = group[1:] + if path[0] == '@' { + path = path[1:] } - if strings.HasSuffix(group, ".rss") { - group = strings.TrimSuffix(group, ".rss") + if strings.HasSuffix(path, ".rss") { + path = strings.TrimSuffix(path, ".rss") outputMode = "rss" } - c, ok := cache.v[group] + c, ok := cache.v[path] if !ok || time.Now().Sub(c.Time).Seconds() > cacheTimeout { var err error - c, err = fetch(group) + c, err = fetch(path) if err != nil { http.Error(w, fmt.Sprintf("error: %s", err), 400) return } if c == nil || len(c.Items) < 1 { - http.Error(w, fmt.Sprintf("%s", "Group not found."), 400) + http.Error(w, fmt.Sprintf("%s", "Page not found."), 400) return } - c.Name = group + c.Name = path c.Time = time.Now() cache.Lock() @@ -145,12 +153,12 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s\n", c.String()) } -func fetch(group string) (c *channel, err error) { - if group == "" { +func fetch(path string) (c *channel, err error) { + if path == "" { return } - url := "https://www.facebook.com/pg/" + group + "/posts/" + url := "https://www.facebook.com/pg/" + path + "/posts/" log.Println("Fetching:", url) -- cgit v1.2.3