2020-06-05 15:14:18 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2016-10-18 09:22:49 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from importlib import import_module
|
2019-09-22 21:55:36 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2016-10-18 09:22:49 +01:00
|
|
|
|
2020-10-25 18:09:42 +00:00
|
|
|
from sanic import __version__
|
2017-02-20 09:57:15 +00:00
|
|
|
from sanic.app import Sanic
|
2018-10-18 05:20:16 +01:00
|
|
|
from sanic.log import logger
|
|
|
|
|
2016-10-18 09:22:49 +01:00
|
|
|
|
2020-06-05 15:14:18 +01:00
|
|
|
def main():
|
2018-10-14 01:55:33 +01:00
|
|
|
parser = ArgumentParser(prog="sanic")
|
|
|
|
parser.add_argument(
|
2020-10-25 18:21:09 +00:00
|
|
|
"-H",
|
|
|
|
"--host",
|
|
|
|
dest="host",
|
|
|
|
type=str,
|
|
|
|
default="127.0.0.1",
|
|
|
|
help="Host address [default 127.0.0.1]",
|
2018-10-14 01:55:33 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-10-25 18:21:09 +00:00
|
|
|
"-p",
|
|
|
|
"--port",
|
|
|
|
dest="port",
|
|
|
|
type=int,
|
|
|
|
default=8000,
|
|
|
|
help="Port to serve on [default 8000]",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-u",
|
|
|
|
"--unix",
|
|
|
|
dest="unix",
|
|
|
|
type=str,
|
|
|
|
default="",
|
|
|
|
help="Location of unix socket",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--cert", dest="cert", type=str, help="Location of certificate for SSL"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--key", dest="key", type=str, help="Location of keyfile for SSL."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-w",
|
|
|
|
"--workers",
|
|
|
|
dest="workers",
|
|
|
|
type=int,
|
|
|
|
default=1,
|
|
|
|
help="Number of worker processes [default 1]",
|
2018-10-14 01:55:33 +01:00
|
|
|
)
|
|
|
|
parser.add_argument("--debug", dest="debug", action="store_true")
|
2020-10-25 18:09:42 +00:00
|
|
|
parser.add_argument(
|
2020-10-25 18:21:09 +00:00
|
|
|
"-v", "--version", action="version", version=f"Sanic {__version__}",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"module", help="Path to your Sanic app. Example: path.to.server:app"
|
2020-10-25 18:09:42 +00:00
|
|
|
)
|
2016-10-18 09:22:49 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
2020-06-05 15:14:18 +01:00
|
|
|
module_path = os.path.abspath(os.getcwd())
|
|
|
|
if module_path not in sys.path:
|
|
|
|
sys.path.append(module_path)
|
|
|
|
|
2020-10-25 18:09:42 +00:00
|
|
|
if ":" in args.module:
|
|
|
|
module_name, app_name = args.module.rsplit(":", 1)
|
|
|
|
else:
|
|
|
|
module_parts = args.module.split(".")
|
|
|
|
module_name = ".".join(module_parts[:-1])
|
|
|
|
app_name = module_parts[-1]
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
module = import_module(module_name)
|
|
|
|
app = getattr(module, app_name, None)
|
2020-04-06 20:45:25 +01:00
|
|
|
app_name = type(app).__name__
|
|
|
|
|
2016-12-26 11:37:16 +00:00
|
|
|
if not isinstance(app, Sanic):
|
2018-10-14 01:55:33 +01:00
|
|
|
raise ValueError(
|
2020-04-06 20:45:25 +01:00
|
|
|
f"Module is not a Sanic app, it is a {app_name}. "
|
|
|
|
f"Perhaps you meant {args.module}.app?"
|
2018-10-14 01:55:33 +01:00
|
|
|
)
|
2017-04-08 21:31:11 +01:00
|
|
|
if args.cert is not None or args.key is not None:
|
2019-09-22 21:55:36 +01:00
|
|
|
ssl = {
|
|
|
|
"cert": args.cert,
|
|
|
|
"key": args.key,
|
|
|
|
} # type: Optional[Dict[str, Any]]
|
2017-04-08 21:31:11 +01:00
|
|
|
else:
|
|
|
|
ssl = None
|
2016-10-18 09:22:49 +01:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
app.run(
|
|
|
|
host=args.host,
|
|
|
|
port=args.port,
|
2020-06-29 06:55:32 +01:00
|
|
|
unix=args.unix,
|
2018-10-14 01:55:33 +01:00
|
|
|
workers=args.workers,
|
|
|
|
debug=args.debug,
|
|
|
|
ssl=ssl,
|
|
|
|
)
|
2017-04-27 03:57:19 +01:00
|
|
|
except ImportError as e:
|
2018-10-14 01:55:33 +01:00
|
|
|
logger.error(
|
2020-04-06 20:45:25 +01:00
|
|
|
f"No module named {e.name} found.\n"
|
|
|
|
f" Example File: project/sanic_server.py -> app\n"
|
|
|
|
f" Example Module: project.sanic_server.app"
|
2018-10-14 01:55:33 +01:00
|
|
|
)
|
2018-10-26 09:29:53 +01:00
|
|
|
except ValueError:
|
2018-09-29 21:20:20 +01:00
|
|
|
logger.exception("Failed to run app")
|
2020-06-05 15:14:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|