Skip to content

Commit

Permalink
Add documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaroesti committed Jan 11, 2017
1 parent 416e122 commit cb5c490
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.idea
.pypirc

build
dist
Expand Down
92 changes: 90 additions & 2 deletions 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.**
**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.
26 changes: 25 additions & 1 deletion 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
Expand All @@ -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: <https://www.dadant.co/static_file.js>; 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)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = '0.0.1'
VERSION = '0.0.2'


setup(
Expand All @@ -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,
Expand Down

0 comments on commit cb5c490

Please sign in to comment.