Added examples and form processing

This commit is contained in:
Channel Cat
2016-10-09 15:28:31 -07:00
parent 8fbc6c2c4e
commit 49c499f44d
12 changed files with 448 additions and 145 deletions

11
examples/Dockerfile Normal file
View File

@@ -0,0 +1,11 @@
FROM python:3.5
MAINTAINER Channel Cat <channelcat@gmail.com>
ADD . /code
RUN pip3 install git+https://github.com/channelcat/sanic
EXPOSE 8000
WORKDIR /code
CMD ["python", "simple_server.py"]

View File

@@ -0,0 +1,6 @@
version: '2'
services:
sanic:
build: .
ports:
- "8000:8000"

10
examples/simple_server.py Normal file
View File

@@ -0,0 +1,10 @@
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({ "test": True })
app.run(host="0.0.0.0", port=8000)

View File

@@ -0,0 +1,59 @@
from sanic import Sanic
from sanic.log import log
from sanic.response import json, text
from sanic.exceptions import ServerError
app = Sanic(__name__)
@app.route("/")
async def test_async(request):
return json({ "test": True })
@app.route("/sync", methods=['GET', 'POST'])
def test_sync(request):
return json({ "test": True })
@app.route("/dynamic/<name>/<id:int>")
def test_params(request, name, id):
return text("yeehaww {} {}".format(name, id))
@app.route("/exception")
def exception(request):
raise ServerError("It's dead jim")
# ----------------------------------------------- #
# Exceptions
# ----------------------------------------------- #
@app.exception(ServerError)
async def test(request, exception):
return json({ "exception": "{}".format(exception), "status": exception.status_code }, status=exception.status_code)
# ----------------------------------------------- #
# Read from request
# ----------------------------------------------- #
@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, "penos": request.form.get('penos') })
@app.route("/query_string")
def query_string(request):
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
# ----------------------------------------------- #
# Run Server
# ----------------------------------------------- #
def before_start(loop):
log.info("OH OH OH OH OHHHHHHHH")
def before_stop(loop):
log.info("TRIED EVERYTHING")
app.run(host="0.0.0.0", port=8000, debug=True, before_start=before_start, before_stop=before_stop)