Clean up imports (#185)

* Clean up import declarations to only use qualified when necessary

* Remove unused imports
This commit is contained in:
Connor Prussin
2021-11-18 22:16:35 -08:00
committed by GitHub
parent f58aa94484
commit 8295d8755e
42 changed files with 1540 additions and 1505 deletions

View File

@@ -1,26 +1,26 @@
module Examples.AsyncResponse.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import Node.Encoding as Encoding
import Node.FS.Aff as FSAff
import Effect.Console (log)
import HTTPure (ServerM, Request, ResponseM, serve, ok)
import Node.Encoding (Encoding(UTF8))
import Node.FS.Aff (readTextFile)
-- | The path to the file containing the response to send
filePath :: String
filePath = "./docs/Examples/AsyncResponse/Hello"
-- | Say 'hello world!' when run
sayHello :: HTTPure.Request -> HTTPure.ResponseM
sayHello = const $ FSAff.readTextFile Encoding.UTF8 filePath >>= HTTPure.ok
sayHello :: Request -> ResponseM
sayHello = const $ readTextFile UTF8 filePath >>= ok
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 sayHello do
Console.log $ " ┌────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl localhost:8080 # => hello world! │"
Console.log $ " └────────────────────────────────────────────┘"
serve 8080 sayHello do
log " ┌────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080 # => hello world! │"
log " └────────────────────────────────────────────┘"

View File

@@ -1,24 +1,24 @@
module Examples.BinaryRequest.Main where
import Prelude
import Effect.Console as Console
import Effect.Console (log)
import Node.Buffer (Buffer)
import HTTPure as HTTPure
import HTTPure (Request, ResponseM, ServerM, toBuffer, serve, ok)
foreign import sha256sum :: Buffer -> String
-- | Respond with file's sha256sum
router :: HTTPure.Request -> HTTPure.ResponseM
router { body } = HTTPure.toBuffer body >>= sha256sum >>> HTTPure.ok
router :: Request -> ResponseM
router { body } = toBuffer body >>= sha256sum >>> ok
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌─────────────────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -XPOST --data-binary @circle.png localhost:8080 │"
Console.log $ " │ # => d5e776724dd5... │"
Console.log $ " └─────────────────────────────────────────────────────────┘"
serve 8080 router do
log " ┌─────────────────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -XPOST --data-binary @circle.png localhost:8080 │"
log " │ # => d5e776724dd5... │"
log " └─────────────────────────────────────────────────────────┘"

View File

@@ -1,28 +1,28 @@
module Examples.BinaryResponse.Main where
import Prelude
import Effect.Console as Console
import Node.FS.Aff as FS
import HTTPure as HTTPure
import Effect.Console (log)
import Node.FS.Aff (readFile)
import HTTPure (ServerM, Request, ResponseM, Headers, serve, ok', header)
-- | The path to the file containing the response to send
filePath :: String
filePath = "./docs/Examples/BinaryResponse/circle.png"
responseHeaders :: HTTPure.Headers
responseHeaders = HTTPure.header "Content-Type" "image/png"
responseHeaders :: Headers
responseHeaders = header "Content-Type" "image/png"
-- | Respond with image data when run
image :: HTTPure.Request -> HTTPure.ResponseM
image = const $ FS.readFile filePath >>= HTTPure.ok' responseHeaders
image :: Request -> ResponseM
image = const $ readFile filePath >>= ok' responseHeaders
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 image do
Console.log $ " ┌──────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -o circle.png localhost:8080 │"
Console.log $ " └──────────────────────────────────────┘"
serve 8080 image do
log " ┌──────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -o circle.png localhost:8080 │"
log " └──────────────────────────────────────┘"

View File

@@ -1,36 +1,35 @@
module Examples.Chunked.Main where
import Prelude
import Effect.Aff as Aff
import Effect.Class as EffectClass
import Effect.Console as Console
import HTTPure as HTTPure
import Node.ChildProcess as ChildProcess
import Node.Stream as Stream
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPure (ServerM, Request, ResponseM, serve, ok)
import Node.ChildProcess (stdout, spawn, defaultSpawnOptions)
import Node.Stream (Readable)
-- | Run a script and return it's stdout stream
runScript :: String -> Aff.Aff (Stream.Readable ())
runScript :: String -> Aff (Readable ())
runScript script =
EffectClass.liftEffect $ ChildProcess.stdout
<$> ChildProcess.spawn "sh" [ "-c", script ] ChildProcess.defaultSpawnOptions
liftEffect $ stdout <$> spawn "sh" [ "-c", script ] defaultSpawnOptions
-- | Say 'hello world!' in chunks when run
sayHello :: HTTPure.Request -> HTTPure.ResponseM
sayHello = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= HTTPure.ok
sayHello :: Request -> ResponseM
sayHello = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= ok
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 sayHello do
Console.log $ " ┌──────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -Nv localhost:8080 │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => < Transfer-Encoding: chunked │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => hello │"
Console.log $ " │ (1 second pause) │"
Console.log $ " │ # => world! │"
Console.log $ " └──────────────────────────────────────┘"
serve 8080 sayHello do
log " ┌──────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -Nv localhost:8080 │"
log " │ # => ... │"
log " │ # => < Transfer-Encoding: chunked │"
log " │ # => ... │"
log " │ # => hello │"
log " │ (1 second pause) │"
log " │ # => world! │"
log " └──────────────────────────────────────┘"

View File

@@ -4,34 +4,34 @@ import Prelude
import Control.Monad.Reader (class MonadAsk, ReaderT, asks, runReaderT)
import Effect.Aff (Aff)
import Effect.Aff.Class (class MonadAff)
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Console (log)
import HTTPure (Request, Response, ResponseM, ServerM, serve, ok)
-- | A type to hold the environment for our ReaderT
type Env = { name :: String }
-- | A middleware that introduces a ReaderT
readerMiddleware ::
(HTTPure.Request -> ReaderT Env Aff HTTPure.Response) ->
HTTPure.Request ->
HTTPure.ResponseM
(Request -> ReaderT Env Aff Response) ->
Request ->
ResponseM
readerMiddleware router request = do
runReaderT (router request) { name: "joe" }
-- | Say 'hello, joe' when run
sayHello :: forall m. MonadAff m => MonadAsk Env m => HTTPure.Request -> m HTTPure.Response
sayHello :: forall m. MonadAff m => MonadAsk Env m => Request -> m Response
sayHello _ = do
name <- asks _.name
HTTPure.ok $ "hello, " <> name
ok $ "hello, " <> name
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 (readerMiddleware sayHello) do
Console.log $ " ┌───────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -v localhost:8080 │"
Console.log $ " │ # => hello, joe │"
Console.log $ " └───────────────────────────────────────┘"
serve 8080 (readerMiddleware sayHello) do
log " ┌───────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -v localhost:8080 │"
log " │ # => hello, joe │"
log " └───────────────────────────────────────┘"

View File

@@ -1,29 +1,28 @@
module Examples.Headers.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import HTTPure ((!@))
import Effect.Console (log)
import HTTPure (ServerM, Headers, Request, ResponseM, (!@), header, serve, ok')
-- | The headers that will be included in every response.
responseHeaders :: HTTPure.Headers
responseHeaders = HTTPure.header "X-Example" "hello world!"
responseHeaders :: Headers
responseHeaders = header "X-Example" "hello world!"
-- | Route to the correct handler
router :: HTTPure.Request -> HTTPure.ResponseM
router { headers } = HTTPure.ok' responseHeaders $ headers !@ "X-Input"
router :: Request -> ResponseM
router { headers } = ok' responseHeaders $ headers !@ "X-Input"
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌──────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -H 'X-Input: test' -v localhost:8080 │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => ...< X-Example: hello world! │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => test │"
Console.log $ " └──────────────────────────────────────────────┘"
serve 8080 router do
log " ┌──────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -H 'X-Input: test' -v localhost:8080 │"
log " │ # => ... │"
log " │ # => ...< X-Example: hello world! │"
log " │ # => ... │"
log " │ # => test │"
log " └──────────────────────────────────────────────┘"

View File

@@ -1,16 +1,16 @@
module Examples.HelloWorld.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Console (log)
import HTTPure (ServerM, serve, ok)
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 (const $ HTTPure.ok "hello world!") do
Console.log $ " ┌────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl localhost:8080 # => hello world! │"
Console.log $ " └────────────────────────────────────────────┘"
serve 8080 (const $ ok "hello world!") do
log " ┌────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080 # => hello world! │"
log " └────────────────────────────────────────────┘"

View File

@@ -1,66 +1,68 @@
module Examples.Middleware.Main where
import Prelude
import Effect.Class as EffectClass
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, serve, fullPath, header, ok, ok')
-- | A middleware that logs at the beginning and end of each request
loggingMiddleware ::
(HTTPure.Request -> HTTPure.ResponseM) ->
HTTPure.Request ->
HTTPure.ResponseM
(Request -> ResponseM) ->
Request ->
ResponseM
loggingMiddleware router request = do
EffectClass.liftEffect $ Console.log $ "Request starting for " <> path
liftEffect $ log $ "Request starting for " <> path
response <- router request
EffectClass.liftEffect $ Console.log $ "Request ending for " <> path
liftEffect $ log $ "Request ending for " <> path
pure response
where
path = HTTPure.fullPath request
path = fullPath request
-- | A middleware that adds the X-Middleware header to the response, if it
-- | wasn't already in the response
headerMiddleware ::
(HTTPure.Request -> HTTPure.ResponseM) ->
HTTPure.Request ->
HTTPure.ResponseM
(Request -> ResponseM) ->
Request ->
ResponseM
headerMiddleware router request = do
response@{ headers } <- router request
pure $ response { headers = header <> headers }
pure $ response { headers = header' <> headers }
where
header = HTTPure.header "X-Middleware" "middleware"
header' = header "X-Middleware" "middleware"
-- | A middleware that sends the body "Middleware!" instead of running the
-- | router when requesting /middleware
pathMiddleware ::
(HTTPure.Request -> HTTPure.ResponseM) ->
HTTPure.Request ->
HTTPure.ResponseM
pathMiddleware _ { path: [ "middleware" ] } = HTTPure.ok "Middleware!"
(Request -> ResponseM) ->
Request ->
ResponseM
pathMiddleware _ { path: [ "middleware" ] } = ok "Middleware!"
pathMiddleware router request = router request
-- | Say 'hello' when run, and add a default value to the X-Middleware header
sayHello :: HTTPure.Request -> HTTPure.ResponseM
sayHello _ = HTTPure.ok' (HTTPure.header "X-Middleware" "router") "hello"
sayHello :: Request -> ResponseM
sayHello _ = ok' (header "X-Middleware" "router") "hello"
-- | The stack of middlewares to use for the server
middlewareStack :: (Request -> ResponseM) -> Request -> ResponseM
middlewareStack = loggingMiddleware <<< headerMiddleware <<< pathMiddleware
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 (middlewares sayHello) do
Console.log $ " ┌───────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -v localhost:8080 │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => ...< X-Middleware: router │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => hello │"
Console.log $ " │ > curl -v localhost:8080/middleware │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => ...< X-Middleware: middleware │"
Console.log $ " │ # => ... │"
Console.log $ " │ # => Middleware! │"
Console.log $ " └───────────────────────────────────────┘"
where
middlewares = loggingMiddleware <<< headerMiddleware <<< pathMiddleware
serve 8080 (middlewareStack sayHello) do
log " ┌───────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -v localhost:8080 │"
log " │ # => ... │"
log " │ # => ...< X-Middleware: router │"
log " │ # => ... │"
log " │ # => hello │"
log " │ > curl -v localhost:8080/middleware │"
log " │ # => ... │"
log " │ # => ...< X-Middleware: middleware │"
log " │ # => ... │"
log " │ # => Middleware! │"
log " └───────────────────────────────────────┘"

View File

@@ -1,25 +1,25 @@
module Examples.MultiRoute.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, serve, ok, notFound)
-- | Specify the routes
router :: HTTPure.Request -> HTTPure.ResponseM
router { path: [ "hello" ] } = HTTPure.ok "hello"
router { path: [ "goodbye" ] } = HTTPure.ok "goodbye"
router _ = HTTPure.notFound
router :: Request -> ResponseM
router { path: [ "hello" ] } = ok "hello"
router { path: [ "goodbye" ] } = ok "goodbye"
router _ = notFound
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl localhost:8080/hello │"
Console.log $ " │ # => hello │"
Console.log $ " │ > curl localhost:8080/goodbye │"
Console.log $ " │ # => goodbye │"
Console.log $ " └────────────────────────────────┘"
serve 8080 router do
log " ┌────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080/hello │"
log " │ # => hello │"
log " │ > curl localhost:8080/goodbye │"
log " │ # => goodbye │"
log " └────────────────────────────────┘"

View File

@@ -1,26 +1,25 @@
module Examples.PathSegments.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import HTTPure ((!@))
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, (!@), serve, ok)
-- | Specify the routes
router :: HTTPure.Request -> HTTPure.ResponseM
router :: Request -> ResponseM
router { path }
| path !@ 0 == "segment" = HTTPure.ok $ path !@ 1
| otherwise = HTTPure.ok $ show path
| path !@ 0 == "segment" = ok $ path !@ 1
| otherwise = ok $ show path
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌───────────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl localhost:8080/segment/<anything> │"
Console.log $ " │ # => <anything> │"
Console.log $ " │ > curl localhost:8080/<anything>/<else>/... │"
Console.log $ " │ # => [ <anything>, <else>, ... ] │"
Console.log $ " └───────────────────────────────────────────────┘"
serve 8080 router do
log " ┌───────────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080/segment/<anything> │"
log " │ # => <anything> │"
log " │ > curl localhost:8080/<anything>/<else>/... │"
log " │ # => [ <anything>, <else>, ... ] │"
log " └───────────────────────────────────────────────┘"

View File

@@ -1,22 +1,31 @@
module Examples.Post.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Console (log)
import HTTPure
( Request
, ResponseM
, ServerM
, Method(Post)
, serve
, ok
, notFound
, toString
)
-- | Route to the correct handler
router :: HTTPure.Request -> HTTPure.ResponseM
router { body, method: HTTPure.Post } = HTTPure.toString body >>= HTTPure.ok
router _ = HTTPure.notFound
router :: Request -> ResponseM
router { body, method: Post } = toString body >>= ok
router _ = notFound
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌───────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl -XPOST --data test localhost:8080 │"
Console.log $ " │ # => test │"
Console.log $ " └───────────────────────────────────────────┘"
serve 8080 router do
log " ┌───────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl -XPOST --data test localhost:8080 │"
log " │ # => test │"
log " └───────────────────────────────────────────┘"

View File

@@ -1,29 +1,28 @@
module Examples.QueryParameters.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import HTTPure ((!@), (!?))
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, (!@), (!?), serve, ok)
-- | Specify the routes
router :: HTTPure.Request -> HTTPure.ResponseM
router :: Request -> ResponseM
router { query }
| query !? "foo" = HTTPure.ok "foo"
| query !@ "bar" == "test" = HTTPure.ok "bar"
| otherwise = HTTPure.ok $ query !@ "baz"
| query !? "foo" = ok "foo"
| query !@ "bar" == "test" = ok "bar"
| otherwise = ok $ query !@ "baz"
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serve 8080 router do
Console.log $ " ┌───────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl localhost:8080?foo │"
Console.log $ " │ # => foo │"
Console.log $ " │ > curl localhost:8080?bar=test │"
Console.log $ " │ # => bar │"
Console.log $ " │ > curl localhost:8080?baz=<anything> │"
Console.log $ " │ # => <anything> │"
Console.log $ " └───────────────────────────────────────┘"
serve 8080 router do
log " ┌───────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl localhost:8080?foo │"
log " │ # => foo │"
log " │ > curl localhost:8080?bar=test │"
log " │ # => bar │"
log " │ > curl localhost:8080?baz=<anything> │"
log " │ # => <anything> │"
log " └───────────────────────────────────────┘"

View File

@@ -1,8 +1,8 @@
module Examples.SSL.Main where
import Prelude
import Effect.Console as Console
import HTTPure as HTTPure
import Effect.Console (log)
import HTTPure (Request, ResponseM, ServerM, serveSecure, ok)
-- | The path to the certificate file
cert :: String
@@ -13,17 +13,17 @@ key :: String
key = "./docs/Examples/SSL/Key.key"
-- | Say 'hello world!' when run
sayHello :: HTTPure.Request -> HTTPure.ResponseM
sayHello _ = HTTPure.ok "hello world!"
sayHello :: Request -> ResponseM
sayHello _ = ok "hello world!"
-- | Boot up the server
main :: HTTPure.ServerM
main :: ServerM
main =
HTTPure.serveSecure 8080 cert key sayHello do
Console.log $ " ┌───────────────────────────────────────────┐"
Console.log $ " │ Server now up on port 8080 │"
Console.log $ " │ │"
Console.log $ " │ To test, run: │"
Console.log $ " │ > curl --insecure https://localhost:8080 │"
Console.log $ " │ # => hello world! │"
Console.log $ " └───────────────────────────────────────────┘"
serveSecure 8080 cert key sayHello do
log " ┌───────────────────────────────────────────┐"
log " │ Server now up on port 8080 │"
log " │ │"
log " │ To test, run: │"
log " │ > curl --insecure https://localhost:8080 │"
log " │ # => hello world! │"
log " └───────────────────────────────────────────┘"