Skip to content

Commit

Permalink
[update]
Browse files Browse the repository at this point in the history
  • Loading branch information
boke0 committed Mar 7, 2021
1 parent f0daac1 commit d9dd92b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.mysql
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
7 changes: 0 additions & 7 deletions docker/bin/entrypoint-mysql.sh

This file was deleted.

2 changes: 2 additions & 0 deletions docker/conf/uwsgi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ vacuum=true
gevent=100
pidfile=/tmp/uwsgi.pid
module=project:application
http-websockets=true
logto=/project/uwsgi.log
17 changes: 16 additions & 1 deletion mitama/app/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import json
import wsgiref.util as wsgiutil
import uwsgi
from urllib.parse import parse_qs


Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions mitama/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion tests/test_apps/hello/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 8 additions & 0 deletions tests/test_apps/hello/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
{% for user in users %}
{{ lists.userItem(user, small=True) }}
{% endfor %}
<button onclick="send()">hhh</button>
<script>
const ws = new WebSocket("{{ fullurl(request, "/sock", scheme="ws") }}")
ws.onmessage = (e) => console.log(e.data)
function send() {
ws.send('hhh')
}
</script>
{% endblock %}
8 changes: 8 additions & 0 deletions tests/test_apps/uwsgi.ini
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d9dd92b

Please sign in to comment.