現在是2011年5月,只是對當前趨勢的更新。
我認爲今天大多數web開發都是通過yesod或快照框架來完成的。兩者都非常好,非常好(感謝所有參與的人!!)。此外還有包裝包裝。
我的小REST示例(或resful服務器)。 (好吧,也許這個例子不是一個真正的寧靜的服務器,但它顯示瞭如何處理GET/PUT請求,其餘部分取決於你..)
如果你在瀏覽器中打開http://localhost:8000/mytest,那麼「Get request」被展示。如果使用rest-client(也是localhost:8000/mytest)發出PUT請求,則請求主體的內容將存儲在「/tmp/restrq.txt」中。
此代碼是管理單元框架的Site.hs文件的一部分:
- | Constants
tempFilePath :: String
tempFilePath = "/tmp/restrq.txt"
-- | Helper Functions
-- Bytestring Conversion
strictToLazy :: B.ByteString -> BL.ByteString
strictToLazy x
| B.null x = BL.Empty
| otherwise = BL.Chunk x BL.Empty
lazyToStrict :: BL.ByteString -> B.ByteString
lazyToStrict = B.concat . BL.toChunks
getRequestString :: MonadSnap m => m B.ByteString
getRequestString = do message <- getRequestBody
return (lazyToStrict message)
-- | Action for PUT request
action :: Application()
action = do message <- getRequestString
liftIO $ B.writeFile tempFilePath (B8.append (B8.pack "--- REST BODY ---\n") message)
-- | /mytest (GET and PUT requests possible)
mytest :: Application()
mytest = method GET (writeBS "Get request") <|> method PUT action
-- | The main entry point handler.
site :: Application()
site = route [ ("/", index)
, ("/mytest", mytest)
]
<|> serveDirectory "resources/static"
只是澄清:鬣狗實際上並沒有使用wai軟件包,儘管它有一個類似的界面。如果你正在尋找一個基於wai的服務器,你應該使用Warp。 – 2011-09-12 19:40:16
@MichaelSnoyman你是怎麼測量rps的? – 2012-12-23 23:40:15