generated from hackmtlca/flask-sqlite-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (38 loc) · 1.4 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
from flask import Flask, g, request
from flask_sqlalchemy import SQLAlchemy
import os
import shutil
import glob
from src.secrets import JWT_SECRET
import jwt
# Checks if the tmp folder is already init. If not, create folder and copy content over.
if not os.path.exists('./tmp'):
os.mkdir('./tmp')
for f in glob.glob(os.path.join('./data', '*.*')):
shutil.copy(f, './tmp')
# Creates the flask app.
app = Flask(__name__)
# Removes the logging, less overhead.
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Generates path to the database file (has to be absolute).
path = ('sqlite:///' + os.path.join(os.path.abspath(os.getcwd()), 'tmp/database.db')).replace('\\', '/')
app.config['SQLALCHEMY_DATABASE_URI'] = path
# Connects database to app.
db = SQLAlchemy(app)
# Method that injects into the `g` variable the logged in state and the user. This make this information available for each view.
@app.before_request
def inject_user_state():
try:
g.user = jwt.decode(request.cookies.get('session'), JWT_SECRET)
g.logged_in = True
except:
g.user = None
g.logged_in = False
if __name__ == '__main__':
from src.api.users import users
from src.routes import routes
# Registers the API blueprints.
app.register_blueprint(users)
app.register_blueprint(routes)
# Remove debug mode once in production.
app.run(debug=True, host='0.0.0.0', port=80)