all repos — navani @ 8e576664699c9c4a292bd9ffda650d7b0e8e086b

forlater's primary mail processing service

main.go (view raw)

 1package main
 2
 3import (
 4	"encoding/json"
 5	"log"
 6	"net/http"
 7	"net/url"
 8
 9	"git.icyphox.sh/forlater/navani/mail"
10	"git.icyphox.sh/forlater/navani/reader"
11)
12
13func main() {
14	http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
15		m := mail.Mail{}
16		json.NewDecoder(r.Body).Decode(&m)
17		body, err := mail.MailBody(m.Parts)
18		if err != nil {
19			log.Println(err)
20		}
21
22		for _, u := range distinct(mail.ExtractURLs(body)) {
23			parsedURL, err := url.Parse(u)
24			if err != nil {
25				log.Println(err)
26			}
27
28			f, err := reader.Fetch(parsedURL.String())
29			if err != nil {
30				log.Println(err)
31			}
32
33			article, err := reader.Readable(f, parsedURL)
34			if err != nil {
35				log.Println(err)
36			}
37
38			err = mail.SendArticle(&article, m.From)
39			if err != nil {
40				log.Println(err)
41			}
42		}
43		log.Printf("sent mail to %s\n", m.From)
44		w.WriteHeader(204)
45	})
46
47	http.ListenAndServe(":8001", nil)
48}