on wai-extra/3.0.4.1 help sign in
80 lines 2.3 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2import Network.Wai
3import Network.Wai.Handler.Warp (defaultSettings, setPort)
4import Network.Wai.Handler.WarpTLS
5import Network.HTTP.Types (status200)
6import Blaze.ByteString.Builder (copyByteString)
7import Data.Monoid
8import qualified Data.Conduit as C
9import qualified Data.Conduit.List as CL
10import Network.HTTP.ReverseProxy
11import Network.HTTP.Conduit
13main = do
14 putStrLn "https://localhost:3009/"
15 --manager <- newManager def
16 runTLS (tlsSettings "certificate.pem" "key.pem")
17 { onInsecure = AllowInsecure
18 } (setPort 3009 defaultSettings) app
20app req f = f $
21 case rawPathInfo req of
22 "/builder/withlen" -> builderWithLen
23 "/builder/nolen" -> builderNoLen
24 "/file/withlen" -> fileWithLen
25 "/file/nolen" -> fileNoLen
26 "/source/withlen" -> sourceWithLen
27 "/source/nolen" -> sourceNoLen
28 "/secure" -> responseLBS status200 [] $ if isSecure req then "secure" else "insecure"
29 x -> index x
31builderWithLen = responseBuilder
32 status200
33 [ ("Content-Type", "text/plain")
34 , ("Content-Length", "4")
35 ]
36 $ copyByteString "PONG"
38builderNoLen = responseBuilder
39 status200
40 [ ("Content-Type", "text/plain")
41 ]
42 $ copyByteString "PONG"
44sourceWithLen = responseStream
45 status200
46 [ ("Content-Type", "text/plain")
47 , ("Content-Length", "4")
48 ]
49 $ \sendChunk _flush -> sendChunk $ copyByteString "PONG"
51sourceNoLen = responseStream
52 status200
53 [ ("Content-Type", "text/plain")
54 ]
55 $ \sendChunk _flush -> sendChunk $ copyByteString "PONG"
57fileWithLen = responseFile
58 status200
59 [ ("Content-Type", "text/plain")
60 , ("Content-Length", "4")
61 ]
62 "pong.txt"
63 Nothing
65fileNoLen = responseFile
66 status200
67 [ ("Content-Type", "text/plain")
68 ]
69 "pong.txt"
70 Nothing
72index p = responseBuilder status200 [("Content-Type", "text/html")] $ mconcat $ map copyByteString
73 [ "<p><a href='/builder/withlen'>builder withlen</a></p>\n"
74 , "<p><a href='/builder/nolen'>builder nolen</a></p>\n"
75 , "<p><a href='/file/withlen'>file withlen</a></p>\n"
76 , "<p><a href='/file/nolen'>file nolen</a></p>\n"
77 , "<p><a href='/source/withlen'>source withlen</a></p>\n"
78 , "<p><a href='/source/nolen'>source nolen</a></p>\n"
79 , p
80 ]