diff --git a/README.md b/README.md index 470f584..685a8cb 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,11 @@ to know what they do: Run it: ```sh -python -m uvicorn app:app --port 5051 +python -m anonstream ``` -This will start a webserver listening on the local host at port 5051. +This will start a webserver listening on the local host at port 5051 (use +`--port PORT` to override). If you go to `http://localhost:5051` in a web browser now you should see the site. When you started the webserver some credentials were printed @@ -68,6 +69,24 @@ setting up OBS Studio and a Tor onion service. If you want to use different streaming software and put your stream on the Internet some other way, read those instructions and copy the gist. +## Running + +Start anonstream like this: +```sh +python -m anonstream +``` +The default port is 5051. Append `--help` to see options. + +If you want to use a different ASGI server, point it to the app factory +at `asgi:create_app()`. For example with `uvicorn`: +```sh +python -m uvicorn asgi:create_app --factory --port 5051 +``` + +In either case you can explicitly set the location of the config file +using the `ANONSTREAM_CONFIG` environment variable. + + ## Hacking anonstream has APIs for accessing internal state and hooking into diff --git a/anonstream/__main__.py b/anonstream/__main__.py new file mode 100644 index 0000000..f3801bc --- /dev/null +++ b/anonstream/__main__.py @@ -0,0 +1,32 @@ +import argparse +import os + +import uvicorn + +from anonstream import create_app + +formatter = lambda prog: argparse.HelpFormatter(prog, max_help_position=26) +parser = argparse.ArgumentParser( + 'python -m anonstream', + description='Start the anonstream webserver locally.', + formatter_class=formatter, +) +parser.add_argument( + '--config', '-c', + metavar='FILE', + default=os.environ.get('ANONSTREAM_CONFIG', 'config.toml'), + help=( + 'location of config.toml ' + '(default: $ANONSTREAM_CONFIG or ./config.toml)' + ), +) +parser.add_argument( + '--port', '-p', + type=int, + default=5051, + help='bind webserver to this port (default: 5051)', +) +args = parser.parse_args() + +app = create_app(args.config) +uvicorn.run(app, port=args.port) diff --git a/app.py b/app.py deleted file mode 100644 index 45f5b90..0000000 --- a/app.py +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-FileCopyrightText: 2022 n9k -# SPDX-License-Identifier: AGPL-3.0-or-later - -import os -import anonstream - -config_file = os.path.join(os.path.dirname(__file__), 'config.toml') -app = anonstream.create_app(config_file) - -if __name__ == '__main__': - app.run(port=5051, debug=True) diff --git a/asgi.py b/asgi.py new file mode 100644 index 0000000..4034f64 --- /dev/null +++ b/asgi.py @@ -0,0 +1,23 @@ +# SPDX-FileCopyrightText: 2022 n9k +# SPDX-License-Identifier: AGPL-3.0-or-later + +if __name__ == '__main__': + import sys + message = ( + 'To start anonstream, run one of:\n' + ' $ python -m anonstream\n' + ' $ python -m uvicorn asgi:create_app --factory --port 5051\n' + ) + print(message, file=sys.stderr, end='') + exit(1) + +import os +import anonstream + +config = os.environ.get( + 'ANONSTREAM_CONFIG', + os.path.join(os.path.dirname(__file__), 'config.toml'), +) + +def create_app(): + return anonstream.create_app(config)