Code cleanup (#54)

This commit is contained in:
Connor Prussin 2017-07-22 23:13:47 -07:00 committed by Connor Prussin
parent 8210f35e4f
commit b88b905dad
4 changed files with 18 additions and 24 deletions

View File

@ -25,15 +25,13 @@ read request = Aff.makeAff \_ success -> do
let stream = HTTP.requestAsStream request let stream = HTTP.requestAsStream request
buf <- ST.newSTRef "" buf <- ST.newSTRef ""
Stream.onDataString stream Encoding.UTF8 \str -> Stream.onDataString stream Encoding.UTF8 \str ->
ST.modifySTRef buf ((<>) str) >>= \_ -> pure unit void $ ST.modifySTRef buf ((<>) str)
Stream.onEnd stream $ ST.readSTRef buf >>= success Stream.onEnd stream $ ST.readSTRef buf >>= success
-- | Write a body to the given HTTP Response and close it. -- | Write a body to the given HTTP Response and close it.
write :: forall e. HTTP.Response -> Body -> Eff.Eff (http :: HTTP.HTTP | e) Unit write :: forall e. HTTP.Response -> Body -> Eff.Eff (http :: HTTP.HTTP | e) Unit
write response body = do write response body = void do
_ <- Stream.writeString stream Encoding.UTF8 body noop _ <- Stream.writeString stream Encoding.UTF8 body $ pure unit
Stream.end stream noop Stream.end stream $ pure unit
noop
where where
stream = HTTP.responseAsStream response stream = HTTP.responseAsStream response
noop = pure unit

View File

@ -27,9 +27,8 @@ write :: forall e.
HTTP.Response -> HTTP.Response ->
Headers -> Headers ->
Eff.Eff (http :: HTTP.HTTP | e) Unit Eff.Eff (http :: HTTP.HTTP | e) Unit
write response headers = do write response headers =
_ <- Traversable.traverse writeHeader $ StrMap.keys headers void $ Traversable.traverse writeHeader $ StrMap.keys headers
pure unit
where where
getHeader header = Maybe.fromMaybe "" $ StrMap.lookup header headers getHeader header = Maybe.fromMaybe "" $ StrMap.lookup header headers
writeHeader header = HTTP.setHeader response header $ getHeader header writeHeader header = HTTP.setHeader response header $ getHeader header

View File

@ -27,11 +27,10 @@ handleRequest :: forall e.
HTTP.Request -> HTTP.Request ->
HTTP.Response -> HTTP.Response ->
ServerM e ServerM e
handleRequest router request response = do handleRequest router request response =
_ <- Aff.runAff (\_ -> pure unit) (\_ -> pure unit) do void $ Aff.runAff (\_ -> pure unit) (\_ -> pure unit) do
req <- Request.fromHTTPRequest request req <- Request.fromHTTPRequest request
EffClass.liftEff $ router req >>= Response.send response EffClass.liftEff $ router req >>= Response.send response
pure unit
-- | Given an options object, a function mapping Request to ResponseM, and an -- | Given an options object, a function mapping Request to ResponseM, and an
-- | HTTPureM containing effects to run on boot, creates and runs a HTTPure -- | HTTPureM containing effects to run on boot, creates and runs a HTTPure

View File

@ -7,7 +7,7 @@ import Control.Monad.Eff as Eff
import Control.Monad.Eff.Exception as Exception import Control.Monad.Eff.Exception as Exception
import Control.Monad.ST as ST import Control.Monad.ST as ST
import Data.Maybe as Maybe import Data.Maybe as Maybe
import Data.Options as Options import Data.Options ((:=))
import Data.String as StringUtil import Data.String as StringUtil
import Data.StrMap as StrMap import Data.StrMap as StrMap
import Node.Encoding as Encoding import Node.Encoding as Encoding
@ -53,26 +53,24 @@ request :: forall e.
String -> String ->
String -> String ->
Aff.Aff (http :: HTTP.HTTP | e) HTTPClient.Response Aff.Aff (http :: HTTP.HTTP | e) HTTPClient.Response
request port method headers path body = Aff.makeAff \_ success -> do request port method headers path body = Aff.makeAff \_ success -> void do
req <- HTTPClient.request options success req <- HTTPClient.request options success
let stream = HTTPClient.requestAsStream req let stream = HTTPClient.requestAsStream req
_ <- Stream.writeString stream Encoding.UTF8 body noop _ <- Stream.writeString stream Encoding.UTF8 body $ pure unit
Stream.end stream noop Stream.end stream $ pure unit
noop
where where
noop = pure unit
options = options =
HTTPClient.method `Options.assoc` method <> HTTPClient.method := method <>
HTTPClient.hostname `Options.assoc` "localhost" <> HTTPClient.hostname := "localhost" <>
HTTPClient.port `Options.assoc` port <> HTTPClient.port := port <>
HTTPClient.path `Options.assoc` path <> HTTPClient.path := path <>
HTTPClient.headers `Options.assoc` HTTPClient.RequestHeaders headers HTTPClient.headers := HTTPClient.RequestHeaders headers
-- | Given an ST String buffer and a new string, concatenate that new string -- | Given an ST String buffer and a new string, concatenate that new string
-- | onto the ST buffer. -- | onto the ST buffer.
concat :: forall e s. concat :: forall e s.
ST.STRef s String -> String -> Eff.Eff (st :: ST.ST s | e) Unit ST.STRef s String -> String -> Eff.Eff (st :: ST.ST s | e) Unit
concat buf new = ST.modifySTRef buf (\old -> old <> new) >>= (\_ -> pure unit) concat buf new = void $ ST.modifySTRef buf ((<>) new)
-- | Convert a request to an Aff containing the string with the response body. -- | Convert a request to an Aff containing the string with the response body.
toString :: forall e. toString :: forall e.