1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package api
import (
"encoding/json"
"log"
"net/http"
"strconv"
"strings"
"adammathes.com/neko/crawler"
"adammathes.com/neko/exporter"
"adammathes.com/neko/models/feed"
"adammathes.com/neko/models/item"
)
// NewRouter returns a configured mux with all API routes.
func NewRouter() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/stream", HandleStream)
mux.HandleFunc("/item/", HandleItem)
mux.HandleFunc("/feed", HandleFeed)
mux.HandleFunc("/feed/", HandleFeed)
mux.HandleFunc("/tag", HandleCategory)
mux.HandleFunc("/export/", HandleExport)
mux.HandleFunc("/crawl", HandleCrawl)
return mux
}
func jsonError(w http.ResponseWriter, msg string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]string{"error": msg})
}
func jsonResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func HandleStream(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
maxID, _ := strconv.ParseInt(r.FormValue("max_id"), 10, 64)
feedID, _ := strconv.ParseInt(r.FormValue("feed_id"), 10, 64)
// Backward compatibility with feed_url if feed_id is not provided
if feedID == 0 && r.FormValue("feed_url") != "" {
var f feed.Feed
f.ByUrl(r.FormValue("feed_url"))
feedID = f.Id
}
category := r.FormValue("tag")
unreadOnly := r.FormValue("read_filter") != "all"
starredOnly := r.FormValue("starred") == "1" || r.FormValue("starred") == "true"
searchQuery := r.FormValue("q")
if searchQuery != "" {
unreadOnly = false
}
items, err := item.Filter(maxID, feedID, category, unreadOnly, starredOnly, 0, searchQuery)
if err != nil {
log.Println(err)
jsonError(w, "failed to filter items", http.StatusInternalServerError)
return
}
jsonResponse(w, items)
}
func HandleItem(w http.ResponseWriter, r *http.Request) {
idStr := strings.TrimPrefix(r.URL.Path, "/item/")
id, _ := strconv.ParseInt(idStr, 10, 64)
if id == 0 {
jsonError(w, "invalid item id", http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodPut:
var i item.Item
if err := json.NewDecoder(r.Body).Decode(&i); err != nil {
jsonError(w, "invalid json", http.StatusBadRequest)
return
}
i.Id = id
i.Save()
jsonResponse(w, i)
case http.MethodPost, http.MethodGet:
// Full text extraction - supporting GET for backward compatibility
i := item.ItemById(id)
if i == nil {
jsonError(w, "item not found", http.StatusNotFound)
return
}
if i.FullContent == "" {
i.GetFullContent()
}
jsonResponse(w, i)
default:
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func HandleFeed(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
feeds, err := feed.All()
if err != nil {
log.Println(err)
jsonError(w, "failed to fetch feeds", http.StatusInternalServerError)
return
}
jsonResponse(w, feeds)
case http.MethodPost:
var f feed.Feed
if err := json.NewDecoder(r.Body).Decode(&f); err != nil {
jsonError(w, "invalid json", http.StatusBadRequest)
return
}
if f.Url == "" {
jsonError(w, "url required", http.StatusBadRequest)
return
}
err := feed.NewFeed(f.Url)
if err != nil {
jsonError(w, "failed to create feed", http.StatusInternalServerError)
return
}
f.ByUrl(f.Url)
ch := make(chan string)
go func() {
crawler.CrawlFeed(&f, ch)
log.Println(<-ch)
}()
w.WriteHeader(http.StatusCreated)
jsonResponse(w, f)
case http.MethodPut:
var f feed.Feed
if err := json.NewDecoder(r.Body).Decode(&f); err != nil {
jsonError(w, "invalid json", http.StatusBadRequest)
return
}
f.Update()
jsonResponse(w, f)
case http.MethodDelete:
idStr := strings.TrimPrefix(r.URL.Path, "/feed/")
id, _ := strconv.ParseInt(idStr, 10, 64)
if id == 0 {
jsonError(w, "invalid feed id", http.StatusBadRequest)
return
}
f := &feed.Feed{Id: id}
f.Delete()
w.WriteHeader(http.StatusNoContent)
default:
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func HandleCategory(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
categories, err := feed.Categories()
if err != nil {
log.Println(err)
jsonError(w, "failed to fetch categories", http.StatusInternalServerError)
return
}
jsonResponse(w, categories)
}
func HandleExport(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
format := strings.TrimPrefix(r.URL.Path, "/export/")
if format == "" {
jsonError(w, "format required", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain") // exporter handles formats internally
w.Write([]byte(exporter.ExportFeeds(format)))
}
func HandleCrawl(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonError(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
go crawler.Crawl()
jsonResponse(w, map[string]string{"message": "crawl started"})
}
|