From 5572dbc8289de934e9ee5bc3f74a0f98365ce3e5 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 21 Apr 2017 17:42:10 -0400 Subject: initial http server Incomplete, but the client is able to connect and send messages which get logged. Split up debug-me.hs into Role/* Switched from cereal to binary, since websockets operate on lazy ByteStrings, and using cereal would involve a copy on every receive. This commit was sponsored by Boyd Stephen Smith Jr. on Patreon. --- SessionID.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SessionID.hs (limited to 'SessionID.hs') diff --git a/SessionID.hs b/SessionID.hs new file mode 100644 index 0000000..a47de8f --- /dev/null +++ b/SessionID.hs @@ -0,0 +1,28 @@ +module SessionID (SessionID, mkSessionID, sessionLogFile, withSessionID) where + +import System.FilePath +import Data.Text +import System.IO + +-- | A SessionID is the base name of the log file to use, +-- and may not contain any path information. +newtype SessionID = SessionID FilePath + deriving (Show, Eq, Ord) + +-- | Smart constructor that enforces security requirements. +mkSessionID :: Text -> Maybe SessionID +mkSessionID t = + let f = unpack t + in if takeFileName f == f + then Just (SessionID f) + else Nothing + +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. +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) -- cgit v1.2.3