diff --git a/archivebox/cli/archivebox_server.py b/archivebox/cli/archivebox_server.py index a5c168cc..b7f970d0 100644 --- a/archivebox/cli/archivebox_server.py +++ b/archivebox/cli/archivebox_server.py @@ -39,6 +39,11 @@ def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional action='store_true', help='Enable DEBUG=True mode with more verbose errors', ) + parser.add_argument( + '--init', + action='store_true', + help='Run archivebox init before starting the server', + ) command = parser.parse_args(args or ()) reject_stdin(__command__, stdin) @@ -46,6 +51,7 @@ def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional runserver_args=command.runserver_args, reload=command.reload, debug=command.debug, + init=command.init, out_dir=pwd or OUTPUT_DIR, ) diff --git a/archivebox/logging_util.py b/archivebox/logging_util.py index 262a9467..6ea64daa 100644 --- a/archivebox/logging_util.py +++ b/archivebox/logging_util.py @@ -84,7 +84,9 @@ class TimedProgress: """Show a progress bar and measure elapsed time until .end() is called""" def __init__(self, seconds, prefix=''): - if SHOW_PROGRESS: + from .config import SHOW_PROGRESS + self.SHOW_PROGRESS = SHOW_PROGRESS + if self.SHOW_PROGRESS: self.p = Process(target=progress_bar, args=(seconds, prefix)) self.p.start() @@ -96,7 +98,7 @@ class TimedProgress: end_ts = datetime.now() self.stats['end_ts'] = end_ts - if SHOW_PROGRESS: + if self.SHOW_PROGRESS: # terminate if we havent already terminated self.p.terminate() self.p.join() diff --git a/archivebox/main.py b/archivebox/main.py index b6996b5e..5f6d8995 100644 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -987,38 +987,54 @@ def schedule(add: bool=False, def server(runserver_args: Optional[List[str]]=None, reload: bool=False, debug: bool=False, + init: bool=False, out_dir: str=OUTPUT_DIR) -> None: """Run the ArchiveBox HTTP server""" runserver_args = runserver_args or [] + if init: + run_subcommand('init', stdin=None, pwd=out_dir) + + # setup config for django runserver from . import config config.SHOW_PROGRESS = False - - if debug: - # if --debug is passed, patch config.DEBUG to be True for this run - config.DEBUG = True - else: - # force staticfiles to be served when DEBUG=False - # TODO: do this using nginx or another server instead of django? - runserver_args.append('--insecure') + config.DEBUG = config.DEBUG or debug check_data_folder(out_dir=out_dir) setup_django(out_dir) + from django.core.management import call_command from django.contrib.auth.models import User - if IS_TTY and not User.objects.filter(is_superuser=True).exists(): + admin_user = User.objects.filter(is_superuser=True).order_by('date_joined').only('username').last() + + print('{green}[+] Starting ArchiveBox webserver...{reset}'.format(**ANSI)) + if admin_user: + print("{lightred}[i] The admin username is:{lightblue} {}{reset}".format(admin_user.username, **ANSI)) + else: print('{lightyellow}[!] No admin users exist yet, you will not be able to edit links in the UI.{reset}'.format(**ANSI)) print() print(' To create an admin user, run:') print(' archivebox manage createsuperuser') print() - print('{green}[+] Starting ArchiveBox webserver...{reset}'.format(**ANSI)) - if not reload: + # fallback to serving staticfiles insecurely with django when DEBUG=False + if config.DEBUG: + print('DEBUG=True') + else: + runserver_args.append('--insecure') # TODO: serve statics w/ nginx instead + + # toggle autoreloading when archivebox code changes (it's on by default) + if reload: + print('AUTORELOAD=True') + else: runserver_args.append('--noreload') + config.SHOW_PROGRESS = False + config.DEBUG = config.DEBUG or debug + + call_command("runserver", *runserver_args)