2024-09-13 04:38:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
PublishedDate string `json:"publishedDate"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Healthcheck route
|
|
|
|
//
|
|
|
|
// Confirms that the app is alive without guarantees
|
|
|
|
// about it being fully-functional.
|
|
|
|
func healthcheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
// About page
|
|
|
|
//
|
|
|
|
// Static content for the about page.
|
|
|
|
func about(w http.ResponseWriter, r *http.Request) {
|
2024-09-20 05:21:37 +00:00
|
|
|
tmpl, _ := template.New("about").ParseFiles("templates/about.html.tmpl", "templates/base.html.tmpl")
|
|
|
|
tmpl.ExecuteTemplate(w, "base", nil)
|
2024-09-13 04:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Feeds list
|
|
|
|
//
|
|
|
|
// Lists all feed elements in store.
|
|
|
|
func listContent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
links := []Link{}
|
|
|
|
for _, feed := range SharedCache.List("feeds") {
|
|
|
|
var formattedItems []Link
|
|
|
|
json.Unmarshal([]byte(feed), &formattedItems)
|
|
|
|
links = append(links, formattedItems...)
|
|
|
|
}
|
|
|
|
|
2024-09-20 05:21:37 +00:00
|
|
|
tmpl, _ := template.New("base").ParseFiles("templates/index.html.tmpl", "templates/base.html.tmpl")
|
|
|
|
tmpl.ExecuteTemplate(w, "base", links)
|
2024-09-13 04:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manage content
|
|
|
|
//
|
|
|
|
// Interface to add new feed subscriptions and get a list
|
|
|
|
// of current subs.
|
|
|
|
func manageContent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
urlValue := r.PostFormValue("url")
|
|
|
|
|
|
|
|
if _, err := url.Parse(urlValue); err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheKey := fmt.Sprintf("feedurl:%s", urlValue)
|
|
|
|
SharedCache.Set(cacheKey, urlValue)
|
|
|
|
|
|
|
|
go fetchFeed(urlValue)
|
|
|
|
|
|
|
|
w.WriteHeader(201)
|
|
|
|
}
|
|
|
|
|
|
|
|
allFeeds := SharedCache.List("feedurl")
|
|
|
|
|
|
|
|
type ManageTmplData struct {
|
|
|
|
Feeds map[string]string
|
|
|
|
}
|
|
|
|
|
2024-09-20 05:21:37 +00:00
|
|
|
tmpl, _ := template.New("manage").ParseFiles("templates/manage.html.tmpl", "templates/base.html.tmpl")
|
|
|
|
tmpl.ExecuteTemplate(w, "base", ManageTmplData{Feeds: allFeeds})
|
2024-09-13 04:38:46 +00:00
|
|
|
}
|