【问题标题】:How to convert from happs -> happstack?如何从 happs -> happstack 转换?
【发布时间】:2016-03-04 16:22:20
【问题描述】:

谁能帮我把下面的内容从happs“翻译”成happstack:

module Main where

import HAppS.Server.AlternativeHTTP
import HAppS.Server.HTTP.AltFileServe
import Control.Monad.State
import Numeric

import Contracts

instance FromData ExContr where
  fromData = do c    <- look "contract"
                arg1 <- look "arg1"
                arg2 <- look "arg2"
                img  <- look "image"
                return $ ExContr (c, map fst $ readFloat arg1
                                            ++ readFloat arg2, read img)

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


   ]

Contracts.hs 包含:

newtype ExContr = ExContr (String, [Double], Bool) deriving (Read,Show,Eq)

renderEx :: ExContr -> IO Html
 renderEx exSpec@(ExContr (contractId, args, lattice)) =
   let pr = evalEx exSpec
       expValChart = if contractId == "probs" then noHtml -- expected value is meaningless for the probabilities it relies on
                     else h3 << "Expected value" +++ image ! [src (chartUrl $ expectedValuePr pr)]
       imageType = "png"
   in if useLatticeImage exSpec
      then do baseName <- mkUniqueName baseDotFilename
              exitCode <- latticeImage pr (webPath ++ tmpImgPath ++ baseName) imageType
              let pageContents =
                    case exitCode of
                      ExitSuccess -> renderExampleForm exSpec (image ! [src latticeUrl, border 1]) expValChart
                                      where latticeUrl = "/" ++ tmpImgPath ++ baseName ++ "." ++ imageType
                      _ -> p << "renderEx: error generating lattice image"
              return $ renderExamplePage pageContents
      else return $ renderExamplePage $ renderExampleForm exSpec (prToTable pr) expValChart

renderExDefault = renderExamplePage $
                 renderExampleForm (ExContr ("zcb", [fromIntegral t1Horizon, 10], True))
                                   noHtml noHtml

另外,我想了解如何安装与上述代码兼容的旧版 HappS。不用说我对 Haskell 很陌生。

【问题讨论】:

    标签: haskell happstack


    【解决方案1】:

    这应该可以工作,假设你的代码中没有提供的 ExContr 类型和 renderEx 函数与我在这里的相似。我实际上无法运行您的代码以确保其行为相同。

    module Main where
    
    import Control.Monad
    import Control.Monad.Trans (liftIO)
    import Happstack.Server.Internal.Monads (anyRequest)
    import Happstack.Server.SimpleHTTP
    import Happstack.Server.FileServe
    import Numeric
    
    -- data ExContr = ExContr (String, [Double], String)
    
    
    -- renderEx :: ExContr -> IO String
    -- renderEx = undefined
    
    instance FromData ExContr where
      fromData = do c    <- look "contract"
                    arg1 <- look "arg1"
                    arg2 <- look "arg2"
                    img  <- look "image" 
                    return $ ExContr (c, map fst $ readFloat arg1
                                                ++ readFloat arg2, read img)
    
    main :: IO ()
    main = do
      simpleHTTP (nullConf { port = 80 }) $ msum [
             dir "contractEx" $ withData $ \(ExContr t) -> msum $ [
                    anyRequest $ fmap toResponse $ liftIO $ renderEx (ExContr t)
                  , anyRequest $ ok $ toResponse renderExDefault
                  ]
           , serveDirectory DisableBrowsing ["Contracts.html"] "public"
           ] 
    

    已编辑:忘记了 renderExDefault 行。

    【讨论】:

    • 以上内容正确地为 Contracts.html 提供服务,但 localhost:8080/contractEx(我已将端口号更改为 8080)给了我:Happstack 7.4.4 Your file is not found To try again is useless It is just not here
    • 我稍微修改了我的回复。我被告知 anyRequest 已被弃用,因此不再需要,但我不知道如何重写它,因为我不使用 happstack。所以这可能对你有用。
    • 感谢您的尝试。修改后的答案给了我一个编译时错误:Main.hs:28:30: Couldn't match expected type ‘Happstack.Server.Internal.Monads.WebT IO Response’ with actual type ‘Response’ In the second argument of ‘($)’, namely ‘toResponse renderExDefault’ In the expression: anyRequest $ toResponse renderExDefault Failed, modules loaded: Contracts.
    • 我认为您可能只需将第二行 anyRequest 更改为 anyRequest $ ok $ toResponse renderExDefault
    • 谢谢。这编译但localhost:8080/contractEx 产生Happstack 7.4.4 Your file is not found To try again is useless It is just not here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多