Haskell Wai Tutorial

Table of Contents

1 What is Wai?

  • Wai is a common protocol for communication between web application and web server.
  • Wai can be used to create RESTful web services.

2 How to install it?

cabal install wai

3 First Hello World Example.

{-# LANGUAGE OverloadedStrings #-}               
import Network.Wai                               
import Network.HTTP.Types                        
import Network.Wai.Handler.Warp (run)            
app :: Application                               
app _ respond = do                               
    respond $ responseLBS                        
        status200                                
        [("Content-Type", "text/plain")]         
        "Hello, World!"                          

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

4 How to response a file from your Wai server.

  • responseFile
  • Assume file index.html is in the current dir where the server is running.
plainIndex::Response
plainIndex = responseFile
    status200
    [("Content-Type", "text/html")]
    "index.html"
    Nothing

app _ respond = do
    respond responseFile
main:: IO()
main = do
      run 8080 app

5 Wai and Database.

6 Execute shell command on your Wai server.

Author: cat

Created: 2019-08-02 Fri 22:09

Validate