The yesod-gitrepo package

[Tags:library, mit]


[Skip to Readme]


Change log
Dependencies base (<5), directory, enclosed-exceptions, http-types, lifted-base, process (>=1.1), temporary (>=1.2), text, wai (>=3.0), yesod-core (>=1.2 && <1.5) [details]
License MIT
Author Michael Snoyman
Stability Unknown
Category Web
Home page
Uploaded Thu May 14 08:39:51 UTC 2015 by MichaelSnoyman
Distributions LTSHaskell:, NixOS:, Stackage:, Tumbleweed:
Downloads 840 total (9 in the last 30 days)
0 []
Status Docs available [build log]
Last success reported on 2015-05-14 [all 1 reports]




Maintainer's Corner

For package maintainers and hackage trustees

Readme for yesod-gitrepo

Readme for yesod-gitrepo-


yesod-gitrepo provides a means of embedding content from a Git repository inside a Yesod application. The typical workflow is:

  • Use gitRepo to specify a repository and branch you want to work with.
  • Provide a function that will perform some processing on the cloned repository.
  • Use grContent in your Handler functions to access this parsed data.
  • Embed the GitRepo as a subsite that can be used to force a refresh of the data.
  • Set up a commit handler that pings that URL. On Github, this would be a webhook.

This is likely easiest to understand with a concrete example, so let's go meta: here's an application that will serve this very file. We'll start off with language extensions and imports:

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeFamilies      #-}
import           ClassyPrelude.Yesod
import           Text.Markdown
import           Yesod.GitRepo

Now we're going to create our foundation datatype. We need to give it one field: the GitRepo value containing our parsed content. Our content will simply be the text inside, wrapped up in a Markdown newtype for easy rendering. This gives us:

data App = App
    { getGitRepo :: GitRepo Markdown

instance Yesod App

And now let's set up our routes. We need just two: a homepage, and the subsite. Our subsite type is GitRepo Markdown (as given above). We replace the space with a hyphen as an escaping mechanism inside Yesod's route syntax:

mkYesod "App" [parseRoutes|
/ HomeR GET
/refresh RefreshR GitRepo-Markdown getGitRepo

Next up is our home handler. We start off by getting the current content parsed from the repository:

getHomeR :: Handler Html
getHomeR = do
    master <- getYesod
    content <- liftIO $ grContent $ getGitRepo master

Then it's just some normal Hamlet code:

    defaultLayout $ do
        setTitle "yesod-gitrepo sample"
                <a href=@{RefreshR GitRepoRoute}>
                    Force a refresh at
                    @{RefreshR GitRepoRoute}

And finally, our main function. We pass in the repo URL and branch name, plus a function that, given the filepath containing the cloned repo, processes it and generates a Markdown value. Finally, we provide the generated repo value to our App constructor and run it:

main :: IO ()
main = do
    repo <- gitRepo
        $ \fp -> fmap Markdown $ readFile $ fp </> ""
    warp 3000 $ App repo

Give it a shot. You should have a webapp hosting this very README file!