2016-10-09 23:28:31 +01:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.log import log
|
|
|
|
from sanic.response import json, text
|
|
|
|
from sanic.exceptions import ServerError
|
|
|
|
|
|
|
|
app = Sanic(__name__)
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
@app.route("/")
|
|
|
|
async def test_async(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"test": True})
|
|
|
|
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
@app.route("/sync", methods=['GET', 'POST'])
|
|
|
|
def test_sync(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"test": True})
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
@app.route("/dynamic/<name>/<id:int>")
|
|
|
|
def test_params(request, name, id):
|
|
|
|
return text("yeehaww {} {}".format(name, id))
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
@app.route("/exception")
|
|
|
|
def exception(request):
|
|
|
|
raise ServerError("It's dead jim")
|
|
|
|
|
2016-10-15 04:22:05 +01:00
|
|
|
@app.route("/await")
|
|
|
|
async def test_await(request):
|
|
|
|
import asyncio
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
return text("I'm feeling sleepy")
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Exceptions
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
|
|
|
@app.exception(ServerError)
|
|
|
|
async def test(request, exception):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"exception": "{}".format(exception), "status": exception.status_code}, status=exception.status_code)
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Read from request
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
|
|
|
@app.route("/json")
|
|
|
|
def post_json(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"received": True, "message": request.json})
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
@app.route("/form")
|
|
|
|
def post_json(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"received": True, "form_data": request.form, "test": request.form.get('test')})
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
@app.route("/query_string")
|
|
|
|
def query_string(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
return json({"parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string})
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Run Server
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2017-01-04 06:23:59 +00:00
|
|
|
def after_start(app, loop):
|
2016-10-09 23:28:31 +01:00
|
|
|
log.info("OH OH OH OH OHHHHHHHH")
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
|
2017-01-04 06:23:59 +00:00
|
|
|
def before_stop(app, loop):
|
2016-10-09 23:28:31 +01:00
|
|
|
log.info("TRIED EVERYTHING")
|
|
|
|
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
app.run(host="0.0.0.0", port=8000, debug=True, after_start=after_start, before_stop=before_stop)
|