2021-05-20 13:35:19 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
from sanic import Sanic, text
|
2022-09-18 15:17:23 +01:00
|
|
|
from sanic.application.constants import Mode
|
|
|
|
from sanic.config import Config
|
2021-05-20 13:35:19 +01:00
|
|
|
from sanic.log import LOGGING_CONFIG_DEFAULTS, logger
|
|
|
|
|
|
|
|
|
|
|
|
LOGGING_CONFIG = {**LOGGING_CONFIG_DEFAULTS}
|
|
|
|
LOGGING_CONFIG["formatters"]["generic"]["format"] = "%(message)s"
|
|
|
|
LOGGING_CONFIG["loggers"]["sanic.root"]["level"] = "DEBUG"
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
app = Sanic("FakeServer", log_config=LOGGING_CONFIG)
|
2021-05-20 13:35:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
async def handler(request):
|
|
|
|
return text(request.ip)
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.main_process_start
|
2021-05-20 13:35:19 +01:00
|
|
|
async def app_info_dump(app: Sanic, _):
|
|
|
|
app_data = {
|
|
|
|
"access_log": app.config.ACCESS_LOG,
|
|
|
|
"auto_reload": app.auto_reload,
|
|
|
|
"debug": app.debug,
|
2021-10-27 08:43:58 +01:00
|
|
|
"noisy_exceptions": app.config.NOISY_EXCEPTIONS,
|
2021-05-20 13:35:19 +01:00
|
|
|
}
|
|
|
|
logger.info(json.dumps(app_data))
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.main_process_stop
|
|
|
|
async def app_cleanup(app: Sanic, _):
|
|
|
|
app.state.auto_reload = False
|
|
|
|
app.state.mode = Mode.PRODUCTION
|
|
|
|
app.config = Config()
|
|
|
|
|
|
|
|
|
2021-05-20 13:35:19 +01:00
|
|
|
@app.after_server_start
|
|
|
|
async def shutdown(app: Sanic, _):
|
|
|
|
app.stop()
|
2021-06-09 10:05:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
return app
|
2022-03-23 10:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_app_with_args(args):
|
|
|
|
try:
|
2022-09-18 15:17:23 +01:00
|
|
|
logger.info(f"foo={args.foo}")
|
2022-03-23 10:00:41 +00:00
|
|
|
except AttributeError:
|
2022-09-18 15:17:23 +01:00
|
|
|
logger.info(f"module={args.module}")
|
2022-03-23 10:00:41 +00:00
|
|
|
|
|
|
|
return app
|