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
|
```purescript
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Prelude
|
import Prelude hiding ((/))
|
||||||
|
|
||||||
import Effect.Console (log)
|
import Data.Generic.Rep (class Generic)
|
||||||
import HTTPurple (ServerM, serve, ok)
|
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 :: ServerM
|
||||||
main = serve 8080 router $ log "Server now up on port 8080"
|
main =
|
||||||
|
serve { port: 8080 } { route, router }
|
||||||
where
|
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
|
## Documentation
|
||||||
|
@ -18,6 +18,7 @@ route :: RD.RouteDuplex' Route
|
|||||||
route = RD.root $ RG.sum
|
route = RD.root $ RG.sum
|
||||||
{ "SayHello": RG.noArgs
|
{ "SayHello": RG.noArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | The path to the file containing the response to send
|
-- | The path to the file containing the response to send
|
||||||
filePath :: String
|
filePath :: String
|
||||||
filePath = "./docs/Examples/BinaryResponse/circle.png"
|
filePath = "./docs/Examples/BinaryResponse/circle.png"
|
||||||
|
@ -3,7 +3,6 @@ module Examples.HelloWorld.Main where
|
|||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
import Data.Generic.Rep (class Generic)
|
import Data.Generic.Rep (class Generic)
|
||||||
import Data.Maybe (Maybe(..))
|
|
||||||
import Effect.Console (log)
|
import Effect.Console (log)
|
||||||
import HTTPurple (ServerM, ok, serve)
|
import HTTPurple (ServerM, ok, serve)
|
||||||
import Routing.Duplex as RD
|
import Routing.Duplex as RD
|
||||||
|
@ -28,7 +28,7 @@ derive instance Generic Route _
|
|||||||
|
|
||||||
route :: RD.RouteDuplex' Route
|
route :: RD.RouteDuplex' Route
|
||||||
route = RD.root $ G.sum
|
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 :: forall a b. Aff (Either a b) -> Aff b
|
||||||
@ -72,37 +72,37 @@ fromHTTPRequestSpec =
|
|||||||
-- it "is correct" do
|
-- it "is correct" do
|
||||||
-- mock <- mockRequest' "/foo/bar" # getRight
|
-- mock <- mockRequest' "/foo/bar" # getRight
|
||||||
-- fullPath mock ?= "/foo/bar"
|
-- fullPath mock ?= "/foo/bar"
|
||||||
-- describe "with empty path segments" do
|
-- describe "with empty path segments" do
|
||||||
-- it "strips the empty segments" do
|
-- it "strips the empty segments" do
|
||||||
-- mock <- mockRequest' "//foo////bar/"
|
-- mock <- mockRequest' "//foo////bar/"
|
||||||
-- fullPath mock ?= "/foo/bar"
|
-- fullPath mock ?= "/foo/bar"
|
||||||
-- describe "with only query parameters" do
|
-- describe "with only query parameters" do
|
||||||
-- it "is correct" do
|
-- it "is correct" do
|
||||||
-- mock <- mockRequest' "?a=b&c=d"
|
-- mock <- mockRequest' "?a=b&c=d"
|
||||||
-- fullPath mock ?= "/?a=b&c=d"
|
-- fullPath mock ?= "/?a=b&c=d"
|
||||||
-- describe "with only empty query parameters" do
|
-- describe "with only empty query parameters" do
|
||||||
-- it "is has the default value of '' for the empty parameters" do
|
-- it "is has the default value of '' for the empty parameters" do
|
||||||
-- mock <- mockRequest' "?a"
|
-- mock <- mockRequest' "?a"
|
||||||
-- fullPath mock ?= "/?a="
|
-- fullPath mock ?= "/?a="
|
||||||
-- describe "with query parameters that have special characters" do
|
-- describe "with query parameters that have special characters" do
|
||||||
-- it "percent encodes query params" do
|
-- it "percent encodes query params" do
|
||||||
-- mock <- mockRequest' "?a=%3Fx%3Dtest"
|
-- mock <- mockRequest' "?a=%3Fx%3Dtest"
|
||||||
-- fullPath mock ?= "/?a=%3Fx%3Dtest"
|
-- fullPath mock ?= "/?a=%3Fx%3Dtest"
|
||||||
-- describe "with empty query parameters" do
|
-- describe "with empty query parameters" do
|
||||||
-- it "strips out the empty arameters" do
|
-- it "strips out the empty arameters" do
|
||||||
-- mock <- mockRequest' "?a=b&&&"
|
-- mock <- mockRequest' "?a=b&&&"
|
||||||
-- fullPath mock ?= "/?a=b"
|
-- fullPath mock ?= "/?a=b"
|
||||||
-- describe "with a mix of segments and query parameters" do
|
-- describe "with a mix of segments and query parameters" do
|
||||||
-- it "is correct" do
|
-- it "is correct" do
|
||||||
-- mock <- mockRequest' "/foo///bar/?&a=b&&c"
|
-- mock <- mockRequest' "/foo///bar/?&a=b&&c"
|
||||||
-- fullPath mock ?= "/foo/bar?a=b&c="
|
-- fullPath mock ?= "/foo/bar?a=b&c="
|
||||||
-- where
|
-- where
|
||||||
-- mockHTTPRequest path = mockRequest "" "POST" path "body" []
|
-- mockHTTPRequest path = mockRequest "" "POST" path "body" []
|
||||||
|
|
||||||
-- mockRequest' path = mockHTTPRequest path >>= fromHTTPRequest route
|
-- mockRequest' path = mockHTTPRequest path >>= fromHTTPRequest route
|
||||||
|
|
||||||
requestSpec :: Test
|
requestSpec :: Test
|
||||||
requestSpec =
|
requestSpec =
|
||||||
describe "Request" do
|
describe "Request" do
|
||||||
fromHTTPRequestSpec
|
fromHTTPRequestSpec
|
||||||
--fullPathSpec
|
--fullPathSpec
|
||||||
|
@ -16,12 +16,12 @@ import HTTPurple.Server (serve)
|
|||||||
import HTTPurple.Server as Server
|
import HTTPurple.Server as Server
|
||||||
import Node.Encoding (Encoding(UTF8))
|
import Node.Encoding (Encoding(UTF8))
|
||||||
import Node.FS.Sync (readTextFile)
|
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 (RouteDuplex')
|
||||||
import Routing.Duplex as RD
|
import Routing.Duplex as RD
|
||||||
import Routing.Duplex.Generic as G
|
import Routing.Duplex.Generic as G
|
||||||
import Routing.Duplex.Generic as RG
|
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 (describe, it)
|
||||||
import Test.Spec.Assertions (expectError)
|
import Test.Spec.Assertions (expectError)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user