Cache the default rendered home page

A bit hacky, since CLForm can't seem to initialize properly without
being part of a request. To avoid a reference-before-assignment error,
the INDEX variable is first initialized as a global in __init__.py. But
it is definitely faster. Repeated renders take around 2ms each. Reading
the cached value takes about 0.002ms.

Also split index() into index_get() and index_post(), since GET calls
always use the cached version, and POST calls never do.
This commit is contained in:
Sage Vaillancourt 2021-07-31 23:14:59 -04:00
parent e52a0e9d28
commit 7398773714
2 changed files with 15 additions and 3 deletions

View File

@ -10,6 +10,8 @@ from flask import (
import writing import writing
INDEX = None
def optimize_css(): def optimize_css():
import re import re
root = os.path.dirname(os.getcwd()) root = os.path.dirname(os.getcwd())

View File

@ -5,6 +5,7 @@ from wtforms import Form, BooleanField, StringField, TextAreaField, validators
import urllib.parse import urllib.parse
from latty import CLData from latty import CLData
import flaskr
writing_blueprint = Blueprint('writing', __name__,) writing_blueprint = Blueprint('writing', __name__,)
@ -47,10 +48,19 @@ class CLForm(Form):
default=body_string default=body_string
) )
@writing_blueprint.route('/', methods=('GET', 'POST')) @writing_blueprint.route('/', methods=['GET'])
def index(): def index_get():
if flaskr.INDEX == None:
flaskr.INDEX = render_template(
'writing.html',
form=CLForm()
)
return flaskr.INDEX
@writing_blueprint.route('/', methods=['POST'])
def index_post():
form = CLForm(request.form) form = CLForm(request.form)
if request.method == 'POST' and form.validate(): if form.validate():
data = CLData( data = CLData(
username=form.username.data, username=form.username.data,
company=form.company.data, company=form.company.data,