2023-08-22 02:24:11 -07:00
|
|
|
# This file is part of pyfedi, which is licensed under the GNU General Public License (GPL) version 3.0.
|
|
|
|
# You should have received a copy of the GPL along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2023-10-02 02:16:44 -07:00
|
|
|
from flask_babel import get_locale
|
2023-08-22 02:24:11 -07:00
|
|
|
|
2023-07-27 21:22:12 -07:00
|
|
|
from app import create_app, db, cli
|
2023-08-29 03:01:06 -07:00
|
|
|
import os, click
|
2023-10-02 02:16:44 -07:00
|
|
|
from flask import session, g
|
|
|
|
from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE
|
2023-10-10 02:25:37 -07:00
|
|
|
from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits
|
2023-07-27 21:22:12 -07:00
|
|
|
|
|
|
|
app = create_app()
|
|
|
|
cli.register(app)
|
|
|
|
|
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def app_context_processor(): # NB there needs to be an identical function in cb.wsgi to make this work in production
|
|
|
|
def getmtime(filename):
|
|
|
|
return os.path.getmtime('app/static/' + filename)
|
2023-10-02 02:16:44 -07:00
|
|
|
return dict(getmtime=getmtime, post_type_link=POST_TYPE_LINK, post_type_image=POST_TYPE_IMAGE, post_type_article=POST_TYPE_ARTICLE)
|
2023-07-27 21:22:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
@app.shell_context_processor
|
|
|
|
def make_shell_context():
|
2023-09-17 02:19:51 -07:00
|
|
|
return {'db': db, 'app': app}
|
2023-08-29 03:01:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
app.jinja_env.globals['getmtime'] = getmtime
|
2023-10-02 02:16:44 -07:00
|
|
|
app.jinja_env.globals['len'] = len
|
2023-10-10 02:25:37 -07:00
|
|
|
app.jinja_env.globals['digits'] = digits
|
2023-10-02 02:16:44 -07:00
|
|
|
app.jinja_env.globals['str'] = str
|
|
|
|
app.jinja_env.filters['shorten'] = shorten_string
|
|
|
|
app.jinja_env.filters['shorten_url'] = shorten_url
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
|
|
|
session['nonce'] = gibberish()
|
|
|
|
g.locale = str(get_locale())
|
|
|
|
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
|
|
|
response.headers['Content-Security-Policy'] = f"script-src 'self' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net 'nonce-{session['nonce']}'"
|
|
|
|
response.headers['Strict-Transport-Security'] = 'max-age=63072000; includeSubDomains; preload'
|
|
|
|
response.headers['X-Content-Type-Options'] = 'nosniff'
|
|
|
|
response.headers['X-Frame-Options'] = 'DENY'
|
|
|
|
return response
|