summaryrefslogtreecommitdiffhomepage
path: root/SessionID.hs
diff options
context:
space:
mode:
Diffstat (limited to 'SessionID.hs')
-rw-r--r--SessionID.hs19
1 files changed, 16 insertions, 3 deletions
diff --git a/SessionID.hs b/SessionID.hs
index 71f2150..3827e1d 100644
--- a/SessionID.hs
+++ b/SessionID.hs
@@ -13,10 +13,13 @@ import Serialization
import System.FilePath
import Data.Text
import System.IO
+import System.Directory
import Network.Wai.Handler.Warp (Port)
import Network.WebSockets hiding (Message)
import qualified Data.Aeson
import Data.Maybe
+import Data.UUID
+import Data.UUID.V4
-- | A SessionID is the base name of the log file to use,
-- and may not contain any path information.
@@ -48,11 +51,21 @@ sessionLogFile :: FilePath -> SessionID -> FilePath
sessionLogFile dir (SessionID f) = dir </> "debug-me." ++ f ++ ".log"
-- | Allocate a new SessionID and return an open Handle to its log file.
+--
+-- A UUID is used, to avoid ever generating a SessionID that has been used
+-- before.
withSessionID :: FilePath -> ((Handle, SessionID) -> IO a) -> IO a
withSessionID dir a = do
- -- TODO find an unused log file and open it
- let sid = SessionID "1"
- withFile "debug-me-server.log" WriteMode $ \h -> a (h, sid)
+ createDirectoryIfMissing False dir
+ sid <- SessionID . toString <$> nextRandom
+ let f = sessionLogFile dir sid
+ -- File should not already exist, but just in case we get
+ -- spectacularly unlucky (or the RNG is broken..),
+ -- avoid overwriting a log, and try again.
+ exists <- doesFileExist f
+ if exists
+ then withSessionID dir a
+ else withFile f WriteMode $ \h -> a (h, sid)
type UrlString = String