79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
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) {
|
|
tmpl, _ := template.New("about").ParseFiles("templates/about.html.tmpl", "templates/base.html.tmpl")
|
|
tmpl.ExecuteTemplate(w, "base", nil)
|
|
}
|
|
|
|
// 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...)
|
|
}
|
|
|
|
tmpl, _ := template.New("base").ParseFiles("templates/index.html.tmpl", "templates/base.html.tmpl")
|
|
tmpl.ExecuteTemplate(w, "base", links)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
tmpl, _ := template.New("manage").ParseFiles("templates/manage.html.tmpl", "templates/base.html.tmpl")
|
|
tmpl.ExecuteTemplate(w, "base", ManageTmplData{Feeds: allFeeds})
|
|
}
|