Git Product home page Git Product logo

warp-systemd's Introduction

warp-systemd

Systemd integration for the Warp Web Server (WAI).

Features

Usage

All systemd integrations are off by default. For example, you can modify SystemdSettings to enable the watchdog:

{-# LANGUAGE OverloadedStrings #-}

import Data.Function                                 ((&))
import qualified Network.Wai.Handler.Warp         as Warp
import qualified Network.Wai.Handler.Warp.Systemd as Systemd
import Network.Wai
import Network.HTTP.Types.Status                     (status200)

app :: Application
app _ respond =
  respond $ responseLBS status200 [] "Hello World"

main :: IO ()
main =
  Systemd.runSystemdWarp systemdSettings Warp.defaultSettings app
  where
    systemdSettings = Systemd.defaultSystemdSettings
      & Systemd.setHeartbeatInterval (Just 5)

Systemd Service Example

Let's configure your app to run as a socket-activated systemd service. We'll be using the sample app in example/Main.hs.

If these instructions look too manual, we also have an example NixOS module and test.

  1. Create a new service unit at /etc/systemd/system/warp.service that'll run your Haskell binary:

    [Unit]
    Description=Warp
    [Service]
    Type=notify
    WatchdogSec=30
    ExecStart=path-to-my-haskell-binary
    
  2. Create the socket unit at /etc/systemd/system/warp.socket that'll listen on port 80:

    [Unit]
    Description=Warp
    [Socket]
    ListenStream=80
    [Install]
    WantedBy=sockets.target
    
  3. Enable the socket unit:

    $ sudo systemctl enable --now warp.socket
  4. Verify that the server is running:

    $ systemctl status warp.socket warp.service
    ● warp.service - Warp
         Loaded: loaded (/etc/systemd/system/warp.service; linked; preset: enabled)
         Active: active (running) since Mon 2023-05-15 14:57:14 UTC; 19min ago
    TriggeredBy: ● warp.socket
       Main PID: 52528 (warp-systemd-ex)
             IP: 0B in, 0B out
             IO: 0B read, 0B written
          Tasks: 1 (limit: 19081)
         Memory: 2.1M
            CPU: 503ms
         CGroup: /system.slice/warp.service
                 └─52528 path-to-my-haskell-binary
    
    May 15 14:57:14 nixos systemd[1]: Starting Warp...
    May 15 14:57:14 nixos warp-systemd-example[52528]: Warp is socket-activated
    May 15 14:57:14 nixos systemd[1]: Started Warp.
    
    ● warp.socket - Warp
         Loaded: loaded (/etc/systemd/system/warp.socket; enabled; preset: enabled)
         Active: active (running) since Mon 2023-05-15 14:11:56 UTC; 1h 5min ago
       Triggers: ● warp.service
         Listen: [::]:80 (Stream)
             IP: 796B in, 690B out
             IO: 0B read, 0B written
          Tasks: 0 (limit: 19081)
         Memory: 8.0K
            CPU: 563us
         CGroup: /system.slice/warp.socket
    
    May 15 14:11:56 nixos systemd[1]: Listening on warp.socket.
    $ curl http://localhost
    Hello World⏎

warp-systemd's People

Contributors

domenkozar avatar roberth avatar sandydoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

cachix bradparker

warp-systemd's Issues

Add example systemd units to docs

E.g.:

# warp.service
[Unit]
Description=Warp
[Service]
Type=notify
WatchdogSec=5
ExecStart=path-to-my-haskell-binary
# warp.socket
[Unit]
Description=Warp
[Socket]
ListenStream=80
[Install]
WantedBy=sockets.target
systemctl enable --now warp.socket

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.