-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
50 lines (37 loc) · 1 KB
/
Copy pathinit.py
File metadata and controls
50 lines (37 loc) · 1 KB
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
from flask import Flask, jsonify, g
from db.DatabaseHelper import DatabaseHelper
from db.Repository import Repository
SQLITE = "Sqlite3"
#POSTGRESQL = "PostgreSQL"
DEFAULT_DB = SQLITE
repo = None
app = Flask(__name__)
def get_db():
if not hasattr(g, 'link_db'):
g.link_db = DatabaseHelper(DEFAULT_DB).db
return g.link_db
@app.before_request
def before_request():
try:
global repo
repo = Repository( get_db() )
except Exception as e:
print(e)
@app.route('/api/<topic>', methods=['GET'])
def one(topic):
data = repo.random(topic)
response = jsonify(data)
response.headers['Cache-Control'] = 'no-store'
return response
@app.route('/api/cheatsheet/<topic>', methods=['GET'])
def table(topic):
data = repo.cheatsheet(topic)
response = jsonify(data)
response.headers['Cache-Control'] = 'no-store'
return response
@app.teardown_appcontext
def close_db(error):
if hasattr(g, 'link_db'):
g.link_db.close()
if __name__ == '__main__':
app.run(debug=True)