Hi there,

First of all, if you haven't already done so I would recommend
checking out the snap-website package on github [0].  That provides an
example of how we've used Snap and Heist on a real site.  It doesn't
use a database, but it should give you some semblance of a starting
point.

I should interject here that some of the more generic boilerplate code
you see there (and in the template created by snap init) will be
merged into Snap in the future, so you won't always have to carry that
along with your app.

Using monad transformers and your own database will get a lot easier
in Snap 0.3.  It's in development right now and we're planning to
release it in January.  So if your project can tolerate working on a
development branch I'd suggest getting it from github and basing your
work around that.  (Then you also won't have to worry about migrating
after we do release.)

If you decide to use 0.3 then I would also suggest using duairc's
snap-extensions package.  It's not on hackage, but you can get it from
github [1].  It provides some infrastructure for working with Snap 0.3
 This code will be merged into the snap package before the 0.3
release.  Assuming you use snap-0.3 + snap-extensions, here's what
some of your app might look like.

data AppState = AppState {
  dbConnection :: Connection
}

withDB f = f =<< asks dbConnection

trivialHandler :: SnapExtend AppState
trivialHandler = do
  num <- withDB (liftIO trivialQuery)
  writeBS $ pack $ show num

I haven't tried to compile this, but it should give you the idea.  The
SnapExtend monad (provided by snap-extensions) is just a reader monad
that is also an instance of MonadSnap (only available in Snap 0.3).

Hope this helps.

[0] https://github.com/snapframework/snap-website
[1] https://github.com/duairc/snap-extensions

On Fri, Dec 3, 2010 at 1:32 AM, Daniel Lyons <[email protected]> wrote:
> Hi,
>
> I'm a somewhat proficient Haskell programmer, but I'm having trouble seeing 
> the forest for the trees with Snap. My needs are pretty modest and I think 
> Snap will be perfect. All I need is to figure out how to set up Snap with 
> Heist and an HDBC connection. Unfortunately, this is my first exposure to 
> iteratees (though I think I grasp the concept) and my first real use of monad 
> transformers, which I think is part of the problem, plus Snap itself is so 
> new.
>
> I'd greatly appreciate just a little assistance getting a trivial Snap webapp 
> going, doing nothing more than rendering a bit of Heist backed by a trivial 
> database query. I think I can handle it from there if I can just get that set 
> up.
>
> Starting with this trivial program:
>
>    module Main where
>
>    import Database.HDBC
>    import Database.HDBC.PostgreSQL
>
>    trivialQuery ∷ Connection → IO Integer
>    trivialQuery db = do
>      [[SqlInteger res]] ← quickQuery db "SELECT COUNT(*) FROM foo" []
>      return res
>
>    main = do
>      db ← connectPostgreSQL "user=fusion dbname=fusion"
>      count ← trivialQuery db
>      putStrLn $ show count
>
> I'm having some trouble seeing how to 1) make a tag that represents 
> trivialQuery, and 2) call it from a template, 3) with Snap serving it.
>
> I must apologize for how basic this is! I don't know why I'm having so much 
> trouble. Thanks for your help!
>
> --
> Daniel Lyons
>
> _______________________________________________
> Snap mailing list
> [email protected]
> http://mailman-mail5.webfaction.com/listinfo/snap
>
_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap

Reply via email to