I'm trying to build a simple way to route requests based on the hostname. Here is the function:
handleAccept :: Handle -> String -> IO ()
handleAccept handle hostname = do
putStrLn $ "Handling request from " ++ (getMessage hostname)
request <- fmap (parseRequest . lines) (hGetContents handle)
respond request handle
return ()
where getMessage x
| x == "hello" = "hello route"
| x == "LOL" = "what's so funny?"
| otherwise = x
Called here:
main = withSocketsDo $ do
sock <- listenOn (PortNumber 9000)
putStrLn "Listening on port 9000"
forever $ do
(handle, hostname, port) <- accept sock
handleAccept handle hostname
hClose handle
When I try to compile I get this error:
parse error (possibly incorrect indentation or mismatched brackets)
at this line in handleAccept:
where getMessage x
| x == "hello" = "hello route"
There seems to be an issue using guards with a where statement. So I tried this dummy function:
wherePlusGuards :: String -> Bool
wherePlusGuards x = getX x
where getX x
| x == "hello" = True
| otherwise = False
This compiles fine. I'm left to believe that the issue is coming from using a where statement and guards inside a do expression. Why is this the case? Please help me.