summaryrefslogtreecommitdiffstats
path: root/bitvend.py
blob: 0eaf962a2d5ec23e28a3b877e06fc5d2411f8e50 (plain)
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
import logging
import threading
import urllib.parse
import flask
import qrcode
import qrcode.image.svg
import six

from bitvend.mdb import BitvendCashlessMDBDevice
from bitvend.utils import to_local_currency, from_local_currency, format_btc, \
    sat_to_btc
from bitvend.processor import PaymentProcessor
from bitvend.models import db, Transaction

logging.basicConfig(level=logging.INFO)

from prometheus_client import start_http_server
start_http_server(8000)

app = flask.Flask(__name__)
app.config.from_object('bitvend.default_settings')

db.init_app(app)

dev = BitvendCashlessMDBDevice()
dev.init_app(app)

proc = PaymentProcessor(dev)
proc.init_app(app)

@app.route('/')
def index():
    return flask.render_template(
        'index.html', items=app.config['ITEMS'])

@app.route('/log')
def log():
    return flask.render_template(
        'log.html', transactions=Transaction.query.all())

@app.route('/reclaim/<tx_hash>')
def reclaim(tx_hash):
    tx = Transaction.query.filter_by(tx_hash=tx_hash).first()

    if tx and tx.product_id is None:
        dev.begin_session(tx.value, tx_hash)
        dev.begin_session(tx.value, tx_hash)
        dev.begin_session(tx.value, tx_hash)
        return flask.redirect('/log')

    flask.abort(404)

@app.route('/qrcode/<path:data>')
def qrcode_gen(data):
    bio = six.BytesIO()
    qr = qrcode.QRCode(border=0, box_size=50)
    qr.add_data(data)
    img = qr.make_image(image_factory=qrcode.image.svg.SvgPathFillImage)
    img.save(bio)
    return bio.getvalue(), 200, {
        'Content-Type': 'image/svg+xml',
        'Cache-Control': 'public,max-age=3600',
        }

@app.context_processor
def ctx_utils():
    return {
        'from_local_currency': from_local_currency,
        'to_local_currency': to_local_currency,
        'format_btc': format_btc,
        'sat_to_btc': sat_to_btc,
        'qrcode': lambda data: flask.url_for('qrcode_gen', data=data),
    }

if __name__ == "__main__":
    with app.app_context():
        db.create_all()

    threading.Thread(target=app.run, kwargs={
        'host': '0.0.0.0',
        'processes': 8,
        }, daemon=True).start()
    proc.start()
    dev.run()