forked from encode/starlette
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Version 0.13 * Fix misnamed requirement * Updating routing docs * Update routing docs * Update docs * Simpler Middleware signature * Update middleware docs * Update exception docs * Allow 'None' in middleware lists, for easy disabling of middleware. * Update README
- Loading branch information
1 parent
7822039
commit 7f8cd04
Showing
26 changed files
with
669 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,77 +5,45 @@ its other functionality. | |
```python | ||
from starlette.applications import Starlette | ||
from starlette.responses import PlainTextResponse | ||
from starlette.routing import Route, Mount, WebSocketRoute | ||
from starlette.staticfiles import StaticFiles | ||
|
||
|
||
app = Starlette() | ||
app.debug = True | ||
app.mount('/static', StaticFiles(directory="static")) | ||
|
||
|
||
@app.route('/') | ||
def homepage(request): | ||
return PlainTextResponse('Hello, world!') | ||
|
||
@app.route('/user/me') | ||
def user_me(request): | ||
username = "John Doe" | ||
return PlainTextResponse('Hello, %s!' % username) | ||
|
||
@app.route('/user/{username}') | ||
def user(request): | ||
username = request.path_params['username'] | ||
return PlainTextResponse('Hello, %s!' % username) | ||
|
||
|
||
@app.websocket_route('/ws') | ||
async def websocket_endpoint(websocket): | ||
await websocket.accept() | ||
await websocket.send_text('Hello, websocket!') | ||
await websocket.close() | ||
|
||
|
||
@app.on_event('startup') | ||
def startup(): | ||
print('Ready to go') | ||
``` | ||
|
||
### Instantiating the application | ||
|
||
* `Starlette(debug=False)` - Create a new Starlette application. | ||
|
||
### Adding routes to the application | ||
|
||
You can use any of the following to add handled routes to the application: | ||
|
||
* `app.add_route(path, func, methods=["GET"])` - Add an HTTP route. The function may be either a coroutine or a regular function, with a signature like `func(request, **kwargs) -> response`. | ||
* `app.add_websocket_route(path, func)` - Add a websocket session route. The function must be a coroutine, with a signature like `func(session, **kwargs)`. | ||
* `@app.route(path)` - Add an HTTP route, decorator style. | ||
* `@app.websocket_route(path)` - Add a WebSocket route, decorator style. | ||
routes = [ | ||
Route('/', homepage), | ||
Route('/user/me', user_me), | ||
Route('/user/{username}', user), | ||
WebSocketRoute('/ws', websocket_endpoint), | ||
Mount('/static', StaticFiles(directory="static")), | ||
] | ||
|
||
### Adding event handlers to the application | ||
|
||
There are two ways to add event handlers: | ||
|
||
* `@app.on_event(event_type)` - Add an event, decorator style | ||
* `app.add_event_handler(event_type, func)` - Add an event through a function call. | ||
|
||
`event_type` must be specified as either `'startup'` or `'shutdown'`. | ||
|
||
### Submounting other applications | ||
|
||
Submounting applications is a powerful way to include reusable ASGI applications. | ||
|
||
* `app.mount(prefix, app)` - Include an ASGI app, mounted under the given path prefix | ||
|
||
### Customizing exception handling | ||
app = Starlette(debug=True, routes=routes, on_startup=[startup]) | ||
``` | ||
|
||
You can use either of the following to catch and handle particular types of | ||
exceptions that occur within the application: | ||
### Instantiating the application | ||
|
||
* `app.add_exception_handler(exc_class_or_status_code, handler)` - Add an error handler. The handler function may be either a coroutine or a regular function, with a signature like `func(request, exc) -> response`. | ||
* `@app.exception_handler(exc_class_or_status_code)` - Add an error handler, decorator style. | ||
* `app.debug` - Enable or disable error tracebacks in the browser. | ||
::: starlette.applications.Starlette | ||
:docstring: | ||
|
||
### Storing state on the app instance | ||
|
||
|
@@ -88,6 +56,6 @@ For example: | |
app.state.ADMIN_EMAIL = '[email protected]' | ||
``` | ||
|
||
### Acessing the app instance | ||
### Accessing the app instance | ||
|
||
Where a `request` is available (i.e. endpoints and middleware), the app is available on `request.app`. For other situations it can be imported from wherever it's instantiated. | ||
Where a `request` is available (i.e. endpoints and middleware), the app is available on `request.app`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.