Add hello world example to readme & format code
This commit is contained in:
parent
345675d5bd
commit
fcf532bd71
47
Readme.md
47
Readme.md
@ -16,15 +16,52 @@ spago install httpurple
|
||||
```purescript
|
||||
module Main where
|
||||
|
||||
import Prelude
|
||||
import Prelude hiding ((/))
|
||||
|
||||
import Effect.Console (log)
|
||||
import HTTPurple (ServerM, serve, ok)
|
||||
import Data.Generic.Rep (class Generic)
|
||||
import HTTPurple (ServerM, ok, serve)
|
||||
import Routing.Duplex (RouteDuplex', root, segment)
|
||||
import Routing.Duplex.Generic (sum)
|
||||
import Routing.Duplex.Generic.Syntax ((/))
|
||||
|
||||
data Route = Hello String
|
||||
|
||||
derive instance Generic Route _
|
||||
|
||||
route :: RouteDuplex' Route
|
||||
route = root $ sum
|
||||
{ "Hello": "hello" / segment
|
||||
}
|
||||
|
||||
main :: ServerM
|
||||
main = serve 8080 router $ log "Server now up on port 8080"
|
||||
main =
|
||||
serve { port: 8080 } { route, router }
|
||||
where
|
||||
router _ = ok "hello world!"
|
||||
router { route: Hello name } = ok $ "hello " <> name
|
||||
```
|
||||
|
||||
then start the server
|
||||
|
||||
```bash
|
||||
➜ spago run
|
||||
Src Lib All
|
||||
Warnings 0 0 0
|
||||
Errors 0 0 0
|
||||
[info] Build succeeded.
|
||||
HTTPurple 🪁 up and running on http://0.0.0.0:8080
|
||||
```
|
||||
|
||||
query your server, e.g. using [httpie](https://httpie.io/)
|
||||
|
||||
```bash
|
||||
➜ http http://localhost:8080/hello/🗺
|
||||
HTTP/1.1 200 OK
|
||||
Connection: keep-alive
|
||||
Content-Length: 10
|
||||
Date: Sun, 22 May 2022 16:50:52 GMT
|
||||
Keep-Alive: timeout=5
|
||||
|
||||
hello 🗺
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
@ -30,7 +30,7 @@ router { route: SayHello } = readTextFile UTF8 filePath >>= ok
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
where
|
||||
onStarted = do
|
||||
log " ┌────────────────────────────────────────────┐"
|
||||
|
@ -28,7 +28,7 @@ router { body } = toBuffer body >>= sha256sum >>> ok
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
where
|
||||
onStarted = do
|
||||
log " ┌─────────────────────────────────────────────────────────┐"
|
||||
|
@ -18,6 +18,7 @@ route :: RD.RouteDuplex' Route
|
||||
route = RD.root $ RG.sum
|
||||
{ "SayHello": RG.noArgs
|
||||
}
|
||||
|
||||
-- | The path to the file containing the response to send
|
||||
filePath :: String
|
||||
filePath = "./docs/Examples/BinaryResponse/circle.png"
|
||||
@ -32,7 +33,7 @@ router = const $ readFile filePath >>= ok' responseHeaders
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
where
|
||||
onStarted = do
|
||||
log " ┌──────────────────────────────────────┐"
|
||||
|
@ -34,7 +34,7 @@ router = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= ok
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
serve { port: 8080, onStarted } { route, router }
|
||||
where
|
||||
onStarted = do
|
||||
log " ┌──────────────────────────────────────┐"
|
||||
|
@ -42,7 +42,7 @@ sayHello _ = do
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, onStarted } { route, router: readerMiddleware sayHello }
|
||||
serve { port: 8080, onStarted } { route, router: readerMiddleware sayHello }
|
||||
where
|
||||
onStarted = do
|
||||
log " ┌───────────────────────────────────────┐"
|
||||
|
@ -3,7 +3,6 @@ module Examples.HelloWorld.Main where
|
||||
import Prelude
|
||||
|
||||
import Data.Generic.Rep (class Generic)
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Effect.Console (log)
|
||||
import HTTPurple (ServerM, ok, serve)
|
||||
import Routing.Duplex as RD
|
||||
|
@ -35,14 +35,14 @@ sayHello _ = ok "hello world!"
|
||||
-- | Boot up the server
|
||||
main :: ServerM
|
||||
main =
|
||||
serve { port: 8080, certFile: cert, keyFile: key, onStarted } { route, router: sayHello }
|
||||
where
|
||||
onStarted =
|
||||
do
|
||||
log " ┌───────────────────────────────────────────┐"
|
||||
log " │ Server now up on port 8080 │"
|
||||
log " │ │"
|
||||
log " │ To test, run: │"
|
||||
log " │ > curl --insecure https://localhost:8080 │"
|
||||
log " │ # => hello world! │"
|
||||
log " └───────────────────────────────────────────┘"
|
||||
serve { port: 8080, certFile: cert, keyFile: key, onStarted } { route, router: sayHello }
|
||||
where
|
||||
onStarted =
|
||||
do
|
||||
log " ┌───────────────────────────────────────────┐"
|
||||
log " │ Server now up on port 8080 │"
|
||||
log " │ │"
|
||||
log " │ To test, run: │"
|
||||
log " │ > curl --insecure https://localhost:8080 │"
|
||||
log " │ # => hello world! │"
|
||||
log " └───────────────────────────────────────────┘"
|
||||
|
@ -28,13 +28,13 @@ derive instance Generic Route _
|
||||
|
||||
route :: RD.RouteDuplex' Route
|
||||
route = RD.root $ G.sum
|
||||
{ "Test": "test" ? { a : RD.optional <<< RD.string }
|
||||
{ "Test": "test" ? { a: RD.optional <<< RD.string }
|
||||
}
|
||||
|
||||
getRight :: forall a b. Aff (Either a b) -> Aff b
|
||||
getRight input = input >>= either (const throwLeft) pure
|
||||
where
|
||||
throwLeft = throwError (error "Invalid route")
|
||||
throwLeft = throwError (error "Invalid route")
|
||||
|
||||
fromHTTPRequestSpec :: Test
|
||||
fromHTTPRequestSpec =
|
||||
@ -72,37 +72,37 @@ fromHTTPRequestSpec =
|
||||
-- it "is correct" do
|
||||
-- mock <- mockRequest' "/foo/bar" # getRight
|
||||
-- fullPath mock ?= "/foo/bar"
|
||||
-- describe "with empty path segments" do
|
||||
-- it "strips the empty segments" do
|
||||
-- mock <- mockRequest' "//foo////bar/"
|
||||
-- fullPath mock ?= "/foo/bar"
|
||||
-- describe "with only query parameters" do
|
||||
-- it "is correct" do
|
||||
-- mock <- mockRequest' "?a=b&c=d"
|
||||
-- fullPath mock ?= "/?a=b&c=d"
|
||||
-- describe "with only empty query parameters" do
|
||||
-- it "is has the default value of '' for the empty parameters" do
|
||||
-- mock <- mockRequest' "?a"
|
||||
-- fullPath mock ?= "/?a="
|
||||
-- describe "with query parameters that have special characters" do
|
||||
-- it "percent encodes query params" do
|
||||
-- mock <- mockRequest' "?a=%3Fx%3Dtest"
|
||||
-- fullPath mock ?= "/?a=%3Fx%3Dtest"
|
||||
-- describe "with empty query parameters" do
|
||||
-- it "strips out the empty arameters" do
|
||||
-- mock <- mockRequest' "?a=b&&&"
|
||||
-- fullPath mock ?= "/?a=b"
|
||||
-- describe "with a mix of segments and query parameters" do
|
||||
-- it "is correct" do
|
||||
-- mock <- mockRequest' "/foo///bar/?&a=b&&c"
|
||||
-- fullPath mock ?= "/foo/bar?a=b&c="
|
||||
-- where
|
||||
-- mockHTTPRequest path = mockRequest "" "POST" path "body" []
|
||||
-- describe "with empty path segments" do
|
||||
-- it "strips the empty segments" do
|
||||
-- mock <- mockRequest' "//foo////bar/"
|
||||
-- fullPath mock ?= "/foo/bar"
|
||||
-- describe "with only query parameters" do
|
||||
-- it "is correct" do
|
||||
-- mock <- mockRequest' "?a=b&c=d"
|
||||
-- fullPath mock ?= "/?a=b&c=d"
|
||||
-- describe "with only empty query parameters" do
|
||||
-- it "is has the default value of '' for the empty parameters" do
|
||||
-- mock <- mockRequest' "?a"
|
||||
-- fullPath mock ?= "/?a="
|
||||
-- describe "with query parameters that have special characters" do
|
||||
-- it "percent encodes query params" do
|
||||
-- mock <- mockRequest' "?a=%3Fx%3Dtest"
|
||||
-- fullPath mock ?= "/?a=%3Fx%3Dtest"
|
||||
-- describe "with empty query parameters" do
|
||||
-- it "strips out the empty arameters" do
|
||||
-- mock <- mockRequest' "?a=b&&&"
|
||||
-- fullPath mock ?= "/?a=b"
|
||||
-- describe "with a mix of segments and query parameters" do
|
||||
-- it "is correct" do
|
||||
-- mock <- mockRequest' "/foo///bar/?&a=b&&c"
|
||||
-- fullPath mock ?= "/foo/bar?a=b&c="
|
||||
-- where
|
||||
-- mockHTTPRequest path = mockRequest "" "POST" path "body" []
|
||||
|
||||
-- mockRequest' path = mockHTTPRequest path >>= fromHTTPRequest route
|
||||
-- mockRequest' path = mockHTTPRequest path >>= fromHTTPRequest route
|
||||
|
||||
requestSpec :: Test
|
||||
requestSpec =
|
||||
describe "Request" do
|
||||
fromHTTPRequestSpec
|
||||
--fullPathSpec
|
||||
--fullPathSpec
|
||||
|
@ -16,12 +16,12 @@ import HTTPurple.Server (serve)
|
||||
import HTTPurple.Server as Server
|
||||
import Node.Encoding (Encoding(UTF8))
|
||||
import Node.FS.Sync (readTextFile)
|
||||
import Node.HTTP.Secure (key, keyString, cert, certString)
|
||||
import Node.HTTP.Secure (cert, certString, key, keyString)
|
||||
import Routing.Duplex (RouteDuplex')
|
||||
import Routing.Duplex as RD
|
||||
import Routing.Duplex.Generic as G
|
||||
import Routing.Duplex.Generic as RG
|
||||
import Test.HTTPurple.TestHelpers (Test, (?=), get, get', getStatus)
|
||||
import Test.HTTPurple.TestHelpers (Test, get, get', getStatus, (?=))
|
||||
import Test.Spec (describe, it)
|
||||
import Test.Spec.Assertions (expectError)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user