0

Scotty用来写一个小的网络应用程序。我需要IOScottyM类型内运行。

有几个困难:

首先,我不能自动派生类型同义词MonadIO以便运行liftIO

type ScottyM = ScottyT Text IO 

其次,我不知道如何从中派生 ScottyT MonadIO

newtype ScottyT e m a

Constructors
ScottyT  

    runS :: State (ScottyState e m) a 

我有哪些选择?

谢谢

4

1 回答 1

0

您是否有必要在内部ScottyM而不是在开始之前运行 IO 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>"]

另一种选择是滥用notFoundnext在 IO 中运行ActionM,这更直接:

{-# 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>"]
于 2020-06-07T14:02:17.270 回答