Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions reserve.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ executable reserve
, http-types
, http-kit >= 0.5
, bytestring
, filepath
, directory
, time

test-suite spec
type:
Expand All @@ -57,6 +60,9 @@ test-suite spec
, http-types
, http-kit
, bytestring
, filepath
, directory
, time

, hspec >= 1.5
, QuickCheck
Expand Down
28 changes: 18 additions & 10 deletions src/Reserve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Util
import Options
import Interpreter (Interpreter)
import qualified Interpreter
import Data.IORef

data Session = Session Socket Interpreter

Expand All @@ -32,16 +33,23 @@ withSession :: Options -> (Session -> IO a) -> IO a
withSession opts = bracket (openSession opts) closeSession

run :: Options -> IO ()
run opts = withSession opts $ \(Session s interpreter) -> forever $ do
(h, _, _) <- accept s
Interpreter.reload interpreter
Interpreter.start interpreter (optionsAppArgs opts)
c <- inputStreamFromHandle h
let send :: ByteString -> IO ()
send = ignoreResourceVanished . B.hPutStr h
readRequest True c >>= httpRequest (optionsPort opts) send
Interpreter.stop interpreter
ignoreResourceVanished $ hClose h
run opts = withSession opts $ \(Session s int) -> do
Interpreter.start int (optionsAppArgs opts)
ref <- getModTime >>= newIORef
forever $ do
(h, _, _) <- accept s
t0 <- readIORef ref
t1 <- getModTime
when (t0 < t1) $ do
Interpreter.stop int
Interpreter.reload int
Interpreter.start int (optionsAppArgs opts)
writeIORef ref t1
c <- inputStreamFromHandle h
let send :: ByteString -> IO ()
send = ignoreResourceVanished . B.hPutStr h
readRequest True c >>= httpRequest (optionsPort opts) send
ignoreResourceVanished $ hClose h

ignoreResourceVanished :: IO () -> IO ()
ignoreResourceVanished action = catchJust (guard . (== ResourceVanished) . ioe_type) action return
Expand Down
24 changes: 24 additions & 0 deletions src/Util.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Util where

import Control.Applicative
import Control.Concurrent
import Control.Monad
import Control.Exception
import System.IO
import System.FilePath
import System.Directory
import Data.Time.Clock
import Data.List
import Network

connectRetry :: Int -> HostName -> PortNumber -> (Maybe Handle -> IO a) -> IO a
Expand All @@ -17,3 +23,21 @@ connectRetry delay host port action = go 0
tryConnect = try $ bracket connect hClose (action . Just)
retry n = threadDelay delay >> go (succ n)
connect = connectTo host $ PortNumber port

getModTime :: IO UTCTime
getModTime = maximum <$> (listFiles >>= mapM getModificationTime)
where
listFiles :: IO [FilePath]
listFiles = filter ((||) <$> isSuffixOf ".hs" <*> isSuffixOf ".lhs") <$> go "."
where
go dir = do
(dirs, files) <- getFilesAndDirectories dir
(files ++) . concat <$> mapM go (filter (`notElem` exclude) dirs)

exclude :: [FilePath]
exclude = ["./.git"]

getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath])
getFilesAndDirectories dir = do
c <- map (dir </>) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir
(,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c