-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapplication.py
More file actions
102 lines (82 loc) · 3.04 KB
/
application.py
File metadata and controls
102 lines (82 loc) · 3.04 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
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
import os
from tempfile import mkdtemp
from cs50 import SQL
from flask import Flask, redirect, request
from flask_session import Session
from sqlalchemy.pool import NullPool
from werkzeug.exceptions import HTTPException, InternalServerError, default_exceptions
from utils import apology, datetimeformat, usd
# Configure CS50 Library to use PostgreSQL database
uri = os.environ.get("DATABASE_URL")
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://")
db = SQL(uri, poolclass=NullPool) if "sqlite" in uri else SQL(uri)
from routes.buy import buy_blueprint
from routes.cash import cash_blueprint
from routes.history import history_blueprint
from routes.index import index_blueprint
from routes.login import login_blueprint
from routes.password import password_blueprint
from routes.quote import quote_blueprint
from routes.register import register_blueprint
from routes.sell import sell_blueprint
from routes.symbols import symbols_blueprint
# Configure application
app = Flask(__name__)
# Register blueprints
app.register_blueprint(buy_blueprint)
app.register_blueprint(cash_blueprint)
app.register_blueprint(history_blueprint)
app.register_blueprint(index_blueprint)
app.register_blueprint(login_blueprint)
app.register_blueprint(password_blueprint)
app.register_blueprint(quote_blueprint)
app.register_blueprint(register_blueprint)
app.register_blueprint(sell_blueprint)
app.register_blueprint(symbols_blueprint)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.before_request
def before_request():
# if http is requested then redirect to https
if request.headers.get("X-Forwarded-Proto") == "http":
url = request.url.replace("http://", "https://", 1)
code = 301
return redirect(url, code=code)
@app.after_request
def after_request(response):
# Ensure responses aren't cached
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
# Configure HTTP security headers
response.headers[
"Strict-Transport-Security"
] = "max-age=31536000; includeSubDomains"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "SAMEORIGIN"
return response
# Custom filters
app.jinja_env.filters["usd"] = usd
app.jinja_env.filters["datetimeformat"] = datetimeformat
# Configure session to use filesystem (instead of signed cookies), and secure cookies
app.config.update(
SESSION_FILE_DIR=mkdtemp(),
SESSION_PERMANENT=False,
SESSION_TYPE="filesystem",
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_SAMESITE="Lax",
)
Session(app)
def errorhandler(e):
"""Handle error"""
if not isinstance(e, HTTPException):
e = InternalServerError()
return apology(e.name, e.code)
# Listen for errors
for code in default_exceptions:
app.errorhandler(code)(errorhandler)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
app.run(host="0.0.0.0", port=port)