Basic wsgi app working inside twisted
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 22 Oct 2019 17:27:21 +0000 (13:27 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 22 Oct 2019 17:27:21 +0000 (13:27 -0400)
.gitignore [new file with mode: 0644]
main.py [new file with mode: 0644]
phial.py [new file with mode: 0644]
requirements.txt [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..896cdee
--- /dev/null
@@ -0,0 +1,2 @@
+.env/
+__pycache__/
diff --git a/main.py b/main.py
new file mode 100644 (file)
index 0000000..2fa7191
--- /dev/null
+++ b/main.py
@@ -0,0 +1,21 @@
+import phial
+
+def handler(request):
+    return phial.Response(
+        status='200 OK',
+        headers=[('Content-Type', 'text/plain')],
+        content=[b'Hello, world\n'],
+    )
+
+app = phial.App(handler)
+
+if __name__ == '__main__':
+    from twisted.internet import reactor
+    from twisted.web.server import Site
+    from twisted.web.wsgi import WSGIResource
+
+    reactor_args = {}
+    resource = WSGIResource(reactor, reactor.getThreadPool(), app)
+    site = Site(resource)
+    reactor.listenTCP(5000, site)
+    reactor.run(**reactor_args)
diff --git a/phial.py b/phial.py
new file mode 100644 (file)
index 0000000..3447057
--- /dev/null
+++ b/phial.py
@@ -0,0 +1,25 @@
+import collections
+
+Request = collections.namedtuple(
+    'Request',
+    (
+        'environ',
+    )
+)
+
+Response = collections.namedtuple(
+    'Response',
+    (
+        'status',
+        'headers',
+        'content',
+    ),
+)
+
+def App(handler):
+    def app(environ, start_fn):
+        response = handler(Request(environ))
+
+        start_fn(response.status, response.headers)
+        return response.content
+    return app
diff --git a/requirements.txt b/requirements.txt
new file mode 100644 (file)
index 0000000..8926f62
--- /dev/null
@@ -0,0 +1 @@
+Twisted==19.7.0