starlette/docs/applications.md

60 lines
2.1 KiB
Markdown
Raw Normal View History

2018-09-05 12:45:39 +00:00
Starlette includes an application class `Starlette` that nicely ties together all of
2018-08-28 14:11:53 +00:00
its other functionality.
```python
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
2018-08-28 14:11:53 +00:00
from starlette.staticfiles import StaticFiles
app = Starlette()
app.debug = True
app.mount('/static', StaticFiles(directory="static"))
2018-08-28 14:11:53 +00:00
@app.route('/')
def homepage(request):
return PlainTextResponse('Hello, world!')
@app.route('/user/{username}')
def user(request, 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()
2018-08-28 14:11:53 +00:00
```
### Instantiating the application
* `Starlette(debug=False)` - Create a new Starlette application.
2018-08-28 14:11:53 +00:00
### 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.
### 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
You can use either of the following to catch and handle particular types of
exceptions that occur within the application:
* `app.add_exception_handler(exc_class, 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)` - Add an error handler, decorator style.
* `app.debug` - Enable or disable error tracebacks in the browser.