【问题标题】:Why does this work in GHCi but not Scotty?为什么这在 GHCi 中有效,但在 Scotty 中无效?
【发布时间】:2015-09-12 08:10:28
【问题描述】:

我正在尝试制作一个列出给定目录内容的网页,但我遇到了一个奇怪的问题:当我在 GHCi 中逐行运行代码时,代码会产生所需的输出,但是当它被执行时在运行的 Scotty 实例中,它会产生不同的(错误的)输出。这是代码的相关部分:

serveDir :: String -> ActionM ()
serveDir p = do let path = prefix ++ p
                entries <- liftIO $ getDirectoryContents path
                fs <- liftIO $ filterM (doesFileExist . ((++) prefix)) entries
                ds <- liftIO $ filterM (doesDirectoryExist . ((++) prefix)) entries
                liftIO $ print path >> print entries >> print fs >> print ds
                blaze $ renderDir fs ds
   where prefix = "static/"

(do 语句中的最后一行只是将其呈现为 html。这可行,但正确的输出永远不会到达该函数) 当我在 GHCi 中运行此函数的每一行时,我得到以下输出:

*Main> entries <- getDirectoryContents "static/stuff"
*Main> fs <- liftIO $ filterM (doesFileExist . ((++) "static/stuff/")) entries
*Main> ds <- liftIO $ filterM (doesDirectoryExist . ((++) "static/stuff/")) entries
*Main> liftIO $ print entries >> print fs >> print ds
["..","hello","bye","someDir","."]
["hello","bye"]
["..","someDir","."]

这是我所期待的。但是当这个函数从 Scotty 运行时,称为serveDir "stuff/",我得到这个输出:

"static/stuff/"
["..","hello","bye","someDir","."]
[]
["..","."]

这里发生了什么?我知道 ActionM 是 MonadIO 的一个实例,否则将无法编译。我有点不知所措。有人可以对此有所了解或提供建议吗?其余代码可根据要求提供,但我可以说我正在使用 wai-middleware-static 来允许静态文件请求,并且其他函数中来自该目录的其他文件请求有效。

【问题讨论】:

    标签: haskell scotty haskell-wai


    【解决方案1】:
    fs <- liftIO $ filterM (doesFileExist . ((++) prefix)) entries
    

    不应该是(++) path吗?

    【讨论】:

      猜你喜欢
      • 2015-07-14
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多