Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created December 18, 2016 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoto-ogawa/340dcfc89b336104b6bd9699fb36b161 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/340dcfc89b336104b6bd9699fb36b161 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
import Data.Maybe
import Data.CaseInsensitive
import qualified Data.ByteString.Char8 as C
import Network.HTTP.Types (status200)
import Network.Wai
import Network.Wai.Handler.Warp (run)
--
-- type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
--
-- app request respond =
-- ~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Request (Response -> IO ResponseRecieved)
--
app :: Application
app request respond = do
putStrLn "start app functioni ===================================="
--
-- requestMethod
--
-- > :t requestMethod
-- requestMethod :: Request -> Network.HTTP.Types.Method.Method
-- > :i Network.HTTP.Types.Method.Method
-- type Network.HTTP.Types.Method.Method = C.ByteString
--
putStrLn $ "requestMethod : " ++ (C.unpack $ requestMethod request)
--
-- isSecure
--
-- > :t isSecure
-- isSecure :: Request -> Bool
--
putStrLn $ "secure : " ++ (show $ isSecure request)
--
-- rawPathInfo
--
-- > :t rawPathInfo
-- rawPathInfo :: Request -> C.ByteString
--
putStrLn $ "rawPathInfo : " ++ (C.unpack $ rawPathInfo request)
--
-- rawQueryString
--
-- > :t rawQueryString
-- rawQueryString :: Request -> C.ByteString
--
putStrLn $ "rawQueryString : " ++ (C.unpack $ rawQueryString request)
--
-- requestHeaders
--
-- > :t requestHeaders
-- requestHeaders :: Request -> Network.HTTP.Types.Header.RequestHeaders
-- > :i Network.HTTP.Types.Header.RequestHeaders
-- type Network.HTTP.Types.Header.RequestHeaders = [Network.HTTP.Types.Header.Header]
-- > :i Network.HTTP.Types.Header.Header
-- type Network.HTTP.Types.Header.Header = (Network.HTTP.Types.Header.HeaderName, C.ByteString)
-- > :i Network.HTTP.Types.Header.HeaderName
-- type Network.HTTP.Types.Header.HeaderName = CI C.ByteString
--
-- requestHeaders request :: RequestHeaders
-- = [Hearder ]
-- = [(HeaderName , C.ByteString)]
-- = [(CI C.ByteString, C.ByteString)]
-- ^^
-- CI -- Case Insensitive
--
putStrLn $ "requestHeaders : "
mapM_ (\(x,y) -> putStrLn $ C.unpack $ (foldedCase x) <> " : " <>y) $ requestHeaders request
-- > :i Network.HTTP.Types.URI.Query
-- type Network.HTTP.Types.URI.Query = [Network.HTTP.Types.URI.QueryItem]
-- > :i Network.HTTP.Types.URI.QueryItem
-- type Network.HTTP.Types.URI.QueryItem = (C.ByteString, Maybe C.ByteString)
--
-- queryString request :: Query
-- = [QyertItem]
-- = [(C.ByteString, Maybe C.ByteString)]
--
putStrLn $ "queryString : "
mapM_ (\(x,y) -> putStrLn $ C.unpack $ x <> " : " <> (fromMaybe "none" y)) $ queryString request
--
-- response
--
respond $ responseLBS status200 [("Content-Type", "text/plain")] "hello"
--
--
-- responseLBS status200 [("Content-Type", "text/plain")] "hello"
--
-- responseLBS :: Network.HTTP.Types.Status.Status | status200
-- -> Network.HTTP.Types.Header.ResponseHeaders | [("Content-Type", "text/plain")]
-- -> Data.ByteString.Lazy.Internal.ByteString | "hello"
-- -> Response
--
--
-- run :: warp-3.2.9:Network.Wai.Handler.Warp.Types.Port -> Application -> IO ()
--
main :: IO ()
main = run 3030 app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment