Add documentation for a shutdown hook (#174)

This commit is contained in:
Jan Schulte 2021-06-14 16:08:32 +01:00 committed by GitHub
parent 2f815ce336
commit 8f1730c00b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,3 +64,31 @@ main =
HTTPure.serveSecure' customSSLOptions customOptions router $ HTTPure.serveSecure' customSSLOptions customOptions router $
Console.log "Server up" Console.log "Server up"
``` ```
## Shutdown hook
To gracefully shut down a server you can add a shutdown hook. For this you will need to add the following dependencies:
```
posix-types
node-process
```
Then take the closing handler returned by `serve` and create a `SIGINT` and `SIGTERM` hook:
```purescript
import Prelude
import Data.Posix.Signal (Signal(..))
import Effect (Effect)
import Effect.Console as Console
import HTTPure as HTTPure
import Node.Process (onSignal)
main :: Effect Unit
main = do
closingHandler <- HTTPure.serve 8080 (const $ HTTPure.ok "hello world!") do
Console.log $ "Server now up on port 8080"
onSignal SIGINT $ closingHandler $ Console.log "Received SIGINT, stopping service now."
onSignal SIGTERM $ closingHandler $ Console.log "Received SIGTERM, stopping service now."
```