add nginx + wss/chat

This commit is contained in:
Alexander Mahr 2024-09-29 11:29:56 +02:00
parent 9d444505ce
commit 3eb8f65872
13 changed files with 113 additions and 17 deletions

View file

@ -0,0 +1,5 @@
FROM alpine:edge
RUN apk update && apk add py3-websockets
COPY --chmod=0555 ./entrypoint.sh /entrypoint.sh
COPY --chmod=0555 ./entrypoint.py /entrypoint.py
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -6,7 +6,7 @@ import asyncio
from websockets.asyncio.client import connect from websockets.asyncio.client import connect
async def test(): async def test():
uri = "ws://server:1080" uri = "ws://websocket-server:1080"
async with connect(uri) as websocket: async with connect(uri) as websocket:
print(f"Get username via:\n name = os.environ.get('USER')") print(f"Get username via:\n name = os.environ.get('USER')")
msg = os.environ.get('USER') msg = os.environ.get('USER')

View file

@ -1,5 +0,0 @@
FROM alpine:edge
RUN apk update && apk add py3-websockets
COPY --chmod=0555 entrypoint.sh /entrypoint.sh
COPY --chmod=0555 entrypoint.py /entrypoint.py
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -1,19 +1,27 @@
services: services:
# the server # the websocket-server
server: nginx:
stop_grace_period: 1s
build: 'nginx'
ports:
- 443:443
volumes:
- ./nginx/ssl.d:/ssl.d
# the websocket-server
websocket-server:
stop_grace_period: 1s stop_grace_period: 1s
environment: environment:
PYTHONUNBUFFERED: true PYTHONUNBUFFERED: true
build: 'server' build: 'websocket-server'
expose: expose:
- 1080 - 1080
# the client # the client
client: client-test-websocket:
stop_grace_period: 1s stop_grace_period: 1s
environment: environment:
USER: ${USER} USER: ${USER}
build: 'client' build: 'client-test-websocket'
restart: on-failure restart: on-failure
depends_on: depends_on:
server: websocket-server:
condition: service_started condition: service_started

20
nginx/Dockerfile Normal file
View file

@ -0,0 +1,20 @@
FROM nginx:latest
RUN <<EOF
set -x
SSLDIR="/etc/nginx/ssl.d"
mkdir -p "$SSLDIR"
cd "$SSLDIR"
openssl req -x509 \
-newkey rsa:4096 \
-keyout "$SSLDIR"/lovespeech.key.pem \
-out "$SSLDIR"/lovespeech.crt.pem \
-sha256 \
-days 3650 \
-nodes \
-subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=lovespeech"
ls -ialh "$SSLDIR"
EOF
COPY --chmod=0444 --chown=root:root ./etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
COPY --chmod=0555 --chown=root:root ./docker-entrypoint.d/ /docker-entrypoint.d/
COPY --chmod=0555 --chown=root:root ./srv/www/lovespeech /srv/www/lovespeech/
RUN ls /docker-entrypoint.d/

View file

@ -0,0 +1,5 @@
#!/bin/bash
set -x
mountpoint /mnt/ssl.d &&
cp /mnt/ssl.d/*pem /etc/nginx/ssl.d/ || true

View file

@ -0,0 +1,19 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
root /srv/www/lovespeech;
listen 443 ssl;
server_name lovespeech;
ssl_certificate /etc/nginx/ssl.d/lovespeech.crt.pem;
ssl_certificate_key /etc/nginx/ssl.d/lovespeech.key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location /chat/ {
proxy_pass http://websocket-server:1080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

View file

@ -0,0 +1,44 @@
<!doctype html>
<html>
<script>
window.addEventListener("load",()=>{
document.getElementById("ws").value="wss://"+location.host+"/chat/";
var result = document.getElementById("result");
function showresult(message){
var pre = document.createElement("pre");
pre.innerHTML=message;
result.insertBefore(pre,result.children[0]);
}
function do_websocket(destination = "wss://"+location.host+"/chat/")
{
var ws = new WebSocket(destination);
ws.onopen = function () {
ws.send("Hello World");
setTimeout(()=>{ ws.send("message part 2");},2000);
setTimeout(()=>{ ws.close()},30000);
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
console.log("onmessage",received_msg,evt,ws);
showresult(received_msg);
};
ws.onclose = function () {
showresult("<span style='color:red;'>closing ws</span>");
};
}
document.getElementById("wsform").addEventListener("submit",(eventi)=>{
eventi.preventDefault();
do_websocket(document.getElementById("ws").value);
},false);
},false);
</script>
<h1> test websocket </h1>
<form id="wsform">
<input name="ws" id="ws" type="text" value="wss://chat/" style="width:90vw"/>
<input type="submit">
</form>
<div id="result"></div>
</html>

View file

@ -1,5 +0,0 @@
FROM alpine:edge
RUN apk update && apk add py3-websockets
COPY --chmod=0555 entrypoint.sh /entrypoint.sh
COPY --chmod=0555 entrypoint.py /entrypoint.py
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -0,0 +1,5 @@
FROM alpine:edge
RUN apk update && apk add py3-websockets
COPY --chmod=0555 ./entrypoint.sh /entrypoint.sh
COPY --chmod=0555 ./entrypoint.py /entrypoint.py
ENTRYPOINT ["/entrypoint.sh"]