diff --git a/.air.toml b/.air.toml index 98b17b0..f065b5b 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "tmp\\main.exe" -cmd = "go build -o ./tmp/main.exe ./cmd/feedvault/" +cmd = "go build -o ./tmp/main.exe ." delay = 1000 exclude_dir = [ "assets", @@ -15,7 +15,6 @@ exclude_dir = [ "testdata", "tests", ".git", - ".idea", ".vscode", ] exclude_file = [] diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index e62d732..c821317 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -33,4 +33,4 @@ jobs: run: go mod tidy - name: Build - run: go build -v -ldflags="-s -w" .\cmd\feedvault\ + run: go build -v -ldflags="-s -w" . diff --git a/pkg/feeds/feeds.go b/feeds.go similarity index 97% rename from pkg/feeds/feeds.go rename to feeds.go index 95b05e1..48516c5 100644 --- a/pkg/feeds/feeds.go +++ b/feeds.go @@ -1,4 +1,4 @@ -package feeds +package main import ( "context" diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..b97d23a --- /dev/null +++ b/handlers.go @@ -0,0 +1,208 @@ +package main + +import ( + "io" + "log" + "net/http" + + "strings" +) + +func ApiHandler(w http.ResponseWriter, _ *http.Request) { + htmlData := HTMLData{ + Title: "FeedVault API", + Description: "FeedVault API - A feed archive", + Keywords: "RSS, Atom, Feed, Archive, API", + Author: "TheLovinator", + CanonicalURL: "http://localhost:8000/api", + Content: "

Here be dragons.

", + } + html := FullHTML(htmlData) + w.Write([]byte(html)) +} + +func FeedsHandler(w http.ResponseWriter, _ *http.Request) { + htmlData := HTMLData{ + Title: "FeedVault Feeds", + Description: "FeedVault Feeds - A feed archive", + Keywords: "RSS, Atom, Feed, Archive", + Author: "TheLovinator", + CanonicalURL: "http://localhost:8000/feeds", + Content: "

Here be

", + } + html := FullHTML(htmlData) + w.Write([]byte(html)) +} + +func AddFeedHandler(w http.ResponseWriter, r *http.Request) { + var parseErrors []ParseResult + + // Parse the form and get the URLs + err := r.ParseForm() + if err != nil { + http.Error(w, "Error parsing form", http.StatusInternalServerError) + return + } + + urls := r.Form.Get("urls") + if urls == "" { + http.Error(w, "No URLs provided", http.StatusBadRequest) + return + } + + for _, feed_url := range strings.Split(urls, "\n") { + // TODO: Try to upgrade to https if http is provided + + // Validate the URL + err := ValidateFeedURL(feed_url) + if err != nil { + parseErrors = append(parseErrors, ParseResult{FeedURL: feed_url, Msg: err.Error(), IsError: true}) + continue + } + + err = AddFeedToDB(feed_url) + if err != nil { + parseErrors = append(parseErrors, ParseResult{FeedURL: feed_url, Msg: err.Error(), IsError: true}) + continue + } + + // Feed was added successfully + parseErrors = append(parseErrors, ParseResult{FeedURL: feed_url, Msg: "Added", IsError: false}) + + } + htmlData := HTMLData{ + Title: "FeedVault", + Description: "FeedVault - A feed archive", + Keywords: "RSS, Atom, Feed, Archive", + Author: "TheLovinator", + CanonicalURL: "http://localhost:8000/", + Content: "

Feeds added.

", + ParseResult: parseErrors, + } + + html := FullHTML(htmlData) + w.Write([]byte(html)) +} + +func IndexHandler(w http.ResponseWriter, _ *http.Request) { + + content := `

Feeds to archive

+

+ Input the URLs of the feeds you wish to archive below. You can add as many as needed, and access them through the website or API. Alternatively, include links to .opml files, and the feeds within will be archived. +

+
+ + +
+
+

You can also upload .opml files containing the feeds you wish to archive:

+
+ + +
+ ` + + FAQ := ` + +

FAQ

+
+ What are web feeds? +

+ Web feeds are a way to distribute content on the web. They allow users to access updates from websites without having to visit them directly. Feeds are typically used for news websites, blogs, and other sites that frequently update content. +
+ You can read more about web feeds on Wikipedia. +

+
+
+
+ What is FeedVault? +

+ FeedVault is a service that archives web It allows users to access and search for historical content from various websites. The service is designed to preserve the history of the web and provide a reliable source for accessing content that may no longer be available on the original websites. +

+
+
+
+ Why archive feeds? +

+ Web feeds are a valuable source of information, and archiving them ensures that the content is preserved for future reference. By archiving feeds, we can ensure that historical content is available for research, analysis, and other purposes. Additionally, archiving feeds can help prevent the loss of valuable information due to website changes, outages, or other issues. +

+
+
+
+ How does it work? +

+ FeedVault is written in Go and uses the gofeed library to parse The service periodically checks for new content in the feeds and stores it in a database. Users can access the archived feeds through the website or API. +


+
+
+ How can I access the archived feeds? +

+ You can access the archived feeds through the website or API. The website provides a user interface for searching and browsing the feeds, while the API allows you to access the feeds programmatically. You can also download the feeds in various formats, such as JSON, XML, or RSS. +

+
+ ` + + content += FAQ + + htmlData := HTMLData{ + Title: "FeedVault", + Description: "FeedVault - A feed archive", + Keywords: "RSS, Atom, Feed, Archive", + Author: "TheLovinator", + CanonicalURL: "http://localhost:8000/", + Content: content, + } + html := FullHTML(htmlData) + w.Write([]byte(html)) +} + +func UploadOpmlHandler(w http.ResponseWriter, r *http.Request) { + // Parse the form and get the file + r.ParseMultipartForm(10 << 20) // 10 MB + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "No file provided", http.StatusBadRequest) + return + } + defer file.Close() + + // Read the file + all, err := io.ReadAll(file) + if err != nil { + http.Error(w, "Failed to read file", http.StatusInternalServerError) + return + } + // Parse the OPML file + parseResult := []ParseResult{} + links, err := ParseOpml(string(all)) + if err != nil { + parseResult = append(parseResult, ParseResult{FeedURL: "/upload_opml", Msg: err.Error(), IsError: true}) + } else { + // Add the feeds to the database + for _, feed_url := range links.XMLLinks { + log.Println("Adding feed:", feed_url) + + // Validate the URL + err := ValidateFeedURL(feed_url) + if err != nil { + parseResult = append(parseResult, ParseResult{FeedURL: feed_url, Msg: err.Error(), IsError: true}) + continue + } + + parseResult = append(parseResult, ParseResult{FeedURL: feed_url, Msg: "Added", IsError: false}) + } + } + + // Return the results + htmlData := HTMLData{ + Title: "FeedVault", + Description: "FeedVault - A feed archive", + Keywords: "RSS, Atom, Feed, Archive", + Author: "TheLovinator", + CanonicalURL: "http://localhost:8000/", + Content: "

Feeds added.

", + ParseResult: parseResult, + } + html := FullHTML(htmlData) + w.Write([]byte(html)) +} diff --git a/tests/views_test.go b/handlers_test.go similarity index 93% rename from tests/views_test.go rename to handlers_test.go index 5a8b0a2..ff8a053 100644 --- a/tests/views_test.go +++ b/handlers_test.go @@ -5,8 +5,6 @@ import ( "net/http/httptest" "strings" "testing" - - "github.com/TheLovinator1/FeedVault/pkg/handlers" ) func TestIndexHandler(t *testing.T) { @@ -18,7 +16,7 @@ func TestIndexHandler(t *testing.T) { // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. rr := httptest.NewRecorder() - handler := http.HandlerFunc(handlers.IndexHandler) + handler := http.HandlerFunc(IndexHandler) // Our handlers satisfy http.Handler, so we can call their ServeHTTP method // directly and pass in our Request and ResponseRecorder. @@ -48,7 +46,7 @@ func TestApiHandler(t *testing.T) { // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. rr := httptest.NewRecorder() - handler := http.HandlerFunc(handlers.ApiHandler) + handler := http.HandlerFunc(ApiHandler) // Our handlers satisfy http.Handler, so we can call their ServeHTTP method // directly and pass in our Request and ResponseRecorder. diff --git a/pkg/html/html.go b/html.go similarity index 91% rename from pkg/html/html.go rename to html.go index 2a0a45a..e60f3ab 100644 --- a/pkg/html/html.go +++ b/html.go @@ -1,14 +1,10 @@ -package html +package main import ( "fmt" "log" "math/rand" "strings" - - "github.com/TheLovinator1/FeedVault/pkg/models" - "github.com/TheLovinator1/FeedVault/pkg/quotes" - "github.com/TheLovinator1/FeedVault/pkg/stats" ) type HTMLData struct { @@ -18,7 +14,7 @@ type HTMLData struct { Author string CanonicalURL string Content string - ParseResult []models.ParseResult + ParseResult []ParseResult } var style = ` @@ -150,7 +146,7 @@ const ( ` ) -func buildErrorList(parseResults []models.ParseResult) string { +func buildErrorList(parseResults []ParseResult) string { var errorBuilder strings.Builder if len(parseResults) > 0 { errorBuilder.WriteString("