first commit
Browse files- Dockerfile +7 -0
- app/__pycache__/main.cpython-37.pyc +0 -0
- app/main.py +52 -0
- requirements.txt +4 -0
- start.sh +12 -0
Dockerfile
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
WORKDIR /fastapi/app
|
3 |
+
COPY ./requirements.txt /fastapi/requirements.txt
|
4 |
+
RUN pip install --no-cache-dir --upgrade -r /fastapi/requirements.txt
|
5 |
+
COPY ./app /fastapi/app
|
6 |
+
EXPOSE 8000
|
7 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
app/__pycache__/main.cpython-37.pyc
ADDED
Binary file (1.65 kB). View file
|
|
app/main.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, WebSocket
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
|
4 |
+
app = FastAPI()
|
5 |
+
|
6 |
+
html = """
|
7 |
+
<!DOCTYPE html>
|
8 |
+
<html>
|
9 |
+
<head>
|
10 |
+
<title>Chat</title>
|
11 |
+
</head>
|
12 |
+
<body>
|
13 |
+
<h1>WebSocket Chat</h1>
|
14 |
+
<form action="" onsubmit="sendMessage(event)">
|
15 |
+
<input type="text" id="messageText" autocomplete="off"/>
|
16 |
+
<button>Send</button>
|
17 |
+
</form>
|
18 |
+
<ul id='messages'>
|
19 |
+
</ul>
|
20 |
+
<script>
|
21 |
+
var ws = new WebSocket("ws://localhost:8000/ws");
|
22 |
+
ws.onmessage = function(event) {
|
23 |
+
var messages = document.getElementById('messages')
|
24 |
+
var message = document.createElement('li')
|
25 |
+
var content = document.createTextNode(event.data)
|
26 |
+
message.appendChild(content)
|
27 |
+
messages.appendChild(message)
|
28 |
+
};
|
29 |
+
function sendMessage(event) {
|
30 |
+
var input = document.getElementById("messageText")
|
31 |
+
ws.send(input.value)
|
32 |
+
input.value = ''
|
33 |
+
event.preventDefault()
|
34 |
+
}
|
35 |
+
</script>
|
36 |
+
</body>
|
37 |
+
</html>
|
38 |
+
"""
|
39 |
+
|
40 |
+
|
41 |
+
@app.get("/")
|
42 |
+
async def get():
|
43 |
+
return HTMLResponse(html)
|
44 |
+
|
45 |
+
|
46 |
+
@app.websocket("/ws")
|
47 |
+
async def websocket_endpoint(websocket: WebSocket):
|
48 |
+
await websocket.accept()
|
49 |
+
while True:
|
50 |
+
data = await websocket.receive_text()
|
51 |
+
await websocket.send_text(f"Message text was: {data}")
|
52 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
pydantic
|
3 |
+
uvicorn
|
4 |
+
websockets
|
start.sh
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env bash
|
2 |
+
set -e
|
3 |
+
|
4 |
+
cd /fastapi/app
|
5 |
+
|
6 |
+
if [ "$DEBUG" = true ] ; then
|
7 |
+
echo 'Debugging - ON'
|
8 |
+
uvicorn main:app --host 0.0.0.0 --port 80 --reload
|
9 |
+
else
|
10 |
+
echo 'Debugging - OFF'
|
11 |
+
uvicorn main:app --host 0.0.0.0 --port 80
|
12 |
+
fi
|