-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
170 lines (105 loc) · 4.52 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import logging
import secrets
from utils.hash import hash_user_token
from utils.counter import AtomicCounter
from services.ghost_api import get_post, get_post_payment
from config.settings import (SESSION_LIFETIME,
URL,
ADMIN_PANEL)
import os
from services.iota import Listener
from datetime import datetime, timedelta
from flask_socketio import SocketIO
from flask import (Flask,
render_template,
request,
session,
send_from_directory,
redirect)
from database.db import db
from admin import admin, auth
from database.operations import (check_slug,
get_access,
get_slug_data,
get_author_address,
set_socket_session,
access_expired,
reset_socket_sessions)
app = Flask(__name__.split('.')[0])
app.config.from_object('config.flask')
# create web socket for async communication
socketio = SocketIO(app, async_mode='threading', cors_allowed_origins="*")
# create itoa listener
iota_listener = Listener(socketio)
# expand default 31 days session lifetime if neccessary
if SESSION_LIFETIME > 744:
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=SESSION_LIFETIME)
logging.basicConfig(level=logging.INFO)
LOG = logging.getLogger("ghost-iota-pay")
connected_clients = AtomicCounter()
@app.before_request
def make_session_permanent():
# make session persistent
session.permanent = True
@app.route('/', methods=["GET"])
def welcome():
return render_template('welcome.html')
@app.route('/favicon.ico', methods=["GET"])
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico')
@app.route('/<slug>', methods=["GET"])
def proxy(slug):
# Check if slug exists and add to db
slug_check = check_slug(slug, iota_listener)
if slug_check is None:
return 'Slug not available'
if slug_check == 'free':
return redirect('%s/%s' % (URL, slug))
# Check if user already has cookie and set one
if 'iota_ghost_user_token:' not in session:
session['iota_ghost_user_token:'] = secrets.token_hex(16)
user_token_hash = hash_user_token(session['iota_ghost_user_token:'], slug)
access = get_access(user_token_hash, slug)
if access.exp_date is not None:
if access.exp_date > datetime.utcnow():
return get_post(slug, access.exp_date)
return render_template('expired.html',
exp_date = access_expired(user_token_hash).strftime('%d.%m.%y %H:%M UTC'))
slug_info = get_slug_data(slug)
iota_address = get_author_address(slug_info.author_id)
return get_post_payment(slug, render_template('pay.html',
user_token_hash = user_token_hash,
iota_address = iota_address,
price = access.slug_price,
exp_date = (datetime.utcnow() + timedelta(hours = SESSION_LIFETIME))
.strftime('%d.%m.%y %H:%M UTC')))
# socket endpoint to receive payment event
@socketio.on('await_payment')
def await_payment(data):
set_socket_session(data['user_token_hash'], request.sid)
global connected_clients
if connected_clients.increment() < 2:
socketio.start_background_task(iota_listener.start, app)
@socketio.on('disconnect')
def disconnect():
global connected_clients
if connected_clients.decrement() < 1:
LOG.info('%s connected clients. Stopping MQTT ...', connected_clients.value)
reset_socket_sessions()
socketio.start_background_task(iota_listener.stop)
# socket endpoint for manual check on payment
@socketio.on('check_payment')
def check_payment(data):
socketio.start_background_task(iota_listener.manual_payment_check, app, data['iota_address'], data['user_token_hash'])
if __name__ == '__main__':
db.init_app(app)
with app.app_context():
db.create_all()
if ADMIN_PANEL:
admin.init_app(app)
auth.init_app(app)
socketio.run(app, host='0.0.0.0')
# Stop server
LOG.info('Stopping ...')
iota_listener.stop()