New RSSFeed doesn't auto update, server callback adds to feed pool, monitor callback updates feed

This commit is contained in:
Bel LaPointe
2018-10-09 08:52:37 -06:00
parent 8f5630353e
commit 1c6bea94e2
5 changed files with 95 additions and 46 deletions

View File

@@ -1,10 +1,13 @@
package server
import (
"encoding/json"
"net/http"
"net/url"
"os"
"os/signal"
"path"
"regexp"
"strings"
"syscall"
"time"
@@ -12,10 +15,10 @@ import (
type Server struct {
addr string
newItemHandler func(string, time.Duration)
newItemHandler func(string, string, string, time.Duration)
}
func New(addr string, newItemHandler func(string, time.Duration)) (*Server, error) {
func New(addr string, newItemHandler func(string, string, string, time.Duration)) (*Server, error) {
return &Server{
addr: addr,
newItemHandler: newItemHandler,
@@ -47,20 +50,28 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "api":
s.api(w, r)
default:
s.bad(w, r)
s.notFound(w, r)
}
}
func (s *Server) bad(w http.ResponseWriter, r *http.Request) {
func (s *Server) notFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
func (s *Server) bad(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}
func (s *Server) mybad(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
func (s *Server) api(w http.ResponseWriter, r *http.Request) {
switch advance(r) {
case "feed":
s.feed(w, r)
default:
s.bad(w, r)
s.notFound(w, r)
}
}
@@ -68,12 +79,51 @@ func (s *Server) feed(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
case "POST":
s.newItem(w, r)
case "PUT":
s.newItem(w, r)
default:
s.bad(w, r)
s.notFound(w, r)
}
}
func (s *Server) newItem(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
newItemBody := struct {
URL string `json:"url"`
Refresh string `json:"refresh"`
ItemFilter string `json:"items"`
ContentFilter string `json:"content"`
}{}
if err := json.NewDecoder(r.Body).Decode(&newItemBody); err != nil {
s.bad(w, r)
return
}
interval, err := time.ParseDuration(newItemBody.Refresh)
if err != nil {
s.bad(w, r)
return
}
if !validURL(newItemBody.URL) {
s.bad(w, r)
return
}
if _, err := regexp.Compile(newItemBody.ItemFilter); err != nil {
s.bad(w, r)
return
}
if _, err := regexp.Compile(newItemBody.ContentFilter); err != nil {
s.bad(w, r)
return
}
s.newItemHandler(newItemBody.URL, newItemBody.ItemFilter, newItemBody.ContentFilter, interval)
}
func validURL(loc string) bool {
_, err := url.ParseRequestURI(loc)
return err == nil
}
func advance(r *http.Request) string {
p := path.Clean("/" + r.URL.Path)
i := strings.Index(p[1:], "/") + 1

View File

@@ -1,6 +1,7 @@
package server
import (
"bytes"
"fmt"
"net/http"
"syscall"
@@ -18,7 +19,7 @@ func Test_Server(t *testing.T) {
for _, _ = range cases {
var err error
s, err := New(testPort)
s, err := New(testPort, func(string, string, string, time.Duration) {})
if err != nil {
t.Errorf("failed to create server: %v", err)
}
@@ -33,19 +34,29 @@ func Test_Server(t *testing.T) {
if err := checkStatus("GET", "api/feed", http.StatusOK); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusOK); err != nil {
if err := checkStatus("POST", "api/feed", http.StatusBadRequest); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusOK); err != nil {
if err := checkStatus("PUT", "api/feed", http.StatusBadRequest, "invalid json"); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("POST", "api/feed", http.StatusBadRequest, `{"url":"hello/world", "refresh":"1m"}`); err != nil {
t.Errorf(err.Error())
}
if err := checkStatus("PUT", "api/feed", http.StatusOK, `{"url":"localhost:1234", "refresh":"1m"}`); err != nil {
t.Errorf(err.Error())
}
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
}
func checkStatus(method, path string, code int) error {
func checkStatus(method, path string, code int, body ...string) error {
b := bytes.NewBuffer(nil)
if len(body) > 0 {
b = bytes.NewBufferString(body[0])
}
client := &http.Client{}
r, _ := http.NewRequest(method, "http://localhost:"+testPort+"/"+path, nil)
r, _ := http.NewRequest(method, "http://localhost:"+testPort+"/"+path, b)
resp, err := client.Do(r)
if err != nil {
return fmt.Errorf("failed to %v server: %v", method, err)