Compare commits

...

9 Commits

Author SHA1 Message Date
kennethreitz dd0ddab610 single page 2018-10-17 04:52:02 -07:00
kennethreitz d23ac10f90 version 2018-10-17 04:49:00 -07:00
kennethreitz ec18290b8a changelog 2018-10-17 04:48:38 -07:00
kennethreitz 2c4cd39dc9 static application support 2018-10-17 04:48:33 -07:00
kennethreitz 830bad0b85 docs for #53 2018-10-17 04:47:39 -07:00
kennethreitz f14ef6fa15 #53 2018-10-17 04:45:12 -07:00
kennethreitz 7400b1c83d static support #53 2018-10-17 04:38:51 -07:00
kennethreitz e7caf39fba static_route 2018-10-17 04:25:09 -07:00
kennethreitz 09fd0fb0ca version 2018-10-17 04:19:38 -07:00
6 changed files with 35 additions and 8 deletions
+6
View File
@@ -1,3 +1,9 @@
# v0.1.0
- Prototype of static application support.
# v0.0.10
- Bufgix for async class-based views.
# v0.0.9
- Bugfix for async class-based views.
+1
View File
@@ -56,6 +56,7 @@ Features
- Background tasks, spawned off in a ``ThreadPoolExecutor``.
- GraphQL (with *GraphiQL*) support!
- OpenAPI schema generation.
- Single-page webapp support!
Testimonials
------------
+1 -1
View File
@@ -1 +1 @@
__version__ = "0.0.9"
__version__ = "0.1.0"
+26 -7
View File
@@ -42,6 +42,7 @@ class API:
openapi=None,
openapi_route="/schema.yml",
static_dir="static",
static_route="/static",
templates_dir="templates",
enable_hsts=False,
):
@@ -49,7 +50,7 @@ class API:
self.version = version
self.openapi_version = openapi
self.static_dir = Path(os.path.abspath(static_dir))
self.static_route = f"/{static_dir}"
self.static_route = static_route
self.templates_dir = Path(os.path.abspath(templates_dir))
self.built_in_templates_dir = Path(
os.path.abspath(os.path.dirname(__file__) + "/templates")
@@ -74,6 +75,8 @@ class API:
if self.openapi_version:
self.add_route(openapi_route, self.schema_response)
self.static_enabled = False
@property
def _apispec(self):
spec = APISpec(
@@ -198,7 +201,9 @@ class API:
# Run on_request first.
try:
getattr(view, "on_request")(req, resp)
r = getattr(view, "on_request")(req, resp)
if hasattr(r, "send"):
await r
except AttributeError:
pass
@@ -207,7 +212,7 @@ class API:
try:
r = getattr(view, f"on_{method}")(req, resp)
if hasattr(r, 'send'):
if hasattr(r, "send"):
await r
except AttributeError:
pass
@@ -216,15 +221,20 @@ class API:
return resp
def add_route(self, route, endpoint, *, check_existing=True):
def add_route(self, route, endpoint=None, *, static=True, 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, a WSGI application, or graphene schema (GraphQL).
: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 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
self.routes[route] = Route(route, endpoint)
# TODO: A better datastructer or sort it once the app is loaded
self.routes = dict(
@@ -232,8 +242,17 @@ class API:
)
def default_response(self, req, resp):
resp.status_code = status_codes.HTTP_404
resp.text = "Not found."
if self.static_enabled:
self.static_response(req, resp)
else:
resp.status_code = status_codes.HTTP_404
resp.text = "Not found."
def static_response(self, req, resp):
index = (self.static_dir / "index.html").resolve()
if os.path.exists(index):
with open(index, "r") as f:
resp.text = f.read()
def schema_response(self, req, resp):
resp.status_code = status_codes.HTTP_200
+1
View File
@@ -0,0 +1 @@
lorem
View File