@@ -7,13 +7,18 @@ import (
7
7
"os"
8
8
"strings"
9
9
10
+ "embed"
11
+
10
12
"github.com/PuerkitoBio/goquery"
11
13
"github.com/gofiber/fiber/v2"
12
14
"github.com/mmcdole/gofeed"
13
15
)
14
16
17
+ //go:embed index.html
18
+
19
+ var content embed.FS
20
+
15
21
func main () {
16
- fp := gofeed .NewParser ()
17
22
port , ok := os .LookupEnv ("PORT" )
18
23
if ! ok {
19
24
port = "8080"
@@ -24,56 +29,32 @@ func main() {
24
29
app .Get ("/" , func (c * fiber.Ctx ) error {
25
30
feedUrl := c .Query ("url" )
26
31
if feedUrl == "" {
27
- fmt .Println ("No URL provided" )
28
- return c .SendStatus (fiber .StatusBadRequest )
32
+ c .Type ("html" , "UTF8" )
33
+ body , _ := content .ReadFile ("index.html" )
34
+ return c .Send (body )
29
35
}
30
36
31
- _ , err := fp .ParseURL (feedUrl )
32
- if err != nil && err == gofeed .ErrFeedTypeNotDetected {
33
- res , err := http .Get (feedUrl )
34
- if err != nil {
35
- fmt .Println ("Failed to fetch URL" )
36
- return c .SendStatus (fiber .StatusInternalServerError )
37
- }
38
- defer res .Body .Close ()
39
- if res .StatusCode >= 400 {
40
- fmt .Println ("Provided URL returned an error status code" )
41
- return c .SendStatus (res .StatusCode )
42
- }
37
+ feeds , statusCode := getFeeds (feedUrl )
43
38
44
- doc , err := goquery .NewDocumentFromReader (res .Body )
45
- if err != nil {
46
- fmt .Println ("Failed to parse response body" )
47
- return c .SendStatus (fiber .StatusInternalServerError )
39
+ if c .Is ("json" ) {
40
+ if statusCode >= 400 || statusCode == 300 {
41
+ c .Status (statusCode )
48
42
}
43
+ return c .JSON (feeds )
44
+ } else {
45
+ c .Status (statusCode )
46
+ c .Location (feeds [0 ])
49
47
50
- matches := doc .Find (`[rel="alternate"][type="application/rss+xml"]` )
51
- if matches .Length () == 0 {
52
- fmt .Println ("No RSS feeds found on page" )
53
- return c .SendStatus (fiber .StatusNotFound )
48
+ if len (feeds ) > 1 {
49
+ responseBody := "Multiple Choices\n \n "
50
+ for _ , feed := range feeds {
51
+ responseBody += feed + "\n "
52
+ }
53
+ return c .SendString (responseBody )
54
54
}
55
55
56
- foundUrl , ok := matches .First ().Attr ("href" )
57
- if ! ok {
58
- fmt .Println ("href attribute missing from tag" )
59
- return c .SendStatus (fiber .StatusNotFound )
60
- }
61
- c .Set ("Location" , absoluteUrl (feedUrl , foundUrl ))
62
- if matches .Length () > 1 {
63
- fmt .Println ("Multiple feeds found on page" )
64
- return c .SendStatus (fiber .StatusMultipleChoices )
65
- } else {
66
- fmt .Println ("Feed found on page" )
67
- return c .SendStatus (fiber .StatusTemporaryRedirect )
68
- }
69
- } else if err != nil {
70
- fmt .Println ("Failed while attempting to parse feed" )
71
- return c .SendStatus (fiber .StatusInternalServerError )
56
+ return c .Send (nil )
72
57
}
73
-
74
- fmt .Println ("URL provided is already a feed" )
75
- c .Set ("Location" , feedUrl )
76
- return c .SendStatus (fiber .StatusMovedPermanently )
77
58
})
78
59
79
60
fmt .Println (app .Listen (fmt .Sprintf (":%s" , port )))
@@ -87,3 +68,54 @@ func absoluteUrl(requestUrl, foundUrl string) string {
87
68
88
69
return foundUrl
89
70
}
71
+
72
+ func getFeeds (requestURL string ) ([]string , int ) {
73
+ feeds := []string {}
74
+
75
+ fp := gofeed .NewParser ()
76
+ _ , err := fp .ParseURL (requestURL )
77
+ if err == nil {
78
+ feeds = []string {requestURL }
79
+ } else if err != nil && err == gofeed .ErrFeedTypeNotDetected {
80
+ res , err := http .Get (requestURL )
81
+ if err != nil {
82
+ fmt .Println ("Failed to fetch URL" )
83
+ return feeds , fiber .StatusInternalServerError
84
+ }
85
+ defer res .Body .Close ()
86
+
87
+ if res .StatusCode >= 400 {
88
+ fmt .Println ("Provided URL returned an error status code" )
89
+ return feeds , res .StatusCode
90
+ }
91
+
92
+ doc , err := goquery .NewDocumentFromReader (res .Body )
93
+ if err != nil {
94
+ fmt .Println ("Failed to parse response body" )
95
+ return feeds , fiber .StatusInternalServerError
96
+ }
97
+
98
+ matches := doc .Find (`[rel="alternate"][type="application/rss+xml"]` )
99
+ if matches .Length () == 0 {
100
+ fmt .Println ("No RSS feeds found on page" )
101
+ return feeds , fiber .StatusNotFound
102
+ }
103
+
104
+ matches .Each (func (i int , s * goquery.Selection ) {
105
+ feeds = append (feeds , absoluteUrl (requestURL , s .AttrOr ("href" , "" )))
106
+ })
107
+
108
+ if matches .Length () > 1 {
109
+ fmt .Println ("Multiple feeds found on page" )
110
+ return feeds , fiber .StatusMultipleChoices
111
+ } else {
112
+ fmt .Println ("Feed found on page" )
113
+ return feeds , fiber .StatusTemporaryRedirect
114
+ }
115
+ } else if err != nil {
116
+ fmt .Println ("Failed while attempting to parse feed" )
117
+ return feeds , fiber .StatusInternalServerError
118
+ }
119
+
120
+ return feeds , 200
121
+ }
0 commit comments