-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aiohttp.web command-line interface to serve apps #740
Changes from 2 commits
0c9b118
97d8ec0
3fbf106
de3cdc9
74639e0
0251d3d
699b1ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from .web import run_app | ||
from argparse import ArgumentParser | ||
from importlib import import_module | ||
|
||
|
||
def main(): | ||
arg_parser = ArgumentParser( | ||
description="aiohttp.web TCP/IP Application server", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
prog="aiohttp.web" | ||
) | ||
arg_parser.add_argument( | ||
"entry_func", | ||
help=("Callable returning the `aiohttp.web.Application` instance to " | ||
"run. Should be specified in the Python import syntax, " | ||
"e.g. 'package.module.function')"), | ||
metavar="entry-func" | ||
) | ||
arg_parser.add_argument( | ||
"-n", "--hostname", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, as an option -- "-H" and "-P" to don't clash with "-h/--help" |
||
help="TCP/IP hostname to serve on (default: %(default)r)", | ||
default="localhost" | ||
) | ||
arg_parser.add_argument( | ||
"-p", "--port", | ||
help="TCP/IP port to serve on (default: %(default)r)", | ||
type=int, | ||
default="8080" | ||
) | ||
args, extra_args = arg_parser.parse_known_args() | ||
|
||
# Import logic | ||
mod_str, _, func_str = args.entry_func.rpartition(".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
try: | ||
module = import_module(mod_str) | ||
func = getattr(module, func_str) | ||
except (ImportError, ValueError) as e: | ||
arg_parser.error(e) | ||
except AttributeError as e: | ||
arg_parser.error(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it may be merged with previous exception handler. |
||
|
||
app = func(extra_args) | ||
run_app(app, host=args.hostname, port=args.port) | ||
arg_parser.exit(message="Stopped\n") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Example for running Application using the `aiohttp.web` CLI. | ||
|
||
Run this app using:: | ||
|
||
$ python -m aiohttp.web web_app.init | ||
""" | ||
|
||
from aiohttp.web import Application, Response | ||
|
||
|
||
def hello_world(req): | ||
return Response(text="Hello World") | ||
|
||
|
||
def init(args): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example should have a code for demonstrating how to parse |
||
app = Application() | ||
app.router.add_route('GET', '/', hello_world) | ||
|
||
return app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the file content to the end of
web.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay...I wasn't sure about the layout.
I was trying to emulate some resemblance to a package level
__main__.py
.Speaking of which could
web
be turned into a proper sub-package, or would that break backwards compatibility?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aiohttp will keep flat structure without sub-packages.