Accept GET method to post urls

master
2577 3 years ago
parent 9b96abc9d6
commit bfcfe3cfba
  1. 25
      main.go

@ -3,7 +3,6 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"net/http"
"strings"
@ -32,28 +31,32 @@ func (s *serve) htmlHandler(w http.ResponseWriter, req *http.Request) {
}
func (s *serve) postHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
if req.URL.Path == "/" {
log.Printf("GET...")
s.htmlHandler(w, req)
return
}
if req.Method != "POST" || !strings.Contains(req.URL.Path, s.token) {
if !strings.Contains(req.URL.Path, s.token) {
log.Printf("Invalid request (%s): %v", req.Method, req.URL)
http.NotFound(w, req)
return
}
buff, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Error reading body: %v", err)
req.ParseForm()
urls, ok := req.Form["url"]
if !ok || len(urls) < 1 {
log.Printf("Not valid urls: %v", req.URL)
return
}
err = s.feed.add(string(buff))
if err != nil {
log.Printf("Error adding entry to feed: %v", err)
} else {
log.Printf("Added url to the queue: %s", string(buff))
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)
}
}
}

Loading…
Cancel
Save