mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 23:00:17 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dd0ddab610 | |||
| d23ac10f90 | |||
| ec18290b8a | |||
| 2c4cd39dc9 | |||
| 830bad0b85 | |||
| f14ef6fa15 | |||
| 7400b1c83d | |||
| e7caf39fba | |||
| 09fd0fb0ca |
@@ -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.
|
||||
|
||||
|
||||
@@ -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 @@
|
||||
__version__ = "0.0.9"
|
||||
__version__ = "0.1.0"
|
||||
|
||||
+26
-7
@@ -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
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
lorem
|
||||
Reference in New Issue
Block a user