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." +```