How do I use wai-handler-devel with a simple wai application

不羁的心 提交于 2019-12-04 07:42:03

wai-handler-devel's Hackage page says that it should be invoked from the command-line like so:

$ wai-handler-devel <port> My.App.Module myApp

and that your application's type must look like this:

myApp :: (Application -> IO ()) -> IO ()

In this case, you should define myApp as follows:

myApp :: (Application -> IO ()) -> IO ()
myApp handler = handler app

although you may want to inline app entirely:

myApp :: (Application -> IO ()) -> IO ()
myApp handler = handler $ \_ -> return $ responseLBS
    status200
    [("Content-Type", "text/plain")]
    "Hello, World!"

The type is like this so that you can do initialisation on start-up and the like in IO. I suggest reading the SmallApp and FullApp examples from wai-handler-devel's git repository; the latter is especially helpful, as it has debug output showing the flow of the code during a reload, and shows how to integrate a long-running database connection.

The run script for the FullApp example also shows how to use wai-handler-devel programmatically, including manually specifying Hamlet template dependencies (which the wai-handler-devel command-line tool determines automatically).

You should then be able to rewrite your main as follows:

main :: IO ()
main = do
    putStrLn $ "http://localhost:8080/"
    myApp (run 8080)

Of course, you could just as easily pass the run function from wai-handler-fastcgi, wai-handler-scgi or even wai-handler-webkit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!