From 8f1730c00baca6c9e78f4ea20ce77fdda6f2a497 Mon Sep 17 00:00:00 2001 From: Jan Schulte <52407559+hivemind-js@users.noreply.github.com> Date: Mon, 14 Jun 2021 16:08:32 +0100 Subject: [PATCH] Add documentation for a shutdown hook (#174) --- docs/Basics.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/Basics.md b/docs/Basics.md index 7e3a3c8..9b88191 100644 --- a/docs/Basics.md +++ b/docs/Basics.md @@ -64,3 +64,31 @@ main = HTTPure.serveSecure' customSSLOptions customOptions router $ 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." +```