Basic documentation

This commit is contained in:
Channel Cat 2016-10-14 04:51:08 -07:00
parent 8868735fc1
commit 5a7447e975
9 changed files with 161 additions and 10 deletions

View File

@ -17,7 +17,7 @@ All tests were run on a AWS medium instance running ubuntu, using 1 process. Ea
## Hello World ## Hello World
``` ```python
from sanic import Sanic from sanic import Sanic
from sanic.response import json from sanic.response import json
@ -49,6 +49,10 @@ app.run(host="0.0.0.0", port=8000)
* RESTful router * RESTful router
* Blueprints? * Blueprints?
## Limitations:
* No wheels for uvloop and httptools on Windows :(
* Can only run on 1 cpu per worker
## Final Thoughts: ## Final Thoughts:
▄▄▄▄▄ ▄▄▄▄▄

View File

@ -1,14 +1,10 @@
========================== # How to contribute to Sanic
How to contribute to Sanic
==========================
Thank you for your interest! Thank you for your interest!
Running tests ## Running tests
---------------------
* `python -m pip install pytest` * `python -m pip install pytest`
* `python -m pytest tests` * `python -m pytest tests`
Caution ## Warning
=======
One of the main goals of Sanic is speed. Code that lowers the performance of Sanic without significant gains in usability, security, or features may not be merged. One of the main goals of Sanic is speed. Code that lowers the performance of Sanic without significant gains in usability, security, or features may not be merged.

View File

@ -0,0 +1,28 @@
# Exceptions
Check sanic.exceptions for a list of exceptions
## Throwing an exception
```python
from sanic import Sanic
from sanic.exceptions import ServerError
@app.route('/killme')
def i_am_ready_to_die(request):
raise ServerError("Something bad happened")
```
## Handling Exceptions
Just use the @exception decorator. The decorator expects a list of exceptions to handle as arguments. You can pass SanicException to catch them all! The exception handler must expect a request and exception object as arguments.
```python
from sanic import Sanic
from sanic.response import text
from sanic.exceptions import NotFound
@app.exception(NotFound)
def ignore_404s(request, exception):
return text("Yep, I totally found the page: {}".format(request.url))
```

View File

@ -0,0 +1,23 @@
# Getting Started
Make sure you have pip and python 3.5 before starting
## Benchmarks
* Install Sanic
* `python3 -m pip install git+https://github.com/channelcat/sanic/`
* Edit main.py
* ```python3
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({ "hello": "world" })
app.run(host="0.0.0.0", port=8000, debug=True)
```
* Run `python3 main.py`
You now have a working sanic server! To build more features, continue on in the documentation

25
docs/middleware.md Normal file
View File

@ -0,0 +1,25 @@
# Middleware
Middleware can be executed before or after requests. It is executed in the order it was registered. If middleware return a reponse object, the request will stop processing and a response will be returned.
Middleware is registered via the middleware decorator, and can either be added as 'request' or 'response' middleware, based on the argument provided in the decorator. Response middleware receives both the request and the response as arguments.
## Examples
```python
app = Sanic('__name__')
@app.middleware
async def halt_request(request):
return text('I halted the request')
@app.middleware('response')
async def halt_response(request, response):
return text('I halted the response')
@app.route('/')
async def handler(request):
return text('I would like to speak now please')
app.run(host="0.0.0.0", port=8000)
```

View File

@ -0,0 +1,43 @@
# Request Data
## Properties
The following request variables are accessible as properties:
request.files (dictionary of File objects) - List of files that have a name, body, and type
request.json (any) - JSON body
request.args (dict) - Query String variables. Use getlist to get multiple of the same name
request.form (dict) - Posted form variables. Use getlist to get multiple of the same name
See request.py for more information
## Examples
```python
from sanic import Sanic
from sanic.response import json
@app.route("/json")
def post_json(request):
return json({ "received": True, "message": request.json })
@app.route("/form")
def post_json(request):
return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
@app.route("/files")
def post_json(request):
test_file = request.files.get('test')
file_parameters = {
'body': test_file.body,
'name': test_file.name,
'type': test_file.type,
}
return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
@app.route("/query_string")
def query_string(request):
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
```

View File

@ -0,0 +1,32 @@
# Routing
Sanic comes with a basic router that supports request parameters. To specify a parameter, surround it with carrots like so: <PARAM>. Request parameters will be passed to the request handler functions as keyword arguments. To specify a type, add a :type after the parameter name, in the carrots. If the parameter does not match the type supplied, Sanic will throw a NotFound exception, resulting in a 404 page not found error.
## Examples
```python
from sanic import Sanic
from sanic.response import json
@app.route('/tag/<tag>')
async def person_handler(request, tag):
return text('Tag - {}'.format(tag))
@app.route('/number/<integer_arg:int>')
async def person_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg))
@app.route('/number/<number_arg:number>')
async def person_handler(request, number_arg):
return text('Number - {}'.format(number))
@app.route('/person/<name:[A-Za-z]>')
async def person_handler(request, person_id):
return text('Person - {}'.format(folder_id))
@app.route('/folder/<folder_id:[A-Za-z0-9]{0,4}>')
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))
```

View File

@ -39,7 +39,7 @@ def post_json(request):
@app.route("/form") @app.route("/form")
def post_json(request): def post_json(request):
return json({ "received": True, "form_data": request.form, "penos": request.form.get('penos') }) return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
@app.route("/query_string") @app.route("/query_string")
def query_string(request): def query_string(request):

View File

@ -5,7 +5,7 @@ from setuptools import setup
setup( setup(
name='Sanic', name='Sanic',
version="0.0.2", version="0.1.0",
url='http://github.com/channelcat/sanic/', url='http://github.com/channelcat/sanic/',
license='BSD', license='BSD',
author='Channel Cat', author='Channel Cat',