Skip to content

Commit

Permalink
Use index-monad's qualified do module; delete Middleware.QualifiedDo
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Sep 6, 2019
1 parent de84397 commit 5c65f2a
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 180 deletions.
18 changes: 9 additions & 9 deletions examples/Authentication.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Examples.Authentication where

import Prelude

import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect.Aff (Aff)
import Effect (Effect)
import Data.Maybe (Maybe(Just, Nothing))
Expand All @@ -29,17 +29,17 @@ userFromBasicAuth =
main :: Effect Unit
main =
let
myProfilePage =
getConn :>>= \conn ->
myProfilePage = Ix.do
conn <- getConn
case conn.components.authentication of
User name → do
User name → Ix.do
writeStatus statusOK
:*> contentType textHTML
:*> closeHeaders
:*> respond (render (p (text ("You are authenticated as " <> name <> "."))))
contentType textHTML
closeHeaders
respond (render (p (text ("You are authenticated as " <> name <> "."))))

app = do
app = Ix.do
BasicAuth.withAuthentication userFromBasicAuth
:*> BasicAuth.authenticated "Authentication Example" myProfilePage
BasicAuth.authenticated "Authentication Example" myProfilePage
components = { authentication: unit }
in runServer defaultOptionsWithLogging components app
23 changes: 12 additions & 11 deletions examples/AuthenticationAndAuthorization.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module Examples.AuthenticationAndAuthorization where

import Prelude

import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad.Indexed ((:>>=))
import Effect.Aff.Class (class MonadAff)
import Effect (Effect)
import Data.Either (Either(..))
Expand Down Expand Up @@ -45,11 +46,11 @@ htmlWithStatus
(Conn req (res StatusLineOpen) c)
(Conn req (res ResponseEnded) c)
Unit
htmlWithStatus status x =
htmlWithStatus status x = Ix.do
writeStatus status
:*> contentType textHTML
:*> closeHeaders
:*> respond (render x)
contentType textHTML
closeHeaders
respond (render x)


-- Users have user names.
Expand All @@ -76,11 +77,11 @@ profileHandler
(Conn req (res StatusLineOpen) { authentication :: Maybe User | c })
(Conn req (res ResponseEnded) { authentication :: Maybe User | c })
Unit
profileHandler =
getConn :>>= \conn
profileHandler = Ix.do
conn <- getConn
htmlWithStatus
statusOK
(view conn.components.authentication)
statusOK
(view conn.components.authentication)
where
view =
case _ of
Expand Down Expand Up @@ -183,8 +184,8 @@ app = BasicAuth.withAuthentication userFromBasicAuth :>>= \_ → router
li (a ! A.href "/profile" $ text "Profile")
li (a ! A.href "/admin" $ text "Administration")

router =
getRequestData :>>= \{ method, url }
router = Ix.do
{ method, url } <- getRequestData
case method, url of
Left GET, "/" ->
htmlWithStatus statusOK homeView
Expand Down
11 changes: 6 additions & 5 deletions examples/Cookies.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Examples.Cookies where

import Prelude
import Control.Monad.Indexed ((:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect (Effect)
import Hyper.Cookies (cookies)
import Hyper.Node.Server (defaultOptionsWithLogging, runServer)
Expand All @@ -10,8 +10,9 @@ import Hyper.Status (statusOK)

main :: Effect Unit
main =
let app = cookies
:*> writeStatus statusOK
:*> closeHeaders
:*> respond "Hello, Hyper!"
let app = Ix.do
cookies
writeStatus statusOK
closeHeaders
respond "Hello, Hyper!"
in runServer defaultOptionsWithLogging { cookies: unit } app
8 changes: 4 additions & 4 deletions examples/FileServer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Examples.FileServer where

import Prelude

import Control.Monad.Indexed ((:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect (Effect)
import Data.Tuple (Tuple(Tuple))
import Hyper.Node.FileServer (fileServer)
Expand All @@ -14,9 +14,9 @@ import Node.Encoding (Encoding(UTF8))
main :: Effect Unit
main =
let
notFound =
notFound = Ix.do
writeStatus statusNotFound
:*> headers []
:*> respond (Tuple "<h1>Not Found</h1>" UTF8)
headers []
respond (Tuple "<h1>Not Found</h1>" UTF8)
app = fileServer "examples/FileServer" notFound
in runServer defaultOptionsWithLogging {} app
9 changes: 5 additions & 4 deletions examples/FormParser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Examples.FormParser where

import Prelude
import Text.Smolder.HTML.Attributes as A
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad.Indexed ((:>>=), (:*>))
import Effect (Effect)
import Effect.Class (liftEffect)
Expand Down Expand Up @@ -39,11 +40,11 @@ main =
Just s -> p ! A.style "color: red;" $ text s
Nothing -> pure unit

htmlWithStatus status x =
htmlWithStatus status x = Ix.do
writeStatus status
:*> contentType textHTML
:*> closeHeaders
:*> respond (render x)
contentType textHTML
closeHeaders
respond (render x)

handlePost =
parseForm :>>=
Expand Down
9 changes: 5 additions & 4 deletions examples/HelloHyper.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Examples.HelloHyper where

import Prelude
import Control.Monad.Indexed ((:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect (Effect)
import Hyper.Node.Server (defaultOptionsWithLogging, runServer)
import Hyper.Response (closeHeaders, respond, writeStatus)
import Hyper.Status (statusOK)

main :: Effect Unit
main =
let app = writeStatus statusOK
:*> closeHeaders
:*> respond "Hello, Hyper!"
let app = Ix.do
writeStatus statusOK
closeHeaders
respond "Hello, Hyper!"
in runServer defaultOptionsWithLogging {} app
10 changes: 4 additions & 6 deletions examples/NodeStreamRequest.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Examples.NodeStreamRequest where
import Prelude
import Node.Buffer as Buffer
import Node.Stream as Stream
import Control.Monad.Indexed (ibind, (:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad.Indexed ((:>>=))
import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console (log)
Expand Down Expand Up @@ -44,20 +45,17 @@ main =
case _ of

-- Only handle POST requests:
{ method: Left POST } -> do
{ method: Left POST } -> Ix.do
body <- streamBody
logRequestBodyChunks body
writeStatus statusOK
closeHeaders
respond "OK"

-- Non-POST requests are not allowed:
{ method } -> do
{ method } -> Ix.do
writeStatus statusMethodNotAllowed
closeHeaders
respond ("Method not allowed: " <> either show show method)

where
bind = ibind
discard = ibind
in runServer defaultOptionsWithLogging {} app
9 changes: 5 additions & 4 deletions examples/NodeStreamResponse.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Examples.NodeStreamResponse where

import Prelude
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad.Indexed ((:*>))
import Effect.Aff as Aff
import Effect.Aff.Class (class MonadAff, liftAff)
Expand Down Expand Up @@ -35,9 +36,9 @@ main =
, Tuple 500 "Hyper\n"
]

app = do
app = Ix.do
writeStatus statusOK
:*> closeHeaders
:*> streamMessages
:*> end
closeHeaders
streamMessages
end
in runServer defaultOptions {} app
4 changes: 2 additions & 2 deletions examples/QualifiedDo.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Examples.QualifiedDo where

import Prelude
import Effect (Effect)
import Hyper.Middleware as Middleware
import Control.Monad.Indexed.Qualified as Ix
import Hyper.Node.Server (defaultOptionsWithLogging, runServer)
import Hyper.Response (closeHeaders, respond, writeStatus)
import Hyper.Status (statusOK)

main :: Effect Unit
main = runServer defaultOptionsWithLogging {} Middleware.do
main = runServer defaultOptionsWithLogging {} Ix.do
writeStatus statusOK
closeHeaders
respond "Hello, Hyper!"
49 changes: 25 additions & 24 deletions examples/Sessions.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Examples.Sessions where

import Prelude
import Control.Monad.Indexed ((:*>), (:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad.Indexed ((:>>=))
import Effect.Aff (launchAff)
import Effect (Effect)
import Effect.Class (liftEffect)
Expand Down Expand Up @@ -31,38 +32,38 @@ main = void $ launchAff do
, cookies: unit
}

home =
home = Ix.do
writeStatus statusOK
:*> contentType textHTML
:*> closeHeaders
:*> getSession :>>=
contentType textHTML
closeHeaders
getSession :>>=
case _ of
Just (MySession { userId }) ->
lift' (log "Session") :*>
Just (MySession { userId }) -> Ix.do
lift' (log "Session")
respond ("You are logged in as user " <> show userId <> ". "
<> "<a href=\"/logout\">Logout</a> if you're anxious.")
Nothing ->
lift' (log "No Session") :*>
Nothing -> Ix.do
lift' (log "No Session")
respond "<a href=\"/login\">Login</a> to start a session."

login =
login = Ix.do
redirect "/"
:*> saveSession (MySession { userId: 1 })
:*> contentType textHTML
:*> closeHeaders
:*> end
saveSession (MySession { userId: 1 })
contentType textHTML
closeHeaders
end

logout =
logout = Ix.do
redirect "/"
:*> deleteSession
:*> closeHeaders
:*> end
deleteSession
closeHeaders
end

notFound =
notFound = Ix.do
writeStatus statusNotFound
:*> contentType textHTML
:*> closeHeaders
:*> respond "Not Found"
contentType textHTML
closeHeaders
respond "Not Found"

-- Simple router for this example.
router =
Expand All @@ -73,6 +74,6 @@ main = void $ launchAff do
"/logout" -> logout
_ -> notFound

app =
app = Ix.do
cookies
:*> router
router
16 changes: 7 additions & 9 deletions examples/StateT.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Examples.StateT where

import Prelude
import Control.Monad.Indexed (ibind, (:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect.Aff (Aff)
import Effect (Effect)
import Control.Monad.State (evalStateT, get, modify)
Expand All @@ -22,16 +22,14 @@ main =
let
-- Our application just appends to the state in between
-- some operations, then responds with the built up state...
app = do
_ <- lift' (modify (flip append ["I"]))
:*> writeStatus statusOK
:*> lift' (modify (flip append ["have"]))
:*> closeHeaders
:*> lift' (modify (flip append ["state."]))
app = Ix.do
void $ lift' (modify (flip append ["I"]))
writeStatus statusOK
void $ lift' (modify (flip append ["have"]))
closeHeaders
void $ lift' (modify (flip append ["state."]))

msgs ← lift' get
respond (joinWith " " msgs)

where bind = ibind

in runServer' defaultOptionsWithLogging {} runAppM app
17 changes: 8 additions & 9 deletions src/Hyper/Authorization.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Hyper.Authorization where

import Control.Monad.Indexed (ibind)
import Control.Monad.Indexed.Qualified as Ix
import Control.Monad (class Monad)
import Data.Maybe (Maybe(Nothing, Just))
import Data.Unit (unit, Unit)
Expand Down Expand Up @@ -33,16 +33,15 @@ authorized :: forall a m req res b c
(Conn req (res StatusLineOpen) { authorization :: Unit | c })
(Conn req (res ResponseEnded) { authorization :: Unit | c })
Unit
authorized authorizer mw = do
authorized authorizer mw = Ix.do
conn ← getConn
auth ← lift' (authorizer conn)
case auth of
Just a -> do
_ <- modifyConn (withAuthorization a)
_ <- mw
Just a -> Ix.do
modifyConn (withAuthorization a)
mw
modifyConn (withAuthorization unit)
Nothing -> do
_ <- writeStatus statusForbidden
_ <- headers []
Nothing -> Ix.do
writeStatus statusForbidden
headers []
respond "You are not authorized."
where bind = ibind
Loading

0 comments on commit 5c65f2a

Please sign in to comment.