archivebox_server.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. from typing import Iterable
  4. import rich_click as click
  5. from rich import print
  6. from archivebox.misc.util import docstring, enforce_types
  7. from archivebox.config.common import SERVER_CONFIG
  8. @enforce_types
  9. def server(runserver_args: Iterable[str]=(SERVER_CONFIG.BIND_ADDR,),
  10. reload: bool=False,
  11. init: bool=False,
  12. debug: bool=False,
  13. daemonize: bool=False,
  14. nothreading: bool=False) -> None:
  15. """Run the ArchiveBox HTTP server"""
  16. runserver_args = list(runserver_args)
  17. if init:
  18. from archivebox.cli.archivebox_init import init as archivebox_init
  19. archivebox_init(quick=True)
  20. print()
  21. from archivebox.misc.checks import check_data_folder
  22. check_data_folder()
  23. from django.core.management import call_command
  24. from django.contrib.auth.models import User
  25. from archivebox.config.common import SHELL_CONFIG
  26. if not User.objects.filter(is_superuser=True).exclude(username='system').exists():
  27. print()
  28. print('[violet]Hint:[/violet] To create an [bold]admin username & password[/bold] for the [deep_sky_blue3][underline][link=http://{host}:{port}/admin]Admin UI[/link][/underline][/deep_sky_blue3], run:')
  29. print(' [green]archivebox manage createsuperuser[/green]')
  30. print()
  31. host = '127.0.0.1'
  32. port = '8000'
  33. try:
  34. host_and_port = [arg for arg in runserver_args if arg.replace('.', '').replace(':', '').isdigit()][0]
  35. if ':' in host_and_port:
  36. host, port = host_and_port.split(':')
  37. else:
  38. if '.' in host_and_port:
  39. host = host_and_port
  40. else:
  41. port = host_and_port
  42. except IndexError:
  43. pass
  44. print('[green][+] Starting ArchiveBox webserver...[/green]')
  45. print(f' [blink][green]>[/green][/blink] Starting ArchiveBox webserver on [deep_sky_blue4][link=http://{host}:{port}]http://{host}:{port}[/link][/deep_sky_blue4]')
  46. print(f' [green]>[/green] Log in to ArchiveBox Admin UI on [deep_sky_blue3][link=http://{host}:{port}/admin]http://{host}:{port}/admin[/link][/deep_sky_blue3]')
  47. print(' > Writing ArchiveBox error log to ./logs/errors.log')
  48. if SHELL_CONFIG.DEBUG:
  49. if not reload:
  50. runserver_args.append('--noreload') # '--insecure'
  51. if nothreading:
  52. runserver_args.append('--nothreading')
  53. call_command("runserver", *runserver_args)
  54. else:
  55. from workers.supervisord_util import start_server_workers
  56. print()
  57. start_server_workers(host=host, port=port, daemonize=daemonize)
  58. print("\n[i][green][🟩] ArchiveBox server shut down gracefully.[/green][/i]")
  59. @click.command()
  60. @click.argument('runserver_args', nargs=-1)
  61. @click.option('--reload', is_flag=True, help='Enable auto-reloading when code or templates change')
  62. @click.option('--debug', is_flag=True, help='Enable DEBUG=True mode with more verbose errors')
  63. @click.option('--nothreading', is_flag=True, help='Force runserver to run in single-threaded mode')
  64. @click.option('--init', is_flag=True, help='Run a full archivebox init/upgrade before starting the server')
  65. @click.option('--daemonize', is_flag=True, help='Run the server in the background as a daemon')
  66. @docstring(server.__doc__)
  67. def main(**kwargs):
  68. server(**kwargs)
  69. if __name__ == '__main__':
  70. main()