Yesod – Feed The Beast


today I’ve added an atom and RSS feed to this weblog. For those of you who don’t know, the whole page is written in Haskell using the Yesod Web Framework. It’s actually very easy to do but not well documented. That’s why I’ve decided to show you how to do it so you can use it for your own purpose.

First you need yesod-newsfeed (same version as your yesod installation); just add it to your *.cabal file. Using yesod add-handler your can create a new blank handler that will serve as the feed.

Now you’ve got a choice between RSS and atom. I’ll stick to a general news feed, this way both RSS and atom are provided and the client can ask for a specific protocol. Also the handler will be called Feed with its route FeedR and will serve as a feed for Weblog with its route WeblogR. The posts are served in config/models:

    title       Text
    post        Html
    date        UTCTime

So, here is the handler:

module Handler.Feed where

import Import
import Yesod.Feed

getFeedR :: Handler TypedContent
getFeedR = do
        blogPosts <- runDB $ selectList [] [Desc BlogPostDate]
        let myFeedName = "Title"
            myFeedAuthor = "Author"
            myFeedDescription = toHtml $ "description"
            myFeedLanguage = "en"
            myFeedEntries = map entry2feed blogPosts
            myFeedUpdate = getDate $ head blogPosts
            feed = Feed myFeedName FeedR WeblogR myFeedAuthor myFeedDescription myFeedLanguage myFeedUpdate myFeedEntries
        newsFeed feed
        entry2feed (Entity blogPostId blogPost) =
            FeedEntry (ReadBlogPostR blogPostId) (blogPostDate blogPost)
                (blogPostTitle blogPost) (blogPostPost blogPost)
        getDate (Entity _ blogPost) = blogPostDate blogPost
        head [] = undefined
        head (a:_) = a

First there are the module definition and imports. The actual GET function starts by pulling the posts from the database. Inside the let scope there are definitions for title, author, description and language; I’d recommend using Yesod.Default.Config to be able to set those inside of your config file. The feed entries are composed by entry2feed which just takes the data fields from BlogPost and creates a FeedEntry out of them. getDate just returns the date of an entry. In this case the first entry of blogPosts will always be the latest since it’s pulled with BlogPostDate descending. head is defined manually to save an import.

As you can see there’s not much to it even if the almost non-existent documentation is pretty cryptic. The only thing you have to keep in mind is that you have to change the type of getFeedR from Handler Html to Handler TypedContent.

To link the feed on your weblog, just add toWidgetHead [hamlet|<link href="@{FeedR}" rel="alternate" title="Atom feed" type="application/atom+xml">|] to getWeblogR.

I’m still not sure if I want to add a comment feature to this website. It sure would be interesting to see your suggestions and opinions on my posts but my experience with comments is kind of disappointing. I’ll think about it.


06.03.2015 00:51:25 UTC

return flattr


13.04.2019 13:20:20 UTC

дверь из панелей пвх дверь металлический устанавливать фурнитура для дверей входных входные двери дешевые дверь входная деревянная витебске монтаж окон дверь складная межкомнатная гармошка установка межкомнатных дверей в ванной двери дешево купить продажа входных дверей киев

21.03.2019 11:12:21 UTC

входные двери в купить в входные двери молодечно каталог входные двухстворчатые двери в частный металлические двери с браком минские входные двери замки дверные купить двери из дуба межкомнатные двери в рассрочку раздвижная дверь межкомнатная одностворчатая двери входные металлические воронеж цены

02.01.2019 19:54:21 UTC

tmz voice over guy dating kelly local dating apps android dating a divorced man with a baby

what to expect dating scan what is a great dating profile friend dating someone you like christian dating personality test dating sites android apps

schwinn bicycle dating dating age laws in tn