getSnapletUserConfig

5 views
Skip to first unread message

Kevin Jardine

unread,
Oct 29, 2011, 5:23:29 PM10/29/11
to sn...@snapframework.com
Dear all,

I'm trying to write a page handler that dumps the application's config file to a web page.

I understand that the config file should be stored in myapp/snaplet.cfg

My function is currently:

renderConfigDump :: Html
renderConfigDump = htmlTemplate False $ do
  c <- getSnapletUserConfig
  H.h1  "Configuration"
  H.p   $ show c

dumpConfigHandler :: AppHandler ()
dumpConfigHandler = do
    c <- renderConfigDump
    blaze c


and the error I get is:

 No instance for (Monad (m0 b0 v0))
      arising from a use of `getSnapletUserConfig'

Is the fix just a matter of fiddling with the type signature of renderConfigDump (and if so what should it be)
or do I have some deeper conceptual problem with how getSnapletUserConfig should be used?

I expect the latter. I suspect that Config may not have a Show instance but that bit I can probably solve if I can somehow get unstuck from the Snap monad.

Kevin

MightyByte

unread,
Oct 29, 2011, 10:54:44 PM10/29/11
to Kevin Jardine, sn...@snapframework.com
I'm not completely sure what's going on here, but if Html is the data
type from blaze-html, then it makes no sense to be calling
getSnapletUserConfig from inside that monad. getSnapletUserConfig
runs in a MonadSnaplet, of which there are only three instances that I
am aware of: Initializer, Handler, and SnapletHeist. You should call
getSnapletUserConfig from dumpConfigHandler, and pass the necessary
pieces into renderConfigDump.

You could also do it the other way and change the type signature to
renderConfigDump :: AppHandler Html and then change it accordingly.

Kevin Jardine

unread,
Oct 30, 2011, 3:06:10 AM10/30/11
to MightyByte, sn...@snapframework.com
Unfortunately changing the type signature of renderConfigDump to:

renderConfigDump :: AppHandler Html

gives me the same type error.

I'll try to restructure my code so that getSnapletUserConfig is called in dumpConfigHandler instead.

Kevin

Kevin Jardine

unread,
Oct 30, 2011, 5:11:21 AM10/30/11
to MightyByte, sn...@snapframework.com
Just for the record, the code that worked was:

import qualified  Data.Text as DT
import qualified  Data.Configurator as DC


renderConfigDump :: String -> Html
renderConfigDump c = htmlTemplate False $ do
  H.h1  "Configuration"
  H.p   $ toHtml (c :: String)


dumpConfigHandler :: AppHandler ()
dumpConfigHandler = do

    c <- getSnapletUserConfig
    s <- liftIO $ DC.getMap c
    blaze $ renderConfigDump $ show s

As is usual in Haskell, everything works if you know where to call the functions you need and throw in the occasional lift.


It turns out that the config structure not only contains all values for the top level application configuration but also for all the nested snaplets with qualified keys for the nested snaplets. Eg., if a nested snaplet is mynestedsnaplet then the top level config will also contain values for mynestedsnaplet.key1, mynestedsnaplet.key2 etc.

Very convenient!

Reply all
Reply to author
Forward
0 new messages