mirror of
https://github.com/nkanaev/yarr.git
synced 2025-09-13 09:55:36 +00:00
create feeds
This commit is contained in:
@@ -2,6 +2,8 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/nkanaev/yarr/worker"
|
||||
"github.com/nkanaev/yarr/storage"
|
||||
"github.com/mmcdole/gofeed"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"os"
|
||||
@@ -14,7 +16,6 @@ import (
|
||||
)
|
||||
|
||||
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
fmt.Println(os.Getwd())
|
||||
f, err := os.Open("template/index.html")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -45,8 +46,8 @@ func FolderHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
type NewFeed struct {
|
||||
Url string `json:"url"`
|
||||
FolderID int64 `json:"folder_id,omitempty"`
|
||||
Url string `json:"url"`
|
||||
FolderID *int64 `json:"folder_id,omitempty"`
|
||||
}
|
||||
|
||||
func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
@@ -57,6 +58,7 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
feedUrl := feed.Url
|
||||
res, err := http.Get(feedUrl)
|
||||
if err != nil {
|
||||
@@ -73,6 +75,8 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
sources, err := worker.FindFeeds(res)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
writeJSON(rw, map[string]string{"status": "notfound"})
|
||||
@@ -84,15 +88,42 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
} else if len(sources) == 1 {
|
||||
feedUrl = sources[0].Url
|
||||
fmt.Println("feedUrl:", feedUrl)
|
||||
err = createFeed(db(req), feedUrl, 0)
|
||||
if err == nil {
|
||||
writeJSON(rw, map[string]string{"status": "success"})
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") {
|
||||
err = createFeed(db(req), feedUrl, 0)
|
||||
if err == nil {
|
||||
writeJSON(rw, map[string]string{"status": "success"})
|
||||
}
|
||||
fmt.Println("got html url", sources, feedUrl)
|
||||
} else if strings.HasPrefix(contentType, "text/xml") {
|
||||
log.Print("got rss feed")
|
||||
} else {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
log.Print(res.Header.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
func createFeed(s *storage.Storage, url string, folderId int64) error {
|
||||
fmt.Println(s, url)
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURL(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
entry := s.CreateFeed(
|
||||
feed.Title,
|
||||
feed.Description,
|
||||
feed.Link,
|
||||
feed.FeedLink,
|
||||
"",
|
||||
folderId,
|
||||
)
|
||||
|
||||
fmt.Println("here we go", entry)
|
||||
return nil
|
||||
}
|
||||
|
||||
func FeedHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"regexp"
|
||||
"net/http"
|
||||
"github.com/nkanaev/yarr/storage"
|
||||
"log"
|
||||
)
|
||||
|
||||
@@ -15,6 +16,7 @@ type Route struct {
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
db *storage.Storage
|
||||
}
|
||||
|
||||
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
|
||||
@@ -44,25 +46,37 @@ var routes []Route = []Route{
|
||||
}
|
||||
|
||||
func Vars(req *http.Request) map[string]string {
|
||||
if rv := req.Context().Value(0); rv != nil {
|
||||
if rv := req.Context().Value(ctxVars); rv != nil {
|
||||
return rv.(map[string]string)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func db(req *http.Request) *storage.Storage {
|
||||
if rv := req.Context().Value(ctxDB); rv != nil {
|
||||
return rv.(*storage.Storage)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
ctxDB = 1
|
||||
ctxVars = 2
|
||||
)
|
||||
|
||||
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
for _, route := range routes {
|
||||
if route.urlRegex.MatchString(req.URL.Path) {
|
||||
ctx := context.WithValue(req.Context(), ctxDB, h.db)
|
||||
if route.urlRegex.NumSubexp() > 0 {
|
||||
vars := make(map[string]string)
|
||||
matches := route.urlRegex.FindStringSubmatchIndex(req.URL.Path)
|
||||
for i, key := range route.urlRegex.SubexpNames()[1:] {
|
||||
vars[key] = req.URL.Path[matches[i*2+2]:matches[i*2+3]]
|
||||
}
|
||||
ctx := context.WithValue(req.Context(), 0, vars)
|
||||
req = req.WithContext(ctx)
|
||||
ctx = context.WithValue(ctx, ctxVars, vars)
|
||||
}
|
||||
route.handler(rw, req)
|
||||
route.handler(rw, req.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -80,7 +94,8 @@ func writeJSON(rw http.ResponseWriter, data interface{}) {
|
||||
}
|
||||
|
||||
func New() *http.Server {
|
||||
h := Handler{}
|
||||
db, _ := storage.New()
|
||||
h := Handler{db: db}
|
||||
s := &http.Server{Addr: "127.0.0.1:8000", Handler: h}
|
||||
return s
|
||||
}
|
||||
|
Reference in New Issue
Block a user