use v6.d;
use Cro::HTTP::Server;
use Cro::HTTP::Router;
use Cro::WebApp::Template;
use Vlc::Client;
use Lirc::Commands;
use App::MediaControl::DB;
class App::MediaControl::Web {
has Vlc::Client $.vlc is required;
has Lirc::Commands $.lirc is required;
has App::MediaControl::DB $.db is required;
has Int $.port = 8080;
has Str $.host = '*';
has Cro::Service $!service handles <stop>;
method start() {
my $vlc = route {
post -> 'play' { await self.vlc.command('pl_play') }
post -> 'play', Int:D $id {
my $file = self.db.get-entry($id);
await self.vlc.play-file(|%(
$file<path name>:p
));
self.db.mark-entry-watched($id);
}
post -> 'pause' { await self.vlc.command('pl_pause') }
post -> 'stop' { await self.vlc.command('pl_stop') }
post -> 'subs' { await self.vlc.command('key', val=>'subtitle-track') }
post -> 'audio' { await self.vlc.command('key', val=>'audio-track') }
post -> 'seek', :$val { await self.vlc.command('seek',:$val) }
post -> 'snapshot', :$val { await self.vlc.command('key',val=>'snapshot') }
get -> 'status' {
my $status = await self.vlc.status();
my $playlist = await self.vlc.playlist();
content 'application/json', %( :$status, :$playlist );
}
}
my $ir = route {
post -> $thing, $arg {
await self.lirc.send($thing, $arg);
}
}
my $media = route {
get -> $id=Nil {
my %reply = children => @(self.db.get-children-of($id));
with $id {
%reply<parents> = self.db.get-parents-of($id);
};
content 'application/json', %reply;
}
get -> 'recent' {
content 'application/json', @(self.db.get-recently-watched-folders());
}
};
my $application = route {
resources-from %?RESOURCES;
templates-from-resources;
get -> { resource 'index.html' }
get -> 'ir.png' { resource 'ir.png' }
get -> 'ir.webmanifest' {
my $root = request.header('ProxyMountRoot') || '/';
template 'ir.webmanifest', %( :$root );
}
include :$vlc, :$ir, :$media;
around -> &handler {
handler();
CATCH {
default {
note "BOOM $_";
response.status = 500;
content 'application/json', %( error => "$_" );
}
}
}
};
$!service = Cro::HTTP::Server.new(
:host(self.host), :port(self.port),
:$application,
);
return $!service.start();
}
}