feed/folder list endpoints

This commit is contained in:
Nazar Kanaev 2020-07-01 20:15:12 +01:00
parent 866d59fe86
commit e7bfc3b0a3
5 changed files with 41 additions and 18 deletions

View File

@ -39,7 +39,19 @@ func StaticHandler(rw http.ResponseWriter, req *http.Request) {
io.Copy(rw, f) io.Copy(rw, f)
} }
func StatusHandler(rw http.ResponseWriter, req *http.Request) {
writeJSON(rw, map[string]interface{}{
"running": handler(req).fetchRunning,
"stats": map[string]int64{},
})
}
func FolderListHandler(rw http.ResponseWriter, req *http.Request) { func FolderListHandler(rw http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
list := db(req).ListFolders()
fmt.Println(list)
json.NewEncoder(rw).Encode(list)
}
} }
func FolderHandler(rw http.ResponseWriter, req *http.Request) { func FolderHandler(rw http.ResponseWriter, req *http.Request) {
@ -51,7 +63,10 @@ type NewFeed struct {
} }
func FeedListHandler(rw http.ResponseWriter, req *http.Request) { func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
if req.Method == "POST" { if req.Method == "GET" {
list := db(req).ListFeeds()
json.NewEncoder(rw).Encode(list)
} else if req.Method == "POST" {
var feed NewFeed var feed NewFeed
if err := json.NewDecoder(req.Body).Decode(&feed); err != nil { if err := json.NewDecoder(req.Body).Decode(&feed); err != nil {
log.Print(err) log.Print(err)
@ -88,13 +103,13 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
} else if len(sources) == 1 { } else if len(sources) == 1 {
feedUrl = sources[0].Url feedUrl = sources[0].Url
fmt.Println("feedUrl:", feedUrl) fmt.Println("feedUrl:", feedUrl)
err = createFeed(db(req), feedUrl, 0) err = createFeed(db(req), feedUrl, feed.FolderID)
if err == nil { if err == nil {
writeJSON(rw, map[string]string{"status": "success"}) writeJSON(rw, map[string]string{"status": "success"})
} }
} }
} else if strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") { } else if strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") {
err = createFeed(db(req), feedUrl, 0) err = createFeed(db(req), feedUrl, feed.FolderID)
if err == nil { if err == nil {
writeJSON(rw, map[string]string{"status": "success"}) writeJSON(rw, map[string]string{"status": "success"})
} }
@ -105,7 +120,7 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
} }
} }
func createFeed(s *storage.Storage, url string, folderId int64) error { func createFeed(s *storage.Storage, url string, folderId *int64) error {
fmt.Println(s, url) fmt.Println(s, url)
fp := gofeed.NewParser() fp := gofeed.NewParser()
feed, err := fp.ParseURL(url) feed, err := fp.ParseURL(url)

View File

@ -17,6 +17,7 @@ type Route struct {
type Handler struct { type Handler struct {
db *storage.Storage db *storage.Storage
fetchRunning bool
} }
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route { func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
@ -38,6 +39,7 @@ func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
var routes []Route = []Route{ var routes []Route = []Route{
p("/", IndexHandler), p("/", IndexHandler),
p("/static/*path", StaticHandler), p("/static/*path", StaticHandler),
p("/api/status", StatusHandler),
p("/api/folders", FolderListHandler), p("/api/folders", FolderListHandler),
p("/api/folders/:id", FolderHandler), p("/api/folders/:id", FolderHandler),
p("/api/feeds", FeedListHandler), p("/api/feeds", FeedListHandler),
@ -59,15 +61,21 @@ func db(req *http.Request) *storage.Storage {
return nil return nil
} }
func handler(req *http.Request) *Handler {
return req.Context().Value(ctxHandler).(*Handler)
}
const ( const (
ctxDB = 1 ctxDB = 1
ctxVars = 2 ctxVars = 2
ctxHandler = 3
) )
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for _, route := range routes { for _, route := range routes {
if route.urlRegex.MatchString(req.URL.Path) { if route.urlRegex.MatchString(req.URL.Path) {
ctx := context.WithValue(req.Context(), ctxDB, h.db) ctx := context.WithValue(req.Context(), ctxDB, h.db)
ctx = context.WithValue(ctx, ctxHandler, &h)
if route.urlRegex.NumSubexp() > 0 { if route.urlRegex.NumSubexp() > 0 {
vars := make(map[string]string) vars := make(map[string]string)
matches := route.urlRegex.FindStringSubmatchIndex(req.URL.Path) matches := route.urlRegex.FindStringSubmatchIndex(req.URL.Path)

View File

@ -1,22 +1,22 @@
package storage package storage
type Feed struct { type Feed struct {
Id int64 Id int64 `json:"id"`
FolderId int64 FolderId *int64 `json:"folder_id"`
Title string Title string `json:"title"`
Description string Description string `json:"description"`
Link string Link string `json:"link"`
FeedLink string FeedLink string `json:"feed_link"`
Icon string Icon string `json:"icon"`
} }
func (s *Storage) CreateFeed(title, description, link, feedLink, icon string, folderId int64) *Feed { func (s *Storage) CreateFeed(title, description, link, feedLink, icon string, folderId *int64) *Feed {
result, err := s.db.Exec(` result, err := s.db.Exec(`
insert into feeds (title, description, link, feed_link, icon, folder_id) insert into feeds (title, description, link, feed_link, icon, folder_id)
values (?, ?, ?, ?, ?, ?) values (?, ?, ?, ?, ?, ?)
on conflict (feed_link) do update set folder_id=?`, on conflict (feed_link) do update set folder_id=?`,
title, description, link, feedLink, icon, intOrNil(folderId), title, description, link, feedLink, icon, folderId,
intOrNil(folderId), folderId,
) )
if err != nil { if err != nil {
return nil return nil

View File

@ -5,9 +5,9 @@ import (
) )
type Folder struct { type Folder struct {
Id int64 Id int64 `json:"id"`
Title string Title string `json:"title"`
IsExpanded bool IsExpanded bool `json:"is_expanded"`
} }
func (s *Storage) CreateFolder(title string) *Folder { func (s *Storage) CreateFolder(title string) *Folder {

View File

@ -11,7 +11,7 @@ var initQuery string = `
create table if not exists folders ( create table if not exists folders (
id integer primary key autoincrement, id integer primary key autoincrement,
title text not null, title text not null,
is_expanded boolean is_expanded boolean not null default false
); );
create table if not exists feeds ( create table if not exists feeds (