Skip to content

Commit

Permalink
Merge pull request #64 from kRITZCREEK/debug-flag
Browse files Browse the repository at this point in the history
hide server output behind --debug flag
  • Loading branch information
kritzcreek committed Sep 13, 2015
2 parents d626d9a + 0d26d55 commit 80a089e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions server/Main.hs
@@ -1,6 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad
import Control.Exception (bracketOnError)
import Control.Monad.State.Lazy
import Data.Maybe (fromMaybe)
Expand Down Expand Up @@ -39,23 +40,25 @@ listenOnLocalhost _ = error "Wrong Porttype"
data Options = Options
{ optionsDirectory :: Maybe FilePath
, optionsPort :: Maybe Int
, optionsDebug :: Bool
}

main :: IO ()
main = do
Options dir port <- execParser opts
Options dir port debug <- execParser opts
maybe (return ()) setCurrentDirectory dir
startServer (PortNumber . fromIntegral $ fromMaybe 4242 port) emptyPscState
startServer (PortNumber . fromIntegral $ fromMaybe 4242 port) debug emptyPscState
where
parser =
Options <$>
optional (strOption (long "directory" <> short 'd')) <*>
optional (option auto (long "port" <> short 'p'))
optional (option auto (long "port" <> short 'p')) <*>
switch (long "debug")
opts = info parser mempty


startServer :: PortID -> PscState -> IO ()
startServer port st_in =
startServer :: PortID -> Bool -> PscState -> IO ()
startServer port debug st_in =
withSocketsDo $
do sock <- listenOnLocalhost port
evalStateT (forever (loop sock)) st_in
Expand All @@ -64,21 +67,21 @@ startServer port st_in =
(h,_,_) <- accept sock
hSetEncoding h utf8
cmd <- T.hGetLine h
T.putStrLn cmd
when debug (T.putStrLn cmd)
return (cmd, h)
loop :: Socket -> PscIde ()
loop sock = do
(cmd,h) <- liftIO $ acceptCommand sock
case decodeT cmd of
Just cmd' -> do
result <- handleCommand cmd'
liftIO $ T.putStrLn ("Answer was: " <> (T.pack . show $ result))
when debug $ liftIO $ T.putStrLn ("Answer was: " <> (T.pack . show $ result))
case result of
-- What function can I use to clean this up?
Right r -> liftIO $ T.hPutStrLn h (encodeT r)
Left err -> liftIO $ T.hPutStrLn h (encodeT err)
Nothing ->
liftIO $ T.hPutStrLn h "Error parsing Command."
liftIO $ T.hPutStrLn h $ encodeT (GeneralError "Error parsing Command.")
liftIO $ hClose h

handleCommand :: Command -> PscIde (Either Error Success)
Expand Down

0 comments on commit 80a089e

Please sign in to comment.