Skip to content

Commit 5dd6904

Browse files
author
Joonas Nietosvaara
committed
Fix style; use same toStrict function as rest of code.
1 parent b29ca4e commit 5dd6904

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

example/AuthSinglePageApp.hs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module AuthSinglePageApp (app, initialServerState) where
2222
--------------------------------------------------------------------------------
2323
-- Imports:
2424
import Control.Monad.Except
25-
import Data.ByteString (ByteString, toStrict)
25+
import Data.ByteString (ByteString)
2626
import Data.ByteString.Builder (toLazyByteString)
2727
import qualified Data.ByteString.Char8 as Char8
2828
import qualified Data.ByteString.Lazy.Char8 as LChar8
@@ -147,12 +147,12 @@ api = Proxy
147147

148148
--------------------------------------------------------------------------------
149149
app :: MVar ServerState -> Manager -> Provider -> Credentials -> Application
150-
app mvar_serverstate mgr provider creds =
151-
serve api (handlers mvar_serverstate mgr provider creds)
150+
app mvarServerState mgr provider creds =
151+
serve api (handlers mvarServerState mgr provider creds)
152152

153153
--------------------------------------------------------------------------------
154154
handlers :: MVar ServerState -> Manager -> Provider -> Credentials -> Server API
155-
handlers mvar_serverstate mgr provider creds =
155+
handlers mvarServerState mgr provider creds =
156156
index :<|> login :<|> success :<|> failed :<|> waitForLogin :<|> static
157157
where
158158
----------------------------------------------------------------------------
@@ -171,10 +171,10 @@ handlers mvar_serverstate mgr provider creds =
171171
login = do
172172
-- If available, request "profile" scope so that we can show user's name
173173
-- on the page.
174-
let has_profile = case provider & providerDiscovery & scopesSupported of {
174+
let hasProfile = case provider & providerDiscovery & scopesSupported of {
175175
Nothing -> False;
176176
Just scope -> hasScope scope "profile" }
177-
let scope = if has_profile then openid <> profile else openid
177+
let scope = if hasProfile then openid <> profile else openid
178178
let req = defaultAuthenticationRequest scope creds
179179
r <- liftIO (authenticationRedirect (providerDiscovery provider) req)
180180
case r of
@@ -207,15 +207,15 @@ handlers mvar_serverstate mgr provider creds =
207207
-- cookie.
208208
-- * Send exit signal to close the connection.
209209
let socketID = decodeUtf8 $ getSocketIDCookie socketIDCookie
210-
serverstate <- liftIO $ readMVar mvar_serverstate
210+
serverstate <- liftIO $ readMVar mvarServerState
211211
liftIO $ case Map.lookup socketID serverstate of
212212
Nothing -> TextIO.putStrLn $
213213
"No connection for socketID " `append` socketID
214214
Just (conn, exitSignal) -> do
215215
WS.sendTextData conn $ afterLoginMessage _token
216216
putMVar exitSignal ()
217217
-- Remove this connection from server state.
218-
modifyMVar_ mvar_serverstate (return . Map.delete socketID)
218+
modifyMVar_ mvarServerState (return . Map.delete socketID)
219219

220220
pure . H.docTypeHtml $ do
221221
H.title "Success!"
@@ -224,14 +224,15 @@ handlers mvar_serverstate mgr provider creds =
224224
"setTimeout(function() {window.close();}, 1000);"
225225
where
226226
afterLoginMessage :: TokenResponse ClaimsSet -> Text
227-
afterLoginMessage tr = the_dict & Aeson.encode & toStrict & decodeUtf8
227+
afterLoginMessage tr =
228+
theDict & Aeson.encode & LChar8.toStrict & decodeUtf8
228229
where
229-
access_tokens = Aeson.toJSON $
230+
accessTokens = Aeson.toJSON $
230231
(const Nothing <$> tr :: TokenResponse (Maybe Text))
231-
id_token = Aeson.toJSON $ idToken tr
232-
the_dict = Map.fromList [
233-
("access_tokens", access_tokens),
234-
("id_token", id_token)] :: Map.Map Text Aeson.Value
232+
idToken_ = Aeson.toJSON $ idToken tr
233+
theDict = Map.fromList [
234+
("access_tokens", accessTokens),
235+
("id_token", idToken_)] :: Map.Map Text Aeson.Value
235236

236237
----------------------------------------------------------------------------
237238
-- Should have been a success, but one or more params are missing.
@@ -249,27 +250,28 @@ handlers mvar_serverstate mgr provider creds =
249250
waitForLogin = streamData
250251
where
251252
streamData :: MonadIO m => WS.Connection -> m ()
252-
streamData conn = do
253+
streamData conn = liftIO $ do
253254
-- * Generate and send socket ID to client (to be stored in a cookie).
254255
-- * Spin off a ping thread that keeps the connection alive.
255256
-- * Then block until we receive the exit signal from another thread.
256257
-- Another thread sends the actual login notification to the client.
257258
-- The connection is automatically closed once this IO action
258259
-- finishes, which is why a signal must be used to keep it from
259260
-- finishing prematurely.
260-
socketID <- liftIO $ decodeUtf8 . convertToBase Base64URLUnpadded <$>
261+
socketID <- decodeUtf8 . convertToBase Base64URLUnpadded <$>
261262
(getRandomBytes 32 :: IO ByteString)
262-
exitSignal <- liftIO (newEmptyMVar :: IO (MVar ()))
263-
liftIO $ modifyMVar_ mvar_serverstate $ \ss ->
263+
exitSignal <- newEmptyMVar :: IO (MVar ())
264+
modifyMVar_ mvarServerState $ \ss ->
264265
return $ Map.insert socketID (conn, exitSignal) ss
265-
liftIO $ WS.sendTextData conn (socketIDMessage socketID)
266-
liftIO $ WSC.withPingThread conn 30 (return ()) $ do
266+
WS.sendTextData conn (socketIDMessage socketID)
267+
WSC.withPingThread conn 30 (return ()) $ do
267268
takeMVar exitSignal
268269

269270
socketIDMessage :: Text -> Text
270-
socketIDMessage socketID = the_dict & Aeson.encode & toStrict & decodeUtf8
271+
socketIDMessage socketID =
272+
theDict & Aeson.encode & LChar8.toStrict & decodeUtf8
271273
where
272-
the_dict = Map.fromList [("socketID", socketID)] :: Map.Map Text Text
274+
theDict = Map.fromList [("socketID", socketID)] :: Map.Map Text Text
273275

274276
static :: Server Static
275277
static = serveDirectoryWebApp "static"

example/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ main = do
4646
app <- liftIO $ case optionsSinglePageApp opts of
4747
False -> return $ Auth.app mgr provider creds
4848
True -> do
49-
mvar_serverstate <- newMVar AuthSinglePageApp.initialServerState
50-
return $ AuthSinglePageApp.app mvar_serverstate mgr provider creds
49+
mvarServerState <- newMVar AuthSinglePageApp.initialServerState
50+
return $ AuthSinglePageApp.app mvarServerState mgr provider creds
5151

5252
putStrLn "Starting web server"
5353
Warp.runTLS tls settings app

0 commit comments

Comments
 (0)