diff --git a/.gitignore b/.gitignore index 1390525..fe0fd66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +.pypirc build dist diff --git a/README.md b/README.md index 3a9b4fb..1a2f9c1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,94 @@ # flask-http2-push [![Build Status](https://travis-ci.org/jdaroesti/flask-http2-push.svg?branch=master)](https://travis-ci.org/jdaroesti/flask-http2-push) +[![pypi](https://img.shields.io/pypi/v/flask-http2-push.svg)](https://pypi.python.org/pypi/flask-http2-push) -A Flask extension to add http2 server push to your application. +*** -**This is a work in progress. Not suitable for production yet.** \ No newline at end of file +**Bottom line**: A Flask extension to add HTTP2 server push to your application. + +*** + +This is a drop in library for doing HTTP2 server push with a Flask application. It +needs a server that supports HTTP2 push, like Google Appengine or Cloudfare. +Once added to your Flask application it will generate the necessary headers +for server push to work. + +## Getting started + +To install run: +```bash +pip install flask-http2-push +``` + +## Usage +You need to create a `push_manifest.json` file (can be named anything you want) +that contains the files you want to push (Appengine currently has a limit of 10 files). +The file format is very easy: + +```json +{ + "/path/to/file-to-push.js": { + "type": "script", + "weight": 1 + }, + "/path/to/file-to-push.css": { + "type": "style", + "weight": 1 + }, + "/path/to/file-to-push.woff2": { + "type": "font", + "weight": 1 + }, + "/path/to/file-to-push.html": { + "type": "document", + "weight": 1 + }, +} +``` + +There is a library for automatically generating the file. You can find +it here (https://www.npmjs.com/package/http2-push-manifest). + +Once you have your manifest file, you can use the library in your flask +view functions like so: + +```python +import flask +from flask_http2_push import http2push + +app = flask.Flask(__name__) + +@app.route('/') +@http2push() +def main(): + return 'hello, world!' +``` + +This will try to find a file named `push_manifest.json` at the root of the +project. If you would like to name the manifest differently or use another +location, you can specify it in the decorator: + + +```python +@app.route('/') +@http2push('static/my_manifest.json') +def main(): + return 'hello, world!' +``` + + +## Running the tests +```bash +pip install -r requirements.txt +nosetests +``` + +## Aknowledgements + +This library is inspired by the Google Appengine HTTP2 push library (https://github.com/GoogleChrome/http2push-gae). +The explainer document contained within that repo is a very good read if +you want to further explore how HTTP2 push works (https://github.com/GoogleChrome/http2push-gae/blob/master/EXPLAINER.md) + +## License + +MIT license. See LICENSE.txt for full info. \ No newline at end of file diff --git a/flask_http2_push.py b/flask_http2_push.py index 62e0904..6dcb085 100644 --- a/flask_http2_push.py +++ b/flask_http2_push.py @@ -1,6 +1,14 @@ """ File: flask_http2_push.py +Exposes a decorator `http2push` that can be used on +Flask's view functions. + + @app.route('/') + @http2push() + def main(): + return 'hello, world!' + """ import json @@ -15,7 +23,23 @@ def http2push(manifest=PUSH_MANIFEST): - """ """ + """ + Creates the Link header needed in order to use http2 server push + to send resources to the clients on first request. + This is done, primarily, so new clients can render the app + as quickly as possible. + + The spec specifies a header with the following characteristics: + + Link: ; rel=preload; as=script, ... + + The value will be taken from the instance cache or will be created by + reading the `push_manifest.json` file (slow) and storing the value in the + cache. + + :param manifest: The path to the push_manifest.json file. + :return: The response with the http2 server push headers. + """ if not manifest_cache.get(manifest): _set_manifest_cache(manifest) diff --git a/setup.py b/setup.py index 2a7bcfe..aaacd33 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -VERSION = '0.0.1' +VERSION = '0.0.2' setup( @@ -11,7 +11,7 @@ author='David Aroesti', author_email='david@aroesti.me', description="A Flask extension to add http2 server push to your application.", - download_url='https://github.com/jdaroesti/flask-http2-push/tarball/' + VERSION, + download_url='https://github.com/jdaroesti/flask-http2-push/releases/tag/v' + VERSION, long_description=__doc__, py_modules=['flask_http2_push'], zip_safe=False,