How to run IO inside ScottyM

135 views Asked by At

I'm using Scotty to write a small web apps. I need to run IO inside the ScottyM type.

There are several difficulties:

First I can't automatically derive type synonyms from MonadIO in order to run liftIO:

type ScottyM = ScottyT Text IO 

Second, I don't know how to derive ScottyT from MonadIO:

newtype ScottyT e m a

Constructors
ScottyT  

    runS :: State (ScottyState e m) a 

What are my options?

Thanks

1

There are 1 answers

0
Vaibhav Sagar On

Is it necessary for you to run your IO inside ScottyM instead of before you start scotty?

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

import Data.Monoid (mconcat)

main = do
    print "Hello world!" -- Or your IO action of choice
    scotty 3000 $
        get "/:word" $ do
            beam <- param "word"
        html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]

Another option is to abuse notFound and next to run IO in ActionM, which is more straightforward:

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

import Control.Monad.Trans.Class (lift)
import Data.Monoid (mconcat)

main = scotty 3000 $ do
    notFound $ do
        lift $ print "Hello world!" -- Or some other IO action
        next
    get "/:word" $ do
        beam <- param "word"
        html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]