diff --git a/Dockerfile.mysql b/Dockerfile.mysql index a9193c8..cfdd239 100644 --- a/Dockerfile.mysql +++ b/Dockerfile.mysql @@ -15,7 +15,7 @@ RUN mkdir /project RUN chmod 755 /log ADD /docker/conf/uwsgi.ini /conf/uwsgi.ini ADD /docker/conf/nginx.conf /etc/nginx/nginx.conf -ADD /docker/bin/entrypoint-mysql.sh /bin/entrypoint.sh +ADD /docker/bin/entrypoint.sh /bin/entrypoint.sh WORKDIR /project CMD ["/bin/entrypoint.sh"] diff --git a/docker/bin/entrypoint-mysql.sh b/docker/bin/entrypoint-mysql.sh deleted file mode 100755 index fffec7e..0000000 --- a/docker/bin/entrypoint-mysql.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -poetry install - -nginx -postfix start -uwsgi --ini /conf/uwsgi.ini diff --git a/docker/conf/uwsgi.ini b/docker/conf/uwsgi.ini index 23a96f6..f93c0b4 100644 --- a/docker/conf/uwsgi.ini +++ b/docker/conf/uwsgi.ini @@ -6,3 +6,5 @@ vacuum=true gevent=100 pidfile=/tmp/uwsgi.pid module=project:application +http-websockets=true +logto=/project/uwsgi.log diff --git a/mitama/app/http/request.py b/mitama/app/http/request.py index 1ca6ada..7316043 100644 --- a/mitama/app/http/request.py +++ b/mitama/app/http/request.py @@ -4,6 +4,7 @@ import io import json import wsgiref.util as wsgiutil +import uwsgi from urllib.parse import parse_qs @@ -24,6 +25,14 @@ def get(self, key): return None +class uWSGIWebSocket: + def __init__(self, env): + uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', '')) + def receive(self): + return uwsgi.websocket_recv() + def send(self, message): + uwsgi.websocket_send(message) + class _RequestPayload: def __init__(self, field_storage): self._field_storage = field_storage @@ -162,7 +171,13 @@ def websocket(self): if hasattr(self, "_websocket"): return self._websocket else: - self._websocket = self.environ.get("wsgi.websocket") + if "wsgi.websocket" in self.environ and self.environ.get("wsgi.websocket") is not None: + self._websocket = self.environ["wsgi.websocket"] + else: + try: + self._websocket = uWSGIWebSocket(self.environ) + except Exception as err: + print(err) return self._websocket def post(self): diff --git a/mitama/project/project.py b/mitama/project/project.py index 7b00685..ebc948a 100644 --- a/mitama/project/project.py +++ b/mitama/project/project.py @@ -6,6 +6,7 @@ import inspect import smtplib import argparse +import uwsgi from traceback import print_exc from pathlib import Path, PosixPath from email.mime.text import MIMEText diff --git a/poetry.lock b/poetry.lock index cab99ac..9f9f632 100644 --- a/poetry.lock +++ b/poetry.lock @@ -596,6 +596,14 @@ brotli = ["brotlipy (>=0.6.0)"] secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] +[[package]] +name = "uwsgi" +version = "2.0.19.1" +description = "The uWSGI server" +category = "main" +optional = false +python-versions = "*" + [[package]] name = "watchdog" version = "2.0.2" @@ -663,7 +671,7 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "1.1" python-versions = "^3.6" -content-hash = "1f646c3071a9f1827693a5e56e3992642f0d2d0ee33c46653e16bf35b6d12711" +content-hash = "f5a9478b629a412b4650b8b4bfa2c6f01a9e2fb5a0f70c6c61965681ba45abba" [metadata.files] appdirs = [ @@ -1194,6 +1202,9 @@ urllib3 = [ {file = "urllib3-1.26.3-py2.py3-none-any.whl", hash = "sha256:1b465e494e3e0d8939b50680403e3aedaa2bc434b7d5af64dfd3c958d7f5ae80"}, {file = "urllib3-1.26.3.tar.gz", hash = "sha256:de3eedaad74a2683334e282005cd8d7f22f4d55fa690a2a1020a416cb0a47e73"}, ] +uwsgi = [ + {file = "uWSGI-2.0.19.1.tar.gz", hash = "sha256:faa85e053c0b1be4d5585b0858d3a511d2cd10201802e8676060fd0a109e5869"}, +] watchdog = [ {file = "watchdog-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:1f518a6940cde8720b8826a705c164e6b9bd6cf8c00f14269ffac51e017e06ec"}, {file = "watchdog-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:74528772516228f6a015a647027057939ff0b695a0b864cb3037e8e1aabc7ca0"}, diff --git a/pyproject.toml b/pyproject.toml index 7d2fd15..2dba8c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,7 @@ pycryptodome = "^3.10.1" pywebpush = "^1.11.0" gevent = "^21.1.2" gevent-websocket = "^0.10.1" +uWSGI = "^2.0.19" [tool.poetry.dev-dependencies] unittest = "^0.0" diff --git a/tests/test_apps/hello/controller.py b/tests/test_apps/hello/controller.py index c5d5d9a..da68047 100644 --- a/tests/test_apps/hello/controller.py +++ b/tests/test_apps/hello/controller.py @@ -10,4 +10,14 @@ def handle(self, request): return Response.render(template, { "users": User.list() }) - + def sock(self, request): + ws = request.websocket + while True: + msg = ws.receive() + print(msg) + try: + ws.send(msg) + except: + ws.close() + break + return Response() diff --git a/tests/test_apps/hello/templates/welcome.html b/tests/test_apps/hello/templates/welcome.html index b0010e2..31a88cc 100644 --- a/tests/test_apps/hello/templates/welcome.html +++ b/tests/test_apps/hello/templates/welcome.html @@ -6,4 +6,12 @@ {% for user in users %} {{ lists.userItem(user, small=True) }} {% endfor %} + + {% endblock %} diff --git a/tests/test_apps/uwsgi.ini b/tests/test_apps/uwsgi.ini index e69de29..ac58844 100644 --- a/tests/test_apps/uwsgi.ini +++ b/tests/test_apps/uwsgi.ini @@ -0,0 +1,8 @@ +[uwsgi] +http=127.0.0.1:8080 +master=true +vacuum=true +gevent=100 +pidfile=/tmp/uwsgi.pid +module=project:application +http-websockets=true