-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
129 lines (90 loc) · 3.5 KB
/
__init__.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from datetime import timedelta
import requests
from dateutil import parser
from flask import Flask, render_template, request, send_from_directory, session
from authentication.check_token import verify
from config import SENTRY_DSN, SENTRY_SERVER_NAME
# set up sentry for error handling
if SENTRY_DSN != "":
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[FlaskIntegration()],
traces_sample_rate=1.0,
server_name=SENTRY_SERVER_NAME,
)
def handle_error(request, session, error_code):
auth_result = verify(request.headers, session)
if auth_result:
headers = {"Authorization": session["access_token"]}
channel_req = requests.get(
session.get("server_url") + "?action=channels", headers=headers
)
all_channels = channel_req.json()["channels"]
else:
all_channels = []
template = "404.html"
return (
render_template(
template, title="Error", error=error_code, channels=all_channels
),
500,
)
def create_app():
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(32)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///microsub.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# read config.py file
app.config.from_pyfile(os.path.join(".", "config.py"), silent=False)
# set maximum lifetime for session
app.permanent_session_lifetime = timedelta(days=120)
# blueprint for non-auth parts of app
from server.main import main as main_blueprint
app.register_blueprint(main_blueprint)
from client.client_views import client as client_blueprint
app.register_blueprint(client_blueprint)
from authentication.auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
from server.websub import websub as websub_blueprint
app.register_blueprint(websub_blueprint)
from server.server_views import server_views as server_views_blueprint
app.register_blueprint(server_views_blueprint)
# filter used to parse dates
# source: https://stackoverflow.com/questions/4830535/how-do-i-format-a-date-in-jinja2
@app.template_filter("strftime")
def _jinja2_filter_datetime(date, fmt=None):
date = parser.parse(date)
native = date.replace(tzinfo=None)
format = "%b %d, %Y"
return native.strftime(format)
@app.errorhandler(404)
def page_not_found(e):
return handle_error(request, session, 400)
@app.errorhandler(405)
def method_not_allowed(e):
return handle_error(request, session, 405)
@app.errorhandler(500)
def server_error():
handle_error(request, session, 500)
@app.route("/robots.txt")
def robots():
return send_from_directory(app.static_folder, "robots.txt")
@app.route("/favicon.ico")
def favicon():
return send_from_directory(app.static_folder, "favicon.ico")
@app.route("/emojis.json")
def emojis():
return send_from_directory("static", "emojis.json")
@app.route("/manifest.json")
def web_app_manifest():
return send_from_directory("static", "manifest.json")
@app.route("/assets/<path:path>")
def assets(path):
return send_from_directory("assets", path)
# from werkzeug.middleware.profiler import ProfilerMiddleware
# app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='./profile')
return app
create_app()