Created
September 30, 2011 02:37
-
-
Save saml/1252517 to your computer and use it in GitHub Desktop.
hello world web server haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Network (listenOn, PortID(PortNumber)) | |
import Network.Socket (accept, withSocketsDo, sClose) | |
import Network.Socket.ByteString (sendAll) | |
import Control.Concurrent (forkIO) | |
import qualified Data.ByteString.Lazy.Char8 as LazyByte (toChunks) | |
import qualified Data.ByteString as WordByte (concat) | |
import qualified Data.Text.Lazy as LazyText (Text, length, pack, concat) | |
import Data.Text.Lazy.Encoding (encodeUtf8) | |
main = withSocketsDo $ do | |
sock <- listenOn (PortNumber 8080) | |
loop sock | |
loop sock = do | |
(conn, _) <- accept sock | |
forkIO $ body conn | |
loop sock | |
where | |
body c = do | |
let resp = okResp "hello world" --how would I actually read from Socket? | |
sendAll c (WordByte.concat . LazyByte.toChunks $ toByteString resp) | |
sClose c | |
data Response = TextResponse { | |
version :: LazyText.Text, | |
status :: Int, | |
reason :: LazyText.Text, | |
text :: LazyText.Text | |
} deriving (Show, Eq) | |
okResp text = TextResponse { | |
version = "1.1", status = 200, reason = "Ok", text = text } | |
toByteString (TextResponse {version = ver, | |
status = stat, | |
reason = reason, | |
text = text}) = encodeUtf8 . LazyText.concat $ [ | |
"HTTP/", ver, LazyText.pack . show $ stat, reason, "\r\n", | |
"Content-Length: ", LazyText.pack . show $ LazyText.length text, "\r\n", | |
"\r\n", | |
text] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment