2017-02-16 21:53:31 +00:00
|
|
|
import os
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
from sanic import Sanic, response
|
2016-10-09 23:28:31 +01:00
|
|
|
from sanic.exceptions import ServerError
|
2021-11-23 21:00:25 +00:00
|
|
|
from sanic.log import logger as log
|
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
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):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.json({"test": True})
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
@app.route("/sync", methods=["GET", "POST"])
|
2016-10-09 23:28:31 +01:00
|
|
|
def test_sync(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.json({"test": True})
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
2017-08-22 09:40:42 +01:00
|
|
|
@app.route("/dynamic/<name>/<i:int>")
|
2017-06-01 19:53:05 +01:00
|
|
|
def test_params(request, name, i):
|
|
|
|
return response.text("yeehaww {} {}".format(name, i))
|
2016-10-09 23:28:31 +01:00
|
|
|
|
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")
|
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2016-10-15 04:22:05 +01:00
|
|
|
@app.route("/await")
|
|
|
|
async def test_await(request):
|
|
|
|
import asyncio
|
2021-11-23 21:00:25 +00:00
|
|
|
|
2016-10-15 04:22:05 +01:00
|
|
|
await asyncio.sleep(5)
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.text("I'm feeling sleepy")
|
2016-10-15 04:22:05 +01:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2017-02-16 21:53:31 +00:00
|
|
|
@app.route("/file")
|
|
|
|
async def test_file(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return await response.file(os.path.abspath("setup.py"))
|
2017-02-16 21:53:31 +00:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2017-05-18 09:12:26 +01:00
|
|
|
@app.route("/file_stream")
|
|
|
|
async def test_file_stream(request):
|
2021-11-23 21:00:25 +00:00
|
|
|
return await response.file_stream(
|
|
|
|
os.path.abspath("setup.py"), chunk_size=1024
|
|
|
|
)
|
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Exceptions
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
@app.exception(ServerError)
|
|
|
|
async def test(request, exception):
|
2021-11-23 21:00:25 +00:00
|
|
|
return response.json(
|
|
|
|
{"exception": str(exception), "status": exception.status_code},
|
|
|
|
status=exception.status_code,
|
|
|
|
)
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Read from request
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
@app.route("/json")
|
|
|
|
def post_json(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.json({"received": True, "message": request.json})
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
@app.route("/form")
|
2018-03-29 23:57:10 +01:00
|
|
|
def post_form_json(request):
|
2021-11-23 21:00:25 +00:00
|
|
|
return response.json(
|
|
|
|
{
|
|
|
|
"received": True,
|
|
|
|
"form_data": request.form,
|
|
|
|
"test": request.form.get("test"),
|
|
|
|
}
|
|
|
|
)
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
@app.route("/query_string")
|
|
|
|
def query_string(request):
|
2021-11-23 21:00:25 +00:00
|
|
|
return response.json(
|
|
|
|
{
|
|
|
|
"parsed": True,
|
|
|
|
"args": request.args,
|
|
|
|
"url": request.url,
|
|
|
|
"query_string": request.query_string,
|
|
|
|
}
|
|
|
|
)
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
# Run Server
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
|
|
|
|
@app.before_server_start
|
2017-03-12 14:42:34 +00:00
|
|
|
def before_start(app, loop):
|
2018-03-24 13:55:15 +00:00
|
|
|
log.info("SERVER STARTING")
|
2017-03-12 14:42:34 +00:00
|
|
|
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
@app.after_server_start
|
2017-01-04 06:23:59 +00:00
|
|
|
def after_start(app, loop):
|
2018-03-24 13:55:15 +00:00
|
|
|
log.info("OH OH OH OH OHHHHHHHH")
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
@app.before_server_stop
|
2017-01-04 06:23:59 +00:00
|
|
|
def before_stop(app, loop):
|
2018-03-24 13:55:15 +00:00
|
|
|
log.info("SERVER STOPPING")
|
2017-03-12 14:42:34 +00:00
|
|
|
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
@app.after_server_stop
|
2017-03-12 14:42:34 +00:00
|
|
|
def after_stop(app, loop):
|
2018-03-24 13:55:15 +00:00
|
|
|
log.info("TRIED EVERYTHING")
|
2016-10-09 23:28:31 +01:00
|
|
|
|
|
|
|
|
2021-11-23 21:00:25 +00:00
|
|
|
if __name__ == "__main__":
|
2017-03-12 14:42:34 +00:00
|
|
|
app.run(host="0.0.0.0", port=8000, debug=True)
|