package App::Clifton::Launcher;

use IO::Async::Loop;
use Log::Contextual qw(set_logger :log);
use Log::Contextual::SimpleLogger;
use aliased 'App::Clifton::Server';
use Moo;

has config_file => (is => 'ro', required => 1);

sub run {
  my ($self) = @_;
  set_logger(Log::Contextual::SimpleLogger->new({ levels => [ qw(
    info warn debug error
  ) ] }));
  my $loop = IO::Async::Loop->new;
  my $server = Server->new(
    config_file => $self->config_file,
    loop => $loop,
  );
  $loop->later(sub {
    $server->reload_config->on_finished(sub {
      log_info { "Server startup complete" };
    });
  });
  while (1) {
    local $@;
    last if eval { $loop->loop_forever; 1 };
    my $error = $@;
    log_error { "Server exception: ${error}" };
  }
  log_info { "Server shut down cleanly" };
}

1;
