haskellcastingyesodblaze-html

Yesod custom settings.yml field type mismatch


I'm trying to create a new field in my settings.yml file for my static file's locations (so I can change from a local subdirectory in development to a CDN in production), but I can't seem to get a basic "hello world" going. Here is my Settings.hs:

data Extra = Extra
    { extraCopyright :: Text
    , extraAnalytics :: Maybe Text -- ^ Google Analytics
    , extraStatic :: Text
    } deriving Show

parseExtra :: DefaultEnv -> Object -> Parser Extra
parseExtra _ o = Extra
    <$> o .:  "copyright"
    <*> o .:? "analytics"
    <*> o .:  "static"

And here is the relevent part of settings.yml:

Default: &defaults
  host: "*4" # any IPv4 host
  port: 3000
  approot: "http://localhost:3000"
  copyright: Insert copyright statement here
  #analytics: UA-YOURCODE
  static: "/static"

And lastly, the part that throws the error. I didn't really know how I should've gone about including it's value in a request, so I just threw the code in to be executed on each request:

getHomeR :: Handler Html
getHomeR = do
    (formWidget, formEnctype) <- generateFormPost sampleForm
    let submission = Nothing :: Maybe (FileInfo, Text)
        handlerName = "getHomeR" :: Text
        test = fmap extraStatic getExtra
    defaultLayout $ do
        aDomId <- newIdent
        setTitle "Welcome To Yesod!"
        $(widgetFile "homepage")

And I have the same line added on postHomeR aswell (test = fmap extraStatic getExtra). My hamlet:

<p>#{test}

And finally, the error thrown on the screen:

Handler/Home.hs:37:11:
    No instance for (blaze-markup-0.5.1.5:Text.Blaze.ToMarkup
                       (HandlerT App IO Text))
      arising from a use of `toHtml'
    Possible fix:
      add an instance declaration for
      (blaze-markup-0.5.1.5:Text.Blaze.ToMarkup (HandlerT App IO Text))
    In the first argument of `Yesod.Core.Widget.asWidgetT
                              . toWidget', namely
       `toHtml test'
    In a stmt of a 'do' block:
      (Yesod.Core.Widget.asWidgetT . toWidget) (toHtml test)
    In a stmt of a 'do' block:
      do { (Yesod.Core.Widget.asWidgetT . toWidget)
             ((blaze-markup-0.5.1.5:Text.Blaze.Internal.preEscapedText
               . Data.Text.pack)
                "<h1>");
           ((Control.Monad.liftM (toHtml .) getMessageRender)
            >>=
               (\ urender_aaYh
                 -> (Yesod.Core.Widget.asWidgetT . toWidget)
                       (urender_aaYh MsgHello)));
          (Yesod.Core.Widget.asWidgetT . toWidget)
             ((blaze-markup-0.5.1.5:Text.Blaze.Internal.preEscapedText
               . Data.Text.pack)
                "</h1>\
                \<ol><li>Now that you have a working project you should use the <a href=\"http://www.yesodweb.com/book/\">Yesod book</a> to learn more. You can also use this scaffolded site to explore some basic concepts.</li><li> This page was generated by the ");
           (Yesod.Core.Widget.asWidgetT . toWidget) (toHtml handlerName);
           .... }
Build failure, pausing...

Is there any way I can cast it? Is there any way I can make the variable get assigned and loaded during app startup, but be accessible throughout the requests as a constant value? Any help would be greatly appreciated!!


Solution

  • The problem is:

    let test = fmap extraStatic getExtra
    

    getExtra is a Handler action, so what you want is:

    test <- fmap extraStatic getExtra