Add hello world example to readme & format code

This commit is contained in:
sigma-andex 2022-05-22 17:54:11 +01:00
parent 345675d5bd
commit fcf532bd71
No known key found for this signature in database
GPG Key ID: C5F79968835855AB
10 changed files with 91 additions and 54 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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)