I've been playing around with the Scotty web framework and tried to make it work with wai-handler-devel to enable code reloading. Here's an example app
{-# LANGUAGE OverloadedStrings #-}
module Example where
import Data.Monoid (mconcat)
import Network.Wai
import Web.Scotty
handler :: ScottyM ()
handler = get "/:word" $ do
beam <- param "word"
html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]
main :: IO ()
main = scotty 3000 handler
the problem here is that wai-handler-devel
expects to get an (Application -> IO ()) -> IO ()
function to make things work, but in this case I only have ScottyM ()
and IO ()
. There are a few functions in the Web.Scotty
and Web.Scotty.Trans
packages, namely the following
scottyApp :: ScottyM () -> IO Application
this can turn our handler
into IO Network.Wai.Application
, which is closer to what wai-handler-devel
expects, but not exactly.
There's also scottyAppT ::
type signature omitted ... but in this case I'm not even sure how to read the type signature.
What I've found is that there are some examples of using wai-handler-devel
on a WAI application alone, but I just don't know how to convert the Scotty application to a WAI application with the required signature.
I also wanted to ask that since wai-handler-devel
is being deprecated in favor of yesod-bin
, is that supposed to replace it even for simple WAI/Scotty applications like this, or is yesod-bin just for yesod applications?
This actually turned out to be rather easy. Say that the application has a router
function which has the type of router :: ScottyM ()
.
The way you'd run the app normally is
main :: IO ()
main = scotty 3000 router
the only thing needed to make this work with wai-handler-devel
is to define another function, say dev
, that looks like this
dev :: (Application -> IO ()) -> IO ()
dev h = scottyApp router >>= h
After that you just need to run wai-handler-devel 3000 MyModule dev
and it all works perfectly :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With