html render

master
2577 3 years ago
parent df1c36bd13
commit 3f941e8134
  1. 19
      feed.go
  2. 23
      index.html
  3. 16
      main.go

@ -189,6 +189,25 @@ type Item struct {
GUID string `xml:"guid"`
}
func (f feed) items() []feedItem {
var items = []feedItem{}
f.bolt.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(feedBucket)
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var item feedItem
err := json.Unmarshal(v, &item)
if err != nil {
log.Printf("Error reading feed bolt bucket: %v", err)
continue
}
items = append(items, item)
}
return nil
})
return items
}
func (f feed) rss() string {
channel := Channel{
Title: "Farenheit 2577",

@ -0,0 +1,23 @@
<html>
<head>
<title>Fahrenheit 2577</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="alternative" type="application/rss+xml" href="feed.xml">
</head>
<body>
<h1>The temperature at which silicon burns...</h1>
<img src="https://sindominio.net/2577/2577.png" alt="2577" />
<a href="https://sindominio.net/2577/feed.xml">RSS feed</a>
<h2>The latest news from our dystopian present</h2>
<article>
{{range $item := .}}
<h3><a href="{{ $item.URL }}">{{ $item.Title }}</a></h3>
<p>{{ $item.Date.Format "2006 Jan 02" }} - {{ $item.URL }}</p>
{{end}}
</article>
</body>
</html>

@ -2,6 +2,7 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"net/http"
@ -18,7 +19,20 @@ func (s *serve) rssHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(s.feed.rss()))
}
func (s *serve) htmlHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("Serve html")
items := s.feed.items()
index := template.Must(template.ParseFiles("index.html"))
index.Execute(w, items)
log.Printf("Rendered %v items", len(items))
}
func (s *serve) postHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
s.htmlHandler(w, req)
return
}
if req.Method != "POST" || !strings.Contains(req.URL.Path, s.token) {
log.Printf("Invalid request (%s): %v", req.Method, req.URL)
http.NotFound(w, req)
@ -32,7 +46,7 @@ func (s *serve) postHandler(w http.ResponseWriter, req *http.Request) {
}
err = s.feed.add(string(buff))
if err != nil {
log.Printf("Error adding to beed: %v", err)
log.Printf("Error adding entry to feed: %v", err)
} else {
log.Printf("Added url to the queue: %s", string(buff))
}

Loading…
Cancel
Save