plist to install haproxy as macOS launchd service

In this tutorial I am showing how to configure haproxy to run as system service under macOS Sierra, using launchd. This allows routing all traffic to apache that is running in user-space on port 8080 (so I don't have to restart/reload using sudo). Install haproxy using Homebrew (brew install haproxy), then create as root a new file /Library/LaunchDaemons/homebrew.mxcl.haproxy.plist with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.haproxy</string>
    <key>Program</key>
    <string>/usr/local/bin/haproxy</string>
    <key>ProgramArguments</key>
    <array>
      <string>haproxy</string>
      <string>-db</string>
      <string>-f</string>
      <string>/usr/local/etc/haproxy.conf</string>
      <string>-p</string>
      <string>/usr/local/var/run/haproxy.pid</string>
      <string>-V</string>
    </array>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/haproxy.log</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/haproxy-error.log</string>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

Enable and launch the service with the usual command during start, then forget about it:

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.haproxy.plist
sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.haproxy.plist

The haproxy 1.7 configuration to route all traffic to apache looks like this:

global
    daemon
    maxconn 25

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms


    listen http-in
        bind *:80
        server server1 127.0.0.1:8080 maxconn 25

Enjoy!

Bonus: replacing <string>-db</string> with <string>-d</string> enables verbose logging in /usr/local/var/log/haproxy.log.