|
| 1 | +import os |
1 | 2 | from logging import getLogger, DEBUG
|
2 | 3 |
|
3 | 4 | import typer
|
|
16 | 17 | logger = get_logger(__name__)
|
17 | 18 |
|
18 | 19 |
|
19 |
| -@app.command() |
20 |
| -def start(): |
21 |
| - run('gunicorn --bind 0.0.0.0:5000 wsgi:app'.split(' ')) |
| 20 | +@app.command('start') |
| 21 | +def start( |
| 22 | + open_gui: bool = typer.Option( |
| 23 | + default=False, |
| 24 | + help='Determines whether the GUI should be served at the root path, or behind a unique key.', |
| 25 | + ), |
| 26 | + host: str = typer.Option( |
| 27 | + default='0.0.0.0' |
| 28 | + ), |
| 29 | + port: int = typer.Option( |
| 30 | + default=5000 |
| 31 | + ) |
| 32 | +): |
| 33 | + |
| 34 | + def clear_gui_key(): |
| 35 | + try: |
| 36 | + os.remove('.gui_key') |
| 37 | + except FileNotFoundError: |
| 38 | + pass |
| 39 | + |
| 40 | + def generate_gui_key(): |
| 41 | + import secrets |
| 42 | + if os.path.exists('.gui_key'): |
| 43 | + pass |
| 44 | + else: |
| 45 | + open('.gui_key', 'w').write(secrets.token_urlsafe(24)) |
| 46 | + |
| 47 | + def read_gui_key(): |
| 48 | + return open('.gui_key', 'r').read() |
| 49 | + |
| 50 | + def print_gui_info(): |
| 51 | + if open_gui: |
| 52 | + print('GUI is set to [OPEN] - it will be served at the root path.') |
| 53 | + print(f'\n\tView GUI dashboard here: http://{host}:{port}\n') |
| 54 | + else: |
| 55 | + print('GUI is set to [CLOSED] - it will be served at the path /?guiKey=<unique_key>') |
| 56 | + print(f'\n\tView GUI dashboard here: http://{host}:{port}?guiKey={read_gui_key()}\n') |
| 57 | + print('To run the GUI in [OPEN] mode (for development purposes only), run the following command: tvwb start --open-gui') |
| 58 | + gui_modes_url = 'https://github.com/robswc/tradingview-webhooks-bot/discussions/43' |
| 59 | + print(f'To learn more about GUI modes, visit: {gui_modes_url}') |
| 60 | + |
| 61 | + |
| 62 | + def run_server(): |
| 63 | + run(f'gunicorn --bind {host}:{port} wsgi:app'.split(' ')) |
| 64 | + |
| 65 | + # clear gui key if gui is set to open, else generate key |
| 66 | + if open_gui: |
| 67 | + clear_gui_key() |
| 68 | + else: |
| 69 | + generate_gui_key() |
| 70 | + |
| 71 | + print_gui_info() |
| 72 | + run_server() |
| 73 | + |
22 | 74 |
|
23 | 75 |
|
24 | 76 | @app.command('action:create')
|
@@ -156,5 +208,6 @@ def shell():
|
156 | 208 | run(f'python3 tvwb.py {cmd}'.split(' '))
|
157 | 209 | cmd = typer.prompt("Enter TVWB command (q) to exit")
|
158 | 210 |
|
| 211 | + |
159 | 212 | if __name__ == "__main__":
|
160 | 213 | app()
|
0 commit comments