Skip to content

Commit

Permalink
Update entire project to use qualified do notation where relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Aug 13, 2019
1 parent 37cb893 commit 291fe12
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 251 deletions.
37 changes: 19 additions & 18 deletions docs/src/topic-guides/FormSerialization.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module FormSerialization where
import Prelude

import Control.Monad.Error.Class (throwError)
import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed ((:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
Expand Down Expand Up @@ -69,19 +70,19 @@ onPost
onPost =
parseFromForm :>>=
case _ of
Left err ->
Left err -> Ix.do
writeStatus statusBadRequest
:*> closeHeaders
:*> respond (err <> "\n")
closeHeaders
respond (err <> "\n")
Right (Order { beers, meal })
| meal == Omnivore || meal == Carnivore ->
| meal == Omnivore || meal == Carnivore -> Ix.do
writeStatus statusBadRequest
:*> closeHeaders
:*> respond "Sorry, we do not serve meat here.\n"
| otherwise ->
closeHeaders
respond "Sorry, we do not serve meat here.\n"
| otherwise -> Ix.do
writeStatus statusBadRequest
:*> closeHeaders
:*> respond ("One " <> show meal <> " meal and "
closeHeaders
respond ("One " <> show meal <> " meal and "
<> show beers <> " beers coming up!\n")
-- end snippet onPost

Expand All @@ -92,16 +93,16 @@ main =
_.method <$> getRequestData :>>=
case _ of
Left POST -> onPost
Left method ->
Left method -> Ix.do
ignoreBody
:*> writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Method not supported: " <> show method)
Right customMethod ->
writeStatus statusMethodNotAllowed
closeHeaders
respond ("Method not supported: " <> show method)
Right customMethod -> Ix.do
ignoreBody
:*> writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Custom method not supported: " <> show customMethod)
writeStatus statusMethodNotAllowed
closeHeaders
respond ("Custom method not supported: " <> show customMethod)

-- Let's run it.
in runServer defaultOptionsWithLogging {} router
12 changes: 6 additions & 6 deletions docs/src/topic-guides/NodeReaderT.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module NodeReaderT where

import Prelude
import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed.Qualified as Ix
import Effect.Aff (Aff)
import Effect (Effect)
import Control.Monad.Reader (ReaderT, ask, runReaderT)
Expand All @@ -21,10 +21,10 @@ runAppM = flip runReaderT { thingToSay: "Hello, ReaderT!" }

main :: Effect Unit
main =
let app =
lift' ask :>>= \config ->
writeStatus statusOK
:*> closeHeaders
:*> respond config.thingToSay
let app = Ix.do
config <- lift' ask
writeStatus statusOK
closeHeaders
respond config.thingToSay
in runServer' defaultOptionsWithLogging {} runAppM app
-- end snippet main
36 changes: 18 additions & 18 deletions docs/src/topic-guides/ReadBody.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module ReadBody where

import Prelude

import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed ((:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Data.Either (Either(..))
import Data.HTTP.Method (Method(..))
import Effect (Effect)
Expand All @@ -25,17 +26,16 @@ onPost
(Conn req BodyRead res ResponseEnded c)
Unit
-- start snippet onPost
onPost =
readBody :>>=
case _ of
"" ->
onPost = Ix.do
readBody :>>= case _ of
"" -> Ix.do
writeStatus statusBadRequest
:*> closeHeaders
:*> respond "... anyone there?"
msg ->
closeHeaders
respond "... anyone there?"
msg -> Ix.do
writeStatus statusBadRequest
:*> closeHeaders
:*> respond ("You said: " <> msg)
closeHeaders
respond ("You said: " <> msg)
-- end snippet onPost

main :: Effect Unit
Expand All @@ -45,16 +45,16 @@ main =
_.method <$> getRequestData :>>=
case _ of
Left POST -> onPost
Left method ->
Left method -> Ix.do
ignoreBody
:*> writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Method not supported: " <> show method)
Right customMethod ->
writeStatus statusMethodNotAllowed
closeHeaders
respond ("Method not supported: " <> show method)
Right customMethod -> Ix.do
ignoreBody
:*> writeStatus statusMethodNotAllowed
:*> closeHeaders
:*> respond ("Custom method not supported: " <> show customMethod)
writeStatus statusMethodNotAllowed
closeHeaders
respond ("Custom method not supported: " <> show customMethod)

-- Let's run it.
in runServer defaultOptionsWithLogging {} router
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
27 changes: 12 additions & 15 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 ((:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Data.Either (Either(..))
import Data.HTTP.Method (Method(GET))
import Data.Maybe (Maybe(Nothing, Just))
Expand Down Expand Up @@ -47,11 +48,11 @@ htmlWithStatus
(Conn req reqState res StatusLineOpen c)
(Conn req reqState 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 @@ -78,11 +79,9 @@ profileHandler
(Conn req reqState res StatusLineOpen { | AUTHENTICATION_ROWS (Maybe User) c })
(Conn req reqState res ResponseEnded { | AUTHENTICATION_ROWS (Maybe User) c })
Unit
profileHandler =
getConn :>>= \conn →
htmlWithStatus
statusOK
(view conn.components.authentication)
profileHandler = Ix.do
conn <- getConn
htmlWithStatus statusOK (view conn.components.authentication)
where
view =
case _ of
Expand Down Expand Up @@ -112,11 +111,9 @@ adminHandler
(Conn req reqState res StatusLineOpen { | AUTHORIZATION_ROWS Admin + AUTHENTICATION_ROWS User c })
(Conn req reqState res ResponseEnded { | AUTHORIZATION_ROWS Admin + AUTHENTICATION_ROWS User c })
Unit
adminHandler =
getConn :>>= \conn →
htmlWithStatus
statusOK
(view conn.components.authentication)
adminHandler = Ix.do
conn <- getConn
htmlWithStatus statusOK (view conn.components.authentication)
where
view (User { name }) =
section do
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
28 changes: 14 additions & 14 deletions examples/FormParser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Examples.FormParser where

import Prelude

import Control.Bind.Indexed (ibind)
import Control.Monad.Indexed ((:>>=), (:*>))
import Control.Monad.Indexed ((:>>=))
import Control.Monad.Indexed.Qualified as Ix
import Data.Either (Either(Right, Left))
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(Nothing, Just))
Expand Down Expand Up @@ -52,11 +52,11 @@ main =
(Conn HttpRequest reqState HttpResponse StatusLineOpen comp)
(Conn HttpRequest reqState HttpResponse ResponseEnded comp)
Unit
htmlWithStatus status x =
htmlWithStatus status x = Ix.do
writeStatus status
:*> contentType textHTML
:*> closeHeaders
:*> respond (render x)
contentType textHTML
closeHeaders
respond (render x)

handlePost :: forall comp
. Middleware
Expand All @@ -67,9 +67,9 @@ main =
handlePost =
parseForm :>>=
case _ of
Left err -> do
Left err -> Ix.do
liftEffect (log err)
:*> htmlWithStatus statusBadRequest
htmlWithStatus statusBadRequest
(p (text "Bad request, invalid form."))
Right form ->
case required "firstName" form of
Expand All @@ -89,16 +89,16 @@ main =
(Conn HttpRequest BodyUnread HttpResponse StatusLineOpen comp)
(Conn HttpRequest BodyRead HttpResponse ResponseEnded comp)
Unit
router = let bind = ibind in do
router = Ix.do
reqData <- getRequestData
case reqData.method of
Left GET -> do
_ <- ignoreBody
Left GET -> Ix.do
ignoreBody
htmlWithStatus statusOK (renderNameForm Nothing)
Left POST -> do
Left POST -> Ix.do
handlePost
method -> do
_ <- ignoreBody
method -> Ix.do
ignoreBody
htmlWithStatus statusMethodNotAllowed
(text ("Method not supported: " <> show method))

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
Loading

0 comments on commit 291fe12

Please sign in to comment.