diff --git a/examples/httpbin/.test.sh b/examples/httpbin/.test.sh new file mode 100755 index 000000000..5e31cf748 --- /dev/null +++ b/examples/httpbin/.test.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euxo pipefail + +for port in 8080 8081; do + wait_for_port "$port" + curl -vf "http://127.0.0.1:$port/headers" +done diff --git a/examples/httpbin/devenv.nix b/examples/httpbin/devenv.nix new file mode 100644 index 000000000..acc1ea056 --- /dev/null +++ b/examples/httpbin/devenv.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: + +{ + packages = [ pkgs.curl ]; + + services.httpbin = { + enable = true; + bind = [ "127.0.0.1:8080" "127.0.0.1:8081" ]; + }; +} diff --git a/src/modules/services/httpbin.nix b/src/modules/services/httpbin.nix new file mode 100644 index 000000000..bad3eafa4 --- /dev/null +++ b/src/modules/services/httpbin.nix @@ -0,0 +1,34 @@ +{ pkgs, lib, config, ... }: + +let + cfg = config.services.httpbin; + + qs = lib.escapeShellArgs; + + python = pkgs.python3.withPackages (ps: with ps; [ httpbin gunicorn gevent ]); + binds = lib.concatMap (addr: [ "-b" addr ]) cfg.bind; +in +{ + options.services.httpbin = { + enable = lib.mkEnableOption "httpbin"; + + bind = lib.mkOption { + type = with lib.types; listOf str; + default = [ "127.0.0.1:8080" ]; + description = "Addresses for httpbin to listen on."; + }; + + extraArgs = lib.mkOption { + type = with lib.types; listOf str; + default = [ ]; + description = "Gunicorn CLI arguments for httpbin."; + }; + }; + + config = lib.mkIf cfg.enable { + processes.httpbin.exec = '' + exec ${python}/bin/gunicorn httpbin:app -k gevent ${qs binds} ${qs cfg.extraArgs} + ''; + }; +} +