Skip to content

Commit

Permalink
basic rss feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Pineapple217 committed Mar 24, 2024
1 parent f73316b commit 9f1eb0a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ MB_LOGO=" ▄▄▄·▪ ▐ ▄ ▄▄▄ .\n▐█ ▄███ •█▌▐
MB_RIGHTS=pine32.be
MB_LINK=https://pine32.be
MB_MESSAGE="A funny little cycle."
MB_HOST=https://mb.pine32.be
```

### Compose file
Expand Down Expand Up @@ -87,6 +88,7 @@ docker compose up -d
| `MB_RIGHTS` | Name of the right holder thingy | `mb.dev` just a placeholder |
| `MB_LINK` | Link displayed at the top fo the page | `https://mb.dev` link does not exist |
| `MB_MESSAGE` | Message displayed at the top of the page | `Created without any JS.` |
| `MB_HOST` | The full host url where the blog is hosted. For ex. `https://mb.pine32.be` | `http://localhost:3000` |

# Performance

Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
defaultLink = "https://mb.dev"
defaultRights = "mb.dev"
defaultMessage = "Created without any JS."
defaultHost = "http://localhost:3000"

DataDir = "./data"
)
Expand All @@ -24,6 +25,7 @@ var (
HomepageLink string
HomepageRights string
HomepageMessage string
Host string
)

func Load() {
Expand All @@ -45,6 +47,7 @@ func Load() {
initLink()
initRights()
initMessage()
initHost()
}

func initTimezone() {
Expand Down Expand Up @@ -98,3 +101,12 @@ func initMessage() {
}
HomepageMessage = message
}

func initHost() {
host, isSet := os.LookupEnv("MB_HOST")
if !isSet {
Host = defaultHost
return
}
Host = host
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/Pineapple217/mb/handler"
"github.com/Pineapple217/mb/media"
"github.com/Pineapple217/mb/middleware"
"github.com/Pineapple217/mb/rss"

"github.com/labstack/echo/v4"
echoMw "github.com/labstack/echo/v4/middleware"
Expand Down Expand Up @@ -88,7 +89,7 @@ func main() {
// e.Static("/static", "./static/public")
s.StaticFS("/", echo.MustSubFS(publicFS, "static/public"))

//TODO RSS
e.GET("/index.xml", rss.RSSFeed)

//TODO better caching with http headers

Expand Down
86 changes: 86 additions & 0 deletions rss/rss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package rss

import (
"encoding/xml"
"strconv"
"time"

"github.com/Pineapple217/mb/config"
"github.com/Pineapple217/mb/database"
"github.com/labstack/echo/v4"
)

type RSS struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel Channel
}

type Channel struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Generator string `xml:"generator"`
Language string `xml:"language"`
Copyright string `xml:"copyright"`
Items []Item `xml:"item"`
}

type Item struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Category string `xml:"category"`
}

func RSSFeed(c echo.Context) error {
c.Response().Header().Add("Content-Type", "application/rss+xml")

q := database.GetQueries()
posts, err := q.ListPosts(c.Request().Context())
if err != nil {
return err
}
rssPosts := make([]Item, 0)
for _, post := range posts {
unixTime := strconv.FormatInt(post.CreatedAt, 10)
p := Item{
Title: unixTime,
Link: config.Host + "/post/" + unixTime,
PubDate: time.Unix(post.CreatedAt, 0).Format(time.RFC1123Z),
Category: post.Tags.String,
Description: truncateString(post.Content),
}
rssPosts = append(rssPosts, p)
}

feed := RSS{
Version: "2.0",
Channel: Channel{
Title: "Micro Blog of " + config.HomepageRights,
Description: config.HomepageMessage,
Link: config.Host + "/index.xml",
Items: rssPosts,
Generator: "Golang",
Language: "en-uk",
Copyright: "Copyright " + strconv.Itoa(time.Now().Year()) + ", " + config.HomepageRights,
},
}
xmlData, err := xml.MarshalIndent(feed, "", " ")
if err != nil {
return err
}
c.Response().Write([]byte(xml.Header))
c.Response().Write(xmlData)
return nil
}

const descriptionMaxLength = 500

func truncateString(s string) string {
if len(s) <= descriptionMaxLength {
return s
}
return s[:descriptionMaxLength] + "..."
}

0 comments on commit 9f1eb0a

Please sign in to comment.