]> git.scottworley.com Git - hellowebpy/commitdiff
A minimal "hello world" python web server v1.0.0
authorScott Worley <scottworley@scottworley.com>
Wed, 5 Apr 2023 03:22:43 +0000 (20:22 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 5 Apr 2023 03:30:32 +0000 (20:30 -0700)
.gitignore [new file with mode: 0644]
Changelog [new file with mode: 0644]
README.md [new file with mode: 0644]
default.nix [new file with mode: 0644]
git-pre-commit-hook [new file with mode: 0755]
hellowebpy.py [new file with mode: 0644]
setup.py [new file with mode: 0644]
test.sh [new file with mode: 0755]
tests/says-hello.sh [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..1d26af3
--- /dev/null
@@ -0,0 +1,2 @@
+.mypy_cache
+result
diff --git a/Changelog b/Changelog
new file mode 100644 (file)
index 0000000..a9e9056
--- /dev/null
+++ b/Changelog
@@ -0,0 +1,3 @@
+## [1.0.0] - 2023-04-04
+
+Initial release
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..f892d0c
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+A minimal "hello world" python web server.
diff --git a/default.nix b/default.nix
new file mode 100644 (file)
index 0000000..c256aea
--- /dev/null
@@ -0,0 +1,19 @@
+{ pkgs ? import <nixpkgs> { }, lint ? false }:
+
+pkgs.python3Packages.callPackage
+({ autopep8, buildPythonPackage, curl, lib, mypy, pylint, }:
+  buildPythonPackage rec {
+    pname = "hellowebpy";
+    version = "1.0.0";
+    src = lib.cleanSource ./.;
+    PIP_DISABLE_PIP_VERSION_CHECK = 1;
+    checkInputs = [ curl mypy ] ++ lib.optionals lint [ autopep8 pylint ];
+    doCheck = true;
+    checkPhase = "./test.sh";
+    meta = {
+      description = "A minimal 'hello world' python web server.";
+      homepage = "https://git.scottworley.com/hellowebpy";
+      license = pkgs.lib.licenses.gpl3;
+      maintainers = with pkgs.lib.maintainers; [ chkno ];
+    };
+  }) { }
diff --git a/git-pre-commit-hook b/git-pre-commit-hook
new file mode 100755 (executable)
index 0000000..85b4445
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+
+# Copy me to .git/hooks/pre-commit
+
+set -e
+
+cleanup() {
+  if [[ "$D" && -e "$D" ]];then
+    rm -rf "$D"
+  fi
+}
+trap cleanup EXIT
+
+D=$(mktemp -d)
+[[ "$D" && -d "$D" ]]
+
+git checkout-index --prefix="$D/" -a
+pushd "$D"
+
+nix-shell --arg lint true --run './test.sh lint'
+
+popd
diff --git a/hellowebpy.py b/hellowebpy.py
new file mode 100644 (file)
index 0000000..113911c
--- /dev/null
@@ -0,0 +1,20 @@
+from http.server import BaseHTTPRequestHandler, HTTPServer
+
+listen_address = ('', 8080)
+
+
+class HelloServer(BaseHTTPRequestHandler):
+    def do_GET(self) -> None:
+        self.send_response(200)
+        self.send_header("Content-type", "text/html")
+        self.end_headers()
+        self.wfile.write(bytes("<html><body>Hello Web</body></html>", "utf-8"))
+
+
+def main() -> None:
+    webServer = HTTPServer(listen_address, HelloServer)
+    webServer.serve_forever()
+
+
+if __name__ == "__main__":
+    main()
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..0bce1ad
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,13 @@
+from setuptools import setup
+
+setup(
+    name='hellowebpy',
+    version='1.0.0',
+    description="A minimal 'hello world' python web server.",
+    author="Scott Worley",
+    author_email="scottworley@scottworley.com",
+    url="https://git.scottworley.com/hellowebpy",
+    py_modules=['hellowebpy'],
+    license="GPL-3.0",
+    entry_points={'console_scripts': ['hellowebpy = hellowebpy:main']},
+)
diff --git a/test.sh b/test.sh
new file mode 100755 (executable)
index 0000000..9ccbe26
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+set -e
+
+PARALLELISM=4
+
+find . -name build -prune -o -name dist -prune -o -name '*.py' -print0 |
+  xargs -0 mypy --strict --ignore-missing-imports --no-warn-unused-ignores
+
+for test in tests/*;do
+  if [ ! -d "$test" ];then
+    echo "### Running test $test" >&2
+    "$test"
+  fi
+done
+
+find . -name '*_test.py' -print0 | xargs -0 -r -n1 python3
+
+if [ "$1" = lint ];then
+
+  find . -name '*.py' -print0 | xargs -0 pylint --reports=n --persistent=n --ignore-imports=y -d fixme,invalid-name,missing-docstring,subprocess-run-check,too-few-public-methods
+
+  formatting_needs_fixing=$(
+    find . -name '*.py' -print0 |
+      xargs -P "$PARALLELISM" -0 -n1 autopep8 --diff -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+  )
+  if [ "$formatting_needs_fixing" ];then
+    echo "Formatting needs fixing:"
+    echo "$formatting_needs_fixing"
+    exit 1
+  fi
+
+fi
diff --git a/tests/says-hello.sh b/tests/says-hello.sh
new file mode 100755 (executable)
index 0000000..2c1a646
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+set -eu
+
+python3 hellowebpy.py &
+pid=$!
+
+exit_status=1
+
+for _ in $(seq 10);do
+  if curl --max-time 2 http://localhost:8080 | grep 'Hello Web';then
+    echo SUCCESS
+    exit_status=0
+    break
+  fi
+  sleep 1
+done
+
+kill "$pid"
+exit "$exit_status"