Discussion:
happs -> happstack?
Martin Clausen
2016-03-12 12:27:36 UTC
Permalink
I am new to happs(tack) and Haskell in general, and could need some help
converting a very simple happs example to happstack.

I have a SO question here
<http://stackoverflow.com/questions/35800990/how-to-convert-from-happs-happstack> which
has the relevant code. It has been up for some time without a viable answer
and I was hoping someone on this list might be able to help.

Cheers

Martin
--
You received this message because you are subscribed to the Google Groups "HAppS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to happs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/happs.
For more options, visit https://groups.google.com/d/optout.
Jeremy Shaw
2016-03-15 02:46:54 UTC
Permalink
Hello,

I suspect this is part of the problem. In the original you had:

main :: IO ()
main = do simpleHTTP [dir "contractEx"
[withData $ \(ExContr t) ->
[anyRequest $ liftIO $ liftM toResponse =<<
renderEx (ExContr t)]
,anyRequest $ ok $ toResponse renderExDefault]
,fileServe ["Contracts.html"] "public" -- fileserving


]

Note that the 'withData' function takes a list of handlers -- and in that
list there is only one handler.

The withData route will only match if valid data is submitted. If that
route fails to match, then the next route is tried. In this case the next
route is the second anyRequest route which will just render the default
renderExDefault.

In the Happstack version:

main :: IO ()
main = do
simpleHTTP (nullConf { port = 80 }) $ msum [
dir "contractEx" $ withData $ \(ExContr t) -> msum $ [
anyRequest $ fmap toResponse $ liftIO $ renderEx (ExContr t)
, anyRequest $ toResponse renderExDefault
]
, serveDirectory DisableBrowsing ["Contracts.html"] "public"
]

Both anyRequest routes are guarded by the 'withData' function. So, neither
will be called unless there is valid data. Additionally, since the first
anyRequest line should always match, the second anyRequest will never be
called.

The could should probably be something like:

main :: IO ()
main = do
simpleHTTP (nullConf { port = 80 }) $ msum [
dir "contractEx" $ msum $ [
withData $ \(ExContr t) -> anyRequest $ fmap
toResponse $ liftIO $ renderEx (ExContr t)

, anyRequest $ toResponse renderExDefault
]
, serveDirectory DisableBrowsing ["Contracts.html"] "public"
]


So that withData is only used to guard the first anyRequest.

You can read more about routing in this chapter:

http://www.happstack.com/docs/crashcourse/index.html#route-filters

The FromData stuff is not used that commonly anymore. You can read about a
more modern method of handle request data in this chapter:

http://www.happstack.com/docs/crashcourse/index.html#parsing-request-data-from-the-query_string-cookies-and-request-body

- jeremy
Post by Martin Clausen
I am new to happs(tack) and Haskell in general, and could need some help
converting a very simple happs example to happstack.
I have a SO question here
<http://stackoverflow.com/questions/35800990/how-to-convert-from-happs-happstack> which
has the relevant code. It has been up for some time without a viable answer
and I was hoping someone on this list might be able to help.
Cheers
Martin
--
You received this message because you are subscribed to the Google Groups "HAppS" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/happs.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "HAppS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to happs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/happs.
For more options, visit https://groups.google.com/d/optout.
Loading...