-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
chat.py
50 lines (39 loc) 路 1.63 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""A minimalist chat system, using the Flask microframework to handle cookie authentication
Install:
pip install -r requirements.txt
Run (prod):
gunicorn chat:app
Run (dev):
FLASK_APP=chat.py FLASK_DEBUG=1 flask run
Deploy on Heroku:
heroku login
heroku config:set HUB_URL=https://demo.mercure.rocks/hub
heroku config:set COOKIE_DOMAIN=.mercure.rocks
git subtree push --prefix examples/chat-python-flask heroku master
Environment variables:
JWT_KEY: the JWT key to use (must be shared with the Mercure hub)
HUB_URL: the URL of the Mercure hub (default: http://localhost:3000/hub)
TOPIC: the topic to use (default: http://example.com/chat)
TARGET: the target to use (default: chan)
COOKIE_DOMAIN: the cookie domain (default: None)
"""
from flask import Flask, make_response, request, render_template
import jwt
import os
app = Flask(__name__)
@app.route("/")
def chat():
targets = [os.environ.get('TARGET', 'chan')]
token = jwt.encode(
{'mercure': {'subscribe': targets, 'publish': targets}},
os.environ.get('JWT_KEY', '!ChangeMe!'),
algorithm='HS256'
)
hub_url = os.environ.get('HUB_URL', 'http://localhost:3000/hub')
topic = os.environ.get('TOPIC', 'http://example.com/chat')
resp = make_response(render_template('chat.html', config={
'hubURL': hub_url, 'topic': topic}))
resp.set_cookie('mercureAuthorization', token, httponly=True, path='/hub',
samesite="strict", domain=os.environ.get('COOKIE_DOMAIN', None), secure=request.is_secure) # Force secure to True for real apps
return resp