Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up wai-handler-devel with Scotty to have automatic code reloading?

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?

like image 513
Jakub Arnold Avatar asked Apr 05 '14 17:04

Jakub Arnold


1 Answers

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 :)

like image 195
Jakub Arnold Avatar answered Nov 15 '22 06:11

Jakub Arnold