2016-10-18 09:22:49 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from importlib import import_module
|
|
|
|
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.log import log
|
2017-02-20 09:57:15 +00:00
|
|
|
from sanic.app import Sanic
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser(prog='sanic')
|
|
|
|
parser.add_argument('--host', dest='host', type=str, default='127.0.0.1')
|
|
|
|
parser.add_argument('--port', dest='port', type=int, default=8000)
|
2017-03-08 03:54:02 +00:00
|
|
|
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.')
|
2016-10-18 09:22:49 +01:00
|
|
|
parser.add_argument('--workers', dest='workers', type=int, default=1, )
|
|
|
|
parser.add_argument('--debug', dest='debug', action="store_true")
|
|
|
|
parser.add_argument('module')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
|
|
|
module_parts = args.module.split(".")
|
|
|
|
module_name = ".".join(module_parts[:-1])
|
|
|
|
app_name = module_parts[-1]
|
|
|
|
|
|
|
|
module = import_module(module_name)
|
|
|
|
app = getattr(module, app_name, None)
|
2016-12-26 11:37:16 +00:00
|
|
|
if not isinstance(app, Sanic):
|
2016-10-18 09:22:49 +01:00
|
|
|
raise ValueError("Module is not a Sanic app, it is a {}. "
|
|
|
|
"Perhaps you meant {}.app?"
|
|
|
|
.format(type(app).__name__, args.module))
|
2017-04-08 21:31:11 +01:00
|
|
|
if args.cert is not None or args.key is not None:
|
|
|
|
ssl = {'cert': args.cert, 'key': args.key}
|
|
|
|
else:
|
|
|
|
ssl = None
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
app.run(host=args.host, port=args.port,
|
2017-04-08 21:31:11 +01:00
|
|
|
workers=args.workers, debug=args.debug, ssl=ssl)
|
2017-04-27 03:57:19 +01:00
|
|
|
except ImportError as e:
|
|
|
|
log.error("{} found.\n"
|
2016-10-18 09:22:49 +01:00
|
|
|
" Example File: project/sanic_server.py -> app\n"
|
|
|
|
" Example Module: project.sanic_server.app"
|
2017-04-27 03:57:19 +01:00
|
|
|
.format(e))
|
2016-10-18 09:22:49 +01:00
|
|
|
except ValueError as e:
|
2016-10-18 09:38:50 +01:00
|
|
|
log.error("{}".format(e))
|