additions

This commit is contained in:
2017-05-04 21:21:28 -04:00
parent 7f98e958af
commit 75b5b51a93
3 changed files with 30 additions and 6 deletions
+2
View File
@@ -8,3 +8,5 @@ gunicorn = "*"
Flask = "*"
crayons = "*"
maya = "*"
Flask-Compress = "*"
Flask-Cache = "*"
+6 -3
View File
@@ -1,15 +1,18 @@
# flask-common
A Flask extension with lots of common time-savers (file-serving, favicons, etc).
## Nicities
- `X-Powered-By=Flask`
- `X-Powered-By=Flask`.
- `X-Processed-Time: 0.000133037567139`.
- Favicon support.
- `@common.cache.cached(timeout=50)`.
## Web Server: Gunicorn
- `WEB_CONCURRENCY`
- `PORT`
## File Server: WhiteNoise
## File Server: WhiteNoise
Flask-Common automatically configures [WhiteNoise](http://whitenoise.evans.io) to serve your static files.
+22 -3
View File
@@ -3,10 +3,17 @@ import multiprocessing
import crayons
import maya
from flask import request, current_app, url_for
from flask import request, current_app, url_for, redirect
from gunicorn import util
from gunicorn.app.base import Application
from whitenoise import WhiteNoise
from flask_cache import Cache
import warnings
from flask.exthook import ExtDeprecationWarning
warnings.simplefilter('ignore', ExtDeprecationWarning)
# Find the stack on which we want to store the database connection.
# Starting with Flask 0.9, the _app_ctx_stack is the correct one,
@@ -91,18 +98,28 @@ class Common(object):
"""Initializes the Flask application with Common."""
if not 'COMMON_FILESERVER_DISALBED' in app.config:
with app.test_request_context() as c:
# Configure WhiteNoise.
app.wsgi_app = WhiteNoise(app.wsgi_app, root=url_for('static', filename='')[1:])
self.cache = Cache(app, config={'CACHE_TYPE': 'simple'})
@app.before_request
def before_request_callback():
request.start_time = maya.now()
@app.after_request
def after_request_callback(response):
response.headers['X-Powered-By'] = 'Flask'
response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
if not 'COMMON_POWERED_BY_DISALBED' in current_app.config:
response.headers['X-Powered-By'] = 'Flask'
if not 'COMMON_PROCESSED_TIME_DISALBED' in current_app.config:
response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
return response
@app.route('/favicon.ico')
def favicon():
return redirect(url_for('static', filename='favicon.ico'), code=301)
def serve(self, workers=None):
"""Serves the Flask application."""
@@ -112,6 +129,8 @@ class Common(object):
else:
print crayons.yellow('Booting Gunicorn...')
# Start the web server.
server = GunicornServer(self.app, workers=workers or number_of_gunicorn_workers())
server.run()