-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
FROM python:3.5 | ||
MAINTAINER Channel Cat <[email protected]> | ||
FROM sanicframework/sanic:LTS | ||
|
||
ADD . /code | ||
RUN pip3 install git+https://github.com/channelcat/sanic | ||
RUN mkdir /srv | ||
COPY . /srv | ||
|
||
EXPOSE 8000 | ||
WORKDIR /srv | ||
|
||
WORKDIR /code | ||
|
||
CMD ["python", "simple_server.py"] | ||
CMD ["sanic", "simple_server.app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,75 @@ | ||
''' | ||
Based on example from https://github.com/Skyscanner/aiotask-context | ||
and `examples/{override_logging,run_async}.py`. | ||
Needs https://github.com/Skyscanner/aiotask-context/tree/52efbc21e2e1def2d52abb9a8e951f3ce5e6f690 or newer | ||
$ pip install git+https://github.com/Skyscanner/aiotask-context.git | ||
''' | ||
|
||
import asyncio | ||
import uuid | ||
import logging | ||
from signal import signal, SIGINT | ||
|
||
from sanic import Sanic | ||
from sanic import response | ||
|
||
import uvloop | ||
import aiotask_context as context | ||
|
||
from sanic import Sanic, response | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class RequestIdFilter(logging.Filter): | ||
def filter(self, record): | ||
record.request_id = context.get('X-Request-ID') | ||
try: | ||
record.request_id = context.get("X-Request-ID") | ||
except ValueError: | ||
record.request_id = "n/a" | ||
return True | ||
|
||
|
||
LOG_SETTINGS = { | ||
'version': 1, | ||
'disable_existing_loggers': False, | ||
'handlers': { | ||
'console': { | ||
'class': 'logging.StreamHandler', | ||
'level': 'DEBUG', | ||
'formatter': 'default', | ||
'filters': ['requestid'], | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"handlers": { | ||
"console": { | ||
"class": "logging.StreamHandler", | ||
"level": "DEBUG", | ||
"formatter": "default", | ||
"filters": ["requestid"], | ||
}, | ||
}, | ||
'filters': { | ||
'requestid': { | ||
'()': RequestIdFilter, | ||
"filters": { | ||
"requestid": { | ||
"()": RequestIdFilter, | ||
}, | ||
}, | ||
'formatters': { | ||
'default': { | ||
'format': '%(asctime)s %(levelname)s %(name)s:%(lineno)d %(request_id)s | %(message)s', | ||
"formatters": { | ||
"default": { | ||
"format": "%(asctime)s %(levelname)s %(name)s:%(lineno)d %(request_id)s | %(message)s", | ||
}, | ||
}, | ||
'loggers': { | ||
'': { | ||
'level': 'DEBUG', | ||
'handlers': ['console'], | ||
'propagate': True | ||
}, | ||
} | ||
"loggers": { | ||
"": {"level": "DEBUG", "handlers": ["console"], "propagate": True}, | ||
}, | ||
} | ||
|
||
|
||
app = Sanic(__name__, log_config=LOG_SETTINGS) | ||
|
||
|
||
@app.middleware('request') | ||
@app.on_request | ||
async def set_request_id(request): | ||
request_id = request.headers.get('X-Request-ID') or str(uuid.uuid4()) | ||
request_id = request.id | ||
context.set("X-Request-ID", request_id) | ||
log.info(f"Setting {request.id=}") | ||
|
||
|
||
@app.on_response | ||
async def set_request_header(request, response): | ||
response.headers["X-Request-ID"] = request.id | ||
|
||
|
||
@app.route("/") | ||
async def test(request): | ||
log.debug('X-Request-ID: %s', context.get('X-Request-ID')) | ||
log.info('Hello from test!') | ||
log.debug("X-Request-ID: %s", context.get("X-Request-ID")) | ||
log.info("Hello from test!") | ||
return response.json({"test": True}) | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.set_event_loop(uvloop.new_event_loop()) | ||
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True) | ||
loop = asyncio.get_event_loop() | ||
@app.before_server_start | ||
def setup(app, loop): | ||
loop.set_task_factory(context.task_factory) | ||
task = asyncio.ensure_future(server) | ||
try: | ||
loop.run_forever() | ||
except: | ||
loop.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(port=9999, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
from sanic import Sanic | ||
from sanic import response | ||
import logging | ||
|
||
from sanic import Sanic, text | ||
|
||
|
||
logging_format = "[%(asctime)s] %(process)d-%(levelname)s " | ||
logging_format += "%(module)s::%(funcName)s():l%(lineno)d: " | ||
logging_format += "%(message)s" | ||
|
||
logging.basicConfig( | ||
format=logging_format, | ||
level=logging.DEBUG | ||
) | ||
logging.basicConfig(format=logging_format, level=logging.DEBUG) | ||
log = logging.getLogger() | ||
|
||
# Set logger to override default basicConfig | ||
sanic = Sanic() | ||
app = Sanic("app") | ||
|
||
|
||
@sanic.route("/") | ||
@app.route("/") | ||
def test(request): | ||
log.info("received request; responding with 'hey'") | ||
return response.text("hey") | ||
return text("hey") | ||
|
||
|
||
sanic.run(host="0.0.0.0", port=8000) | ||
app.run(host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
from sanic import response | ||
from sanic import Sanic | ||
from sanic import Sanic, response | ||
from sanic.blueprints import Blueprint | ||
|
||
|
||
# Usage | ||
# curl -H "Host: example.com" localhost:8000 | ||
# curl -H "Host: sub.example.com" localhost:8000 | ||
# curl -H "Host: bp.example.com" localhost:8000/question | ||
# curl -H "Host: bp.example.com" localhost:8000/answer | ||
|
||
app = Sanic() | ||
app = Sanic(__name__) | ||
bp = Blueprint("bp", host="bp.example.com") | ||
|
||
|
||
@app.route('/', host=["example.com", | ||
"somethingelse.com", | ||
"therestofyourdomains.com"]) | ||
async def hello(request): | ||
@app.route( | ||
"/", host=["example.com", "somethingelse.com", "therestofyourdomains.com"] | ||
) | ||
async def hello_0(request): | ||
return response.text("Some defaults") | ||
|
||
|
||
@app.route('/', host="sub.example.com") | ||
async def hello(request): | ||
@app.route("/", host="sub.example.com") | ||
async def hello_1(request): | ||
return response.text("42") | ||
|
||
|
||
@bp.route("/question") | ||
async def hello(request): | ||
async def hello_2(request): | ||
return response.text("What is the meaning of life?") | ||
|
||
|
||
@bp.route("/answer") | ||
async def hello(request): | ||
async def hello_3(request): | ||
return response.text("42") | ||
|
||
|
||
@app.get("/name") | ||
def name(request): | ||
return response.text(request.app.url_for("name", _external=True)) | ||
|
||
|
||
app.blueprint(bp) | ||
|
||
if __name__ == '__main__': | ||
app.run(host="0.0.0.0", port=8000) | ||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters