-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.py
executable file
·72 lines (64 loc) · 2.19 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
#!/usr/bin/env python3
import os
from urllib.parse import urlparse
from flask import Flask, render_template, redirect, url_for, request, g
app = Flask(__name__)
class DatabaseWrapper(object):
"""We cried, so you don't have to."""
def __init__(self, db):
self.db = db
def close(self): self.db.close()
def _fix(self, x): return x
def select(self, query, args=[]):
cur = self.db.cursor()
cur.execute(self._fix(query), args)
out = cur.fetchall()
cur.close()
return out
def execute(self, query, args=[]):
cur = self.db.cursor()
cur.execute(self._fix(query), args)
self.db.commit()
cur.close()
if 'DATABASE_URL' in os.environ:
import psycopg2
import urllib.parse
urllib.parse.uses_netloc.append("postgres")
db_url = urlparse(os.environ["DATABASE_URL"])
DatabaseWrapper._fix = lambda self, x: x.replace('?', '%s')
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = psycopg2.connect(
database=db_url.path[1:],
user=db_url.username,
password=db_url.password,
host=db_url.hostname,
port=db_url.port
)
return DatabaseWrapper(db)
else:
import sqlite3
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect('database.sqlite3')
return DatabaseWrapper(db)
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
######################################################################
###### The stuff above is a library to make things work easily. ######
######################################################################
from datetime import datetime
##app.route('/blarg')
@app.route('/')
##def hello():
def index():
#return str(datetime.now())
return render_template('index.html')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host=os.environ.get('BIND_TO', '127.0.0.1'), port=port, debug=bool(int(os.environ.get('FLASK_DEBUG', 1))))