Compare commits

..

8 Commits

Author SHA1 Message Date
kennethreitz cee5ca8873 v0.1.1 2018-10-17 06:01:41 -07:00
kennethreitz d961d4ab43 default routes 2018-10-17 06:01:27 -07:00
kennethreitz 5205150a89 default route 2018-10-17 05:53:23 -07:00
kennethreitz 48e58cde5d docker 2018-10-17 05:19:22 -07:00
kennethreitz 033e91f8df name 2018-10-17 05:15:25 -07:00
kennethreitz aab3705897 myapi 2018-10-17 05:14:19 -07:00
kennethreitz d02efa81f2 deployment 2018-10-17 05:12:11 -07:00
kennethreitz 95a8240da7 single-page webapps 2018-10-17 04:58:11 -07:00
6 changed files with 81 additions and 7 deletions
+3
View File
@@ -1,3 +1,6 @@
# v0.1.1
- Default routes.
# v0.1.0
- Prototype of static application support.
+57
View File
@@ -0,0 +1,57 @@
Deploying Responder
===================
You can deploy Responder anywhere you can deploy a basic Python application.
Docker Deployment
-----------------
Assuming existing ``api.py`` and ``Pipfile.lock`` containing ``responder``.
``Dockerfile``::
from kenethreitz/pipenv
COPY . /app
CMD python3 api.py
That's it!
Heroku Deployment
-----------------
The basics::
$ mkdir my-api
$ cd my-api
$ git init
$ heroku create
...
Install Responder::
$ pipenv install responder
...
Write out a ``api.py``::
import responder
api = responder.API()
@api.route("/")
async def hello(req, resp):
resp.text = "hello, world!"
if __name__ == "__main__":
api.run()
Write out a ``Procfile``::
web: python api.py
That's it! Next, we commit and push to Heroku::
$ git add -A
$ git commit -m 'initial commit'
$ git push heroku master
+1
View File
@@ -104,6 +104,7 @@ User Guides
quickstart
tour
deployment
api
+9
View File
@@ -140,6 +140,15 @@ Responder gives you the ability to mount another ASGI / WSGI app at a subroute::
That's it!
Single-Page Web Apps
--------------------
If you have a single-page webapp, you can tell Responder to serve up your ``static/index.html`` at a route, like so::
api.add_route("/", static=True)
This will make ``index.html`` the default response to all undefined routes.
HSTS (Redirect to HTTPS)
------------------------
+1 -1
View File
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.1.1"
+10 -6
View File
@@ -75,7 +75,7 @@ class API:
if self.openapi_version:
self.add_route(openapi_route, self.schema_response)
self.static_enabled = False
self.default_endpoint = None
@property
def _apispec(self):
@@ -221,20 +221,24 @@ class API:
return resp
def add_route(self, route, endpoint=None, *, static=True, check_existing=True):
def add_route(self, route, endpoint=None, *, default=False, static=False, check_existing=True):
"""Add a route to the API.
:param route: A string representation of the route.
:param endpoint: The endpoint for the route -- can be a callable, a class, or graphene schema (GraphQL).
:param static: if True, and no endpoint was passed, render "static/index.html", and forward all undefined routes to this view.
:param default: If ``True``, all unknown requests will route to this view.
:param static: If ``True``, and no endpoint was passed, render "static/index.html", and it will become a default route.
:param check_existing: If ``True``, an AssertionError will be raised, if the route is already defined.
"""
if check_existing:
assert route not in self.routes
if not endpoint and static:
self.static_enabled = True
endpoint = self.static_response
default = True
if default:
self.default_endpoint = endpoint
self.routes[route] = Route(route, endpoint)
# TODO: A better datastructer or sort it once the app is loaded
self.routes = dict(
@@ -242,8 +246,8 @@ class API:
)
def default_response(self, req, resp):
if self.static_enabled:
self.static_response(req, resp)
if self.default_endpoint:
self.default_endpoint(req, resp)
else:
resp.status_code = status_codes.HTTP_404
resp.text = "Not found."