You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.1 KiB
93 lines
2.1 KiB
package main |
|
|
|
import ( |
|
"flag" |
|
"html/template" |
|
"log" |
|
"net/http" |
|
"strings" |
|
|
|
"github.com/jamiealquiza/envy" |
|
) |
|
|
|
type serve struct { |
|
feed *feed |
|
token string |
|
} |
|
|
|
func (s *serve) rssHandler(w http.ResponseWriter, req *http.Request) { |
|
log.Printf("Serve rss") |
|
w.Write([]byte(s.feed.rss())) |
|
} |
|
|
|
func (s *serve) htmlHandler(w http.ResponseWriter, req *http.Request) { |
|
log.Printf("Serve html") |
|
items := s.feed.items() |
|
index := template.Must(template.ParseFiles("index.html")) |
|
if index == nil { |
|
log.Printf("Error parsing template!") |
|
return |
|
} |
|
index.Execute(w, items) |
|
log.Printf("Rendered %v items", len(items)) |
|
} |
|
|
|
func (s *serve) postHandler(w http.ResponseWriter, req *http.Request) { |
|
if req.URL.Path == "/" { |
|
log.Printf("GET...") |
|
s.htmlHandler(w, req) |
|
return |
|
} |
|
|
|
if !strings.Contains(req.URL.Path, s.token) { |
|
log.Printf("Invalid request (%s): %v", req.Method, req.URL) |
|
http.NotFound(w, req) |
|
return |
|
} |
|
|
|
req.ParseForm() |
|
urls, ok := req.Form["url"] |
|
if !ok || len(urls) < 1 { |
|
log.Printf("Not valid urls: %v", req.URL) |
|
return |
|
} |
|
|
|
for _, url := range urls { |
|
err := s.feed.add(url) |
|
if err != nil { |
|
log.Printf("Error adding to feed: %v", err) |
|
} else { |
|
log.Printf("Added url to the queue: %s", url) |
|
} |
|
} |
|
} |
|
|
|
func main() { |
|
dbPath := flag.String("db-path", "./feed.db", "the path to the database") |
|
token := flag.String("token", "foobar", "token for authentication") |
|
customerKey := flag.String("customer-key", "", "pocket customer key") |
|
accessToken := flag.String("access-token", "", "pocket access token") |
|
envy.Parse("FEED") |
|
flag.Parse() |
|
|
|
var fetcher contentFetcher |
|
if *accessToken != "" && *customerKey != "" { |
|
fetcher = newPocket(*customerKey, *accessToken) |
|
} else { |
|
fetcher = &dummyContentFetcher{} |
|
} |
|
|
|
f, err := newFeed(*dbPath, fetcher) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer f.close() |
|
|
|
staticServer := http.FileServer(http.Dir("./static")) |
|
s := serve{f, *token} |
|
mux := http.NewServeMux() |
|
mux.Handle("/static/", http.StripPrefix("/static", staticServer)) |
|
mux.HandleFunc("/rss", s.rssHandler) |
|
mux.HandleFunc("/", s.postHandler) |
|
log.Fatal(http.ListenAndServe(":2577", mux)) |
|
}
|
|
|