archivebox_server.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. __command__ = 'archivebox server'
  4. import sys
  5. import argparse
  6. from pathlib import Path
  7. from typing import Optional, List, IO
  8. from archivebox.misc.util import docstring
  9. from archivebox.config import DATA_DIR
  10. from archivebox.config.common import SERVER_CONFIG
  11. from ..logging_util import SmartFormatter, reject_stdin
  12. from ..main import server
  13. @docstring(server.__doc__)
  14. def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional[str]=None) -> None:
  15. parser = argparse.ArgumentParser(
  16. prog=__command__,
  17. description=server.__doc__,
  18. add_help=True,
  19. formatter_class=SmartFormatter,
  20. )
  21. parser.add_argument(
  22. 'runserver_args',
  23. nargs='*',
  24. type=str,
  25. default=[SERVER_CONFIG.BIND_ADDR],
  26. help='Arguments to pass to Django runserver'
  27. )
  28. parser.add_argument(
  29. '--reload',
  30. action='store_true',
  31. help='Enable auto-reloading when code or templates change',
  32. )
  33. parser.add_argument(
  34. '--debug',
  35. action='store_true',
  36. help='Enable DEBUG=True mode with more verbose errors',
  37. )
  38. parser.add_argument(
  39. '--nothreading',
  40. action='store_true',
  41. help='Force runserver to run in single-threaded mode',
  42. )
  43. parser.add_argument(
  44. '--init',
  45. action='store_true',
  46. help='Run a full archivebox init/upgrade before starting the server',
  47. )
  48. parser.add_argument(
  49. '--quick-init', '-i',
  50. action='store_true',
  51. help='Run quick archivebox init/upgrade before starting the server',
  52. )
  53. parser.add_argument(
  54. '--createsuperuser',
  55. action='store_true',
  56. help='Run archivebox manage createsuperuser before starting the server',
  57. )
  58. parser.add_argument(
  59. '--daemonize',
  60. action='store_true',
  61. help='Run the server in the background as a daemon',
  62. )
  63. command = parser.parse_args(args or ())
  64. reject_stdin(__command__, stdin)
  65. server(
  66. runserver_args=command.runserver_args + (['--nothreading'] if command.nothreading else []),
  67. reload=command.reload,
  68. debug=command.debug,
  69. init=command.init,
  70. quick_init=command.quick_init,
  71. createsuperuser=command.createsuperuser,
  72. daemonize=command.daemonize,
  73. out_dir=Path(pwd) if pwd else DATA_DIR,
  74. )
  75. if __name__ == '__main__':
  76. main(args=sys.argv[1:], stdin=sys.stdin)