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
|
||||||
|
@ -30,7 +30,7 @@ router { route: SayHello } = readTextFile UTF8 filePath >>= ok
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, onStarted } { route, router }
|
serve { port: 8080, onStarted } { route, router }
|
||||||
where
|
where
|
||||||
onStarted = do
|
onStarted = do
|
||||||
log " ┌────────────────────────────────────────────┐"
|
log " ┌────────────────────────────────────────────┐"
|
||||||
|
@ -28,7 +28,7 @@ router { body } = toBuffer body >>= sha256sum >>> ok
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, onStarted } { route, router }
|
serve { port: 8080, onStarted } { route, router }
|
||||||
where
|
where
|
||||||
onStarted = do
|
onStarted = do
|
||||||
log " ┌─────────────────────────────────────────────────────────┐"
|
log " ┌─────────────────────────────────────────────────────────┐"
|
||||||
|
@ -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"
|
||||||
@ -32,7 +33,7 @@ router = const $ readFile filePath >>= ok' responseHeaders
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, onStarted } { route, router }
|
serve { port: 8080, onStarted } { route, router }
|
||||||
where
|
where
|
||||||
onStarted = do
|
onStarted = do
|
||||||
log " ┌──────────────────────────────────────┐"
|
log " ┌──────────────────────────────────────┐"
|
||||||
|
@ -34,7 +34,7 @@ router = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= ok
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, onStarted } { route, router }
|
serve { port: 8080, onStarted } { route, router }
|
||||||
where
|
where
|
||||||
onStarted = do
|
onStarted = do
|
||||||
log " ┌──────────────────────────────────────┐"
|
log " ┌──────────────────────────────────────┐"
|
||||||
|
@ -42,7 +42,7 @@ sayHello _ = do
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, onStarted } { route, router: readerMiddleware sayHello }
|
serve { port: 8080, onStarted } { route, router: readerMiddleware sayHello }
|
||||||
where
|
where
|
||||||
onStarted = do
|
onStarted = do
|
||||||
log " ┌───────────────────────────────────────┐"
|
log " ┌───────────────────────────────────────┐"
|
||||||
|
@ -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
|
||||||
|
@ -35,14 +35,14 @@ sayHello _ = ok "hello world!"
|
|||||||
-- | Boot up the server
|
-- | Boot up the server
|
||||||
main :: ServerM
|
main :: ServerM
|
||||||
main =
|
main =
|
||||||
serve { port: 8080, certFile: cert, keyFile: key, onStarted } { route, router: sayHello }
|
serve { port: 8080, certFile: cert, keyFile: key, onStarted } { route, router: sayHello }
|
||||||
where
|
where
|
||||||
onStarted =
|
onStarted =
|
||||||
do
|
do
|
||||||
log " ┌───────────────────────────────────────────┐"
|
log " ┌───────────────────────────────────────────┐"
|
||||||
log " │ Server now up on port 8080 │"
|
log " │ Server now up on port 8080 │"
|
||||||
log " │ │"
|
log " │ │"
|
||||||
log " │ To test, run: │"
|
log " │ To test, run: │"
|
||||||
log " │ > curl --insecure https://localhost:8080 │"
|
log " │ > curl --insecure https://localhost:8080 │"
|
||||||
log " │ # => hello world! │"
|
log " │ # => hello world! │"
|
||||||
log " └───────────────────────────────────────────┘"
|
log " └───────────────────────────────────────────┘"
|
||||||
|
@ -28,13 +28,13 @@ 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
|
||||||
getRight input = input >>= either (const throwLeft) pure
|
getRight input = input >>= either (const throwLeft) pure
|
||||||
where
|
where
|
||||||
throwLeft = throwError (error "Invalid route")
|
throwLeft = throwError (error "Invalid route")
|
||||||
|
|
||||||
fromHTTPRequestSpec :: Test
|
fromHTTPRequestSpec :: Test
|
||||||
fromHTTPRequestSpec =
|
fromHTTPRequestSpec =
|
||||||
@ -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