2022-06-11 16:46:03 +00:00
# HTTPurple Basics
2017-10-26 01:03:24 +00:00
2022-06-11 16:46:03 +00:00
This guide is a brief overview of the basics of creating a HTTPurple server.
## TOC
1. [Creating a Server ](#creating-a-server )
1. [Hot module reloading ](#hot-module-reloading )
1. [Further server settings ](#further-server-settings )
2017-10-26 01:03:24 +00:00
## Creating a Server
2022-06-11 16:46:03 +00:00
To create a server, use `HTTPurple.serve` .
2018-07-08 23:28:59 +00:00
Both of these functions take a port number, a router function, and an `Effect`
that will run once the server has booted. The signature of the router function
is:
2017-10-26 01:03:24 +00:00
```purescript
2022-06-11 16:46:03 +00:00
HTTPurple.Request route -> HTTPurple.ResponseM
2017-10-26 01:03:24 +00:00
```
For more details on routing, see the [Routing guide ](./Routing.md ). For more
details on responses, see the [Responses guide ](./Responses.md ). The router can
be composed with middleware; for more details, see the [Middleware
guide](./Middleware.md).
2022-06-11 16:46:03 +00:00
You can create an HTTPurple server using `HTTPurple.serve` :
2017-10-26 01:03:24 +00:00
```purescript
2022-06-11 16:46:03 +00:00
import Prelude hiding ((/))
import HTTPurple
data Route = Hello String
derive instance Generic Route _
route :: RouteDuplex' Route
route = mkRoute
{ "Hello": "hello" / segment
}
main :: ServerM
main =
serve { port: 8080 } { route, router }
where
router { route: Hello name } = ok $ "hello " < > name
2017-10-26 01:03:24 +00:00
```
2022-06-11 16:46:03 +00:00
`HTTPurple.serve` takes as arguments two records:
1. Server configuration - A record containing all additional settings that you want to pass. See [further server settings ](#further-server-settings ) for a list of all settings.
1. A record containing your route and a router for these routes. See the [routing guide ](./Routing.md ) for more information.
2018-02-25 02:49:45 +00:00
2018-02-09 05:46:45 +00:00
2022-06-11 16:46:03 +00:00
## Hot module reloading
With HTTPurple 🪁 you can easily set up a hot module reloading workflow:
Create an `index.js` with the content:
```javascript
import * as Main from './output/Main/index.js'
Main.main()
```
Add to `package.json` :
```json
...
"scripts": {
"hot": "spago build -w & nodemon \"node index.js\""
},
"type": "module",
...
```
Spin up:
```bash
npm run hot
```
Develop:
```bash
HTTPurple 🪁 up and running on http://0.0.0.0:8080
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node "node index.js" index.js`
HTTPurple 🪁 up and running on http://0.0.0.0:8080
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node "node index.js" index.js`
HTTPurple 🪁 up and running on http://0.0.0.0:8080
2018-02-09 05:46:45 +00:00
```
2022-06-11 16:46:03 +00:00
## Further server settings
2017-10-26 01:03:24 +00:00
2022-06-11 16:46:03 +00:00
HTTPurple 🪁 defines a series of settings that you can override.
2018-02-09 05:46:45 +00:00
2022-06-11 16:46:03 +00:00
Here is an example of the full list of server settings:
```
{
hostname: "localhost"
, port: 9000
, certFile: "./Certificate.cer"
, keyFile: "./Key.key"
, notFoundHandler: custom404Handler
, onStarted: log "Server started 🚀"
, closingHandler: NoClosingHandler
}
2018-02-09 05:46:45 +00:00
```
2022-06-11 16:46:03 +00:00
### SSL
2018-02-25 02:49:45 +00:00
2022-06-11 16:46:03 +00:00
**Note**: SSL is usually something that you want to handle at the infrastructure level and not within the application's http server. The SSL support is mainly here because HTTPure had it, but I might remove it in the near future if it hinders development.
2017-10-26 01:03:24 +00:00
2022-06-11 16:46:03 +00:00
You can create an SSL-enabled HTTPurple server using `HTTPurple.serve` by passing a certFile, a keyFile and an optionally a different port:
2017-10-26 01:03:24 +00:00
```purescript
2022-06-11 16:46:03 +00:00
main :: HTTPurple.ServerM
2017-10-26 01:03:24 +00:00
main =
2022-06-11 16:46:03 +00:00
HTTPurple.serve { port: 443, certFile : "./Certificate.cer", keyFile: "./Key.key" } { route, router }
...
2018-07-08 23:28:59 +00:00
```
2021-06-14 15:08:32 +00:00
2022-06-11 16:46:03 +00:00
You can look at [the SSL Example ](./Examples/SSL/Main.purs ), which uses this
method to create the server.
2021-06-14 15:08:32 +00:00
2022-06-11 16:46:03 +00:00
### Closing handler
2021-11-19 06:16:35 +00:00
2022-06-11 16:46:03 +00:00
HTTPurple 🪁 comes with a default closing handler, so `Ctrl+x` just stops the server.
you can switch off this behaviour by passing
```purescript
{ closingHandler: NoClosingHandler }
2021-06-14 15:08:32 +00:00
```
2022-06-11 16:46:03 +00:00
to `serve` and define your own closing handler:
2021-06-14 15:08:32 +00:00
```purescript
import Prelude
2021-11-19 06:16:35 +00:00
import Data.Posix.Signal (Signal(SIGINT, SIGTERM))
2021-06-14 15:08:32 +00:00
import Effect (Effect)
2021-11-19 06:16:35 +00:00
import Effect.Console (log)
2022-06-11 16:46:03 +00:00
import HTTPurple (serve, ok)
2021-06-14 15:08:32 +00:00
import Node.Process (onSignal)
main :: Effect Unit
main = do
2022-06-11 16:46:03 +00:00
closingHandler < - serve 8080 { route , router }
-- do something with closingHandler
2021-06-14 15:08:32 +00:00
```