all repos — navani @ 153b51ab5981138b1834e08eb273d6fec4cb6ef3

forlater's primary mail processing service

Fix caching issues
Anirudh Oppiliappan x@icyphox.sh
Thu, 16 Sep 2021 12:14:33 +0530
commit

153b51ab5981138b1834e08eb273d6fec4cb6ef3

parent

7251f1ba6f5f2ef42c543124d0132057f4894ee4

3 files changed, 15 insertions(+), 8 deletions(-)

jump to
M main.gomain.go

@@ -2,6 +2,7 @@ package main

import ( "encoding/json" + "fmt" "log" "net/http" "net/url"

@@ -33,6 +34,7 @@ }

article, err := reader.Readable(f, parsedURL) if err == nil { + fmt.Println("here") err = mail.SendArticle(&article, m.From, true) if err != nil { log.Printf("error sending mail to: %s: %v\n", m.From, err)
M reader/fetch.goreader/fetch.go

@@ -31,13 +31,16 @@ // the response body in cache. Returns an io.Reader.

func Fetch(url string) (io.Reader, error) { sum := checksum([]byte(url)) c, err := cache.NewConn() + if err != nil { + return nil, fmt.Errorf("cache error: %w", err) + } body, err := c.Get(sum) // Not in cache. if err != nil { resp, err := http.Get(url) if err != nil { - return nil, err + return nil, fmt.Errorf("http error: %w", err) } buf := bytes.Buffer{} // Read into r and write into buf.

@@ -45,7 +48,11 @@ // Cache and return!

r := io.TeeReader(resp.Body, &buf) b, err := io.ReadAll(r) if err != nil { - c.Set(b) + return nil, fmt.Errorf("io error: %w", err) + } + _, err = c.Set(sum, b) + if err != nil { + return nil, fmt.Errorf("cache error: %w", err) } return &buf, nil }

@@ -56,12 +63,10 @@

// Makes a given html body readable. Returns an error if it // can't. func Readable(r io.Reader, u *url.URL) (Article, error) { - if !readability.Check(r) { - return Article{readability.Article{}, u}, fmt.Errorf("failed to parse %s", u) - } article, err := readability.FromReader(r, u) + fmt.Println("article", article) if err != nil { - return Article{readability.Article{}, u}, fmt.Errorf("failed to parse %s: %v\n", u, err) + return Article{readability.Article{}, u}, fmt.Errorf("failed to parse %s: %w\n", u, err) } return Article{article, u}, nil
M templates/html.tpltemplates/html.tpl

@@ -25,9 +25,9 @@ <![endif]-->

</head> <body style="margin:0;padding:0;word-spacing:normal;background-color:#ffffff;"> <div role="article" aria-roledescription="email" lang="en" style="-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#ffffff;"> - <table width="100%" align="left" border="0" cellpadding="0" cellspacing="0" role="presentation"> + <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" role="presentation"> <tr> - <td align="center"> + <td align="left"> <!--[if mso]> <table role="presentation" align="center" style="width:660px;"> <tr>