scotty-session 0.0.2 → 0.0.3
raw patch · 2 files changed
+14/−13 lines, 2 filesdep ~scotty
Dependency ranges changed: scotty
Files
- Web/Scotty/Session.hs +12/−11
- scotty-session.cabal +2/−2
Web/Scotty/Session.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE OverloadedStrings #-}+ module Web.Scotty.Session ( createSessionManager , modifySession@@ -9,7 +10,7 @@ import Control.Concurrent import Control.Concurrent.STM-import Control.Monad.IO.Class (liftIO)+import Control.Monad.IO.Class (MonadIO, liftIO) import Data.Monoid import Control.Arrow @@ -28,7 +29,7 @@ import Data.Time.Format import System.Locale (defaultTimeLocale) -import Web.Scotty+import Web.Scotty.Trans data Session a = Session@@ -41,30 +42,30 @@ newtype ScottySM a = ScottySM { _unSessionManager :: SessionJar a } -- | Create a new session manager-createSessionManager :: ScottyM (ScottySM a)+createSessionManager :: MonadIO m => ScottyT e m (ScottySM a) createSessionManager =- do storage <- liftIO $ atomically $ newTVar (HM.empty)+ do storage <- liftIO $ atomically $ newTVar HM.empty liftIO $ forkIO $ maintainSessions storage return $ ScottySM storage -- | Modify the current users session-modifySession :: ScottySM a -> (Maybe a -> Maybe a) -> ActionM ()+modifySession :: (MonadIO m, ScottyError e) => ScottySM a -> (Maybe a -> Maybe a) -> ActionT e m () modifySession sm@(ScottySM storage) fun = do oldS <- readSession' sm id let newS = oldS { sess_content = fun (sess_content oldS) } liftIO $ insertSession newS storage -- | Read the current users session-readSession :: ScottySM a -> ActionM (Maybe a)+readSession :: (MonadIO m, ScottyError e) => ScottySM a -> ActionT e m (Maybe a) readSession sm = readSession' sm sess_content -readSession' :: ScottySM a -> (Session a -> b) -> ActionM b+readSession' :: (MonadIO m, ScottyError e) => ScottySM a -> (Session a -> b) -> ActionT e m b readSession' (ScottySM storage) fun = do mSession <- loadSession storage case mSession of Just s -> return $ fun s Nothing ->- do newS <- liftIO $ createSession+ do newS <- liftIO createSession liftIO $ insertSession newS storage setCookie newS return $ fun newS@@ -98,17 +99,17 @@ maintainSessions :: SessionJar a -> IO () maintainSessions sessions = do now <- getCurrentTime- let stillValid sess = (sess_validUntil sess) > now+ let stillValid sess = sess_validUntil sess > now atomically $ modifyTVar sessions $ \m -> HM.filter stillValid m threadDelay 1000000 maintainSessions sessions -setCookie :: Session a -> ActionM ()+setCookie :: (Monad m, ScottyError e) => Session a -> ActionT e m () setCookie sess = do let formattedExp = TL.pack $ formatTime defaultTimeLocale "%a, %d-%b-%Y %X %Z" (sess_validUntil sess) setHeader "Set-Cookie" $ "sid=" <> TL.fromStrict (sess_id sess) <> "; " <> formattedExp -loadSession :: SessionJar a -> ActionM (Maybe (Session a))+loadSession :: (MonadIO m, ScottyError e) => SessionJar a -> ActionT e m (Maybe (Session a)) loadSession sessions = do req <- request liftIO $ getUserSession req sessions
scotty-session.cabal view
@@ -1,5 +1,5 @@ Name: scotty-session-Version: 0.0.2+Version: 0.0.3 Synopsis: Adding session functionality to scotty Homepage: https://github.com/agrafix/scotty-session Bug-reports: https://github.com/agrafix/scotty-session/issues@@ -19,7 +19,7 @@ Library Exposed-modules: Web.Scotty.Session default-language: Haskell2010- build-depends: base >= 3 && < 5, scotty >= 0.5, text, unordered-containers, transformers, stm, bytestring, blaze-builder, old-locale, time, http-types,+ build-depends: base >= 3 && < 5, scotty >= 0.6, text, unordered-containers, transformers, stm, bytestring, blaze-builder, old-locale, time, http-types, wai, base64-bytestring, crypto-api GHC-options: -Wall -fno-warn-orphans -fno-warn-unused-do-bind