aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2021-12-29 13:02:01 +0000
committerdakkar <dakkar@thenautilus.net>2021-12-29 13:03:42 +0000
commit5b15505d5c4670f40d1cfeac0317712a743167aa (patch)
treee4b2cf684544efffa3ebb644da4647f56c2ba9c8
parentbetter db (diff)
downloadmedia-control-5b15505d5c4670f40d1cfeac0317712a743167aa.tar.gz
media-control-5b15505d5c4670f40d1cfeac0317712a743167aa.tar.bz2
media-control-5b15505d5c4670f40d1cfeac0317712a743167aa.zip
move more stuff around
-rw-r--r--lib/App/MediaControl.rakumod80
-rw-r--r--lib/App/MediaControl/Web.rakumod58
-rw-r--r--media-control.raku25
3 files changed, 93 insertions, 70 deletions
diff --git a/lib/App/MediaControl.rakumod b/lib/App/MediaControl.rakumod
index e4b5024..77fda99 100644
--- a/lib/App/MediaControl.rakumod
+++ b/lib/App/MediaControl.rakumod
@@ -1,58 +1,46 @@
use v6.d;
-use Cro::HTTP::Server;
-use Cro::HTTP::Router;
+use DB::SQLite;
use Vlc::Client;
+use Lirc::Client;
use Lirc::Commands;
+use App::MediaControl::DB;
+use App::MediaControl::Web;
class App::MediaControl {
- has Vlc::Client $.vlc is required;
- has Lirc::Commands $.lirc is required;
- has Int $.port = 8080;
- has Cro::Service $!service handles <stop>;
-
- method start() {
- my $vlc = route {
- post -> 'play' { await self.vlc.command('pl_play') }
- post -> 'pause' { await self.vlc.command('pl_pause') }
- post -> 'stop' { await self.vlc.command('pl_stop') }
-
- get -> 'status' {
- my $status = await self.vlc.status();
- content 'application/json', $status;
- }
- }
-
- my $ir = route {
- post -> $thing, $arg {
- await self.lirc.send($thing, $arg);
- }
- }
-
- my $application = route {
- resources-from %?RESOURCES;
-
- get -> { resource 'index.html' }
- get -> 'ir.png' { resource 'ir.png' }
- get -> 'ir.webmanifest' { resource 'ir.webmanifest' }
+ has $.config is required;
+ has Vlc::Client $!vlc;
+ has Lirc::Client $!lirc-client;
+ has Lirc::Commands $!lirc;
+ has App::MediaControl::DB $!db;
+ has App::MediaControl::Web $!web;
+
+ submethod TWEAK {
+ $!vlc .= new(
+ password => $!config<vlc><password>,
+ base-uri => $!config<vlc><base-uri>,
+ );
- include :$vlc, :$ir;
+ $!lirc-client .= new();
+ $!lirc .= new(client=>$!lirc-client);
- around -> &handler {
- handler();
- CATCH {
- default {
- note "BOOM $_";
- response.status = 500;
- content 'application/json', %( error => "$_" );
- }
- }
- }
- };
+ $!db .= new(
+ pool => DB::SQLite.new(
+ filename => $!config<db><filename>,
+ ),
+ );
- $!service = Cro::HTTP::Server.new(
- :port(self.port), :$application,
+ $!web .= new(
+ port => $!config<server><port>,
+ :$!vlc, :$!lirc,
);
+ }
+
+ method start() {
+ $!db.ensure-schema();
+ $!web.start();
+ }
- return $!service.start();
+ method stop() {
+ $!web.stop();
}
}
diff --git a/lib/App/MediaControl/Web.rakumod b/lib/App/MediaControl/Web.rakumod
new file mode 100644
index 0000000..b2ac36e
--- /dev/null
+++ b/lib/App/MediaControl/Web.rakumod
@@ -0,0 +1,58 @@
+use v6.d;
+use Cro::HTTP::Server;
+use Cro::HTTP::Router;
+use Vlc::Client;
+use Lirc::Commands;
+
+class App::MediaControl::Web {
+ has Vlc::Client $.vlc is required;
+ has Lirc::Commands $.lirc is required;
+ has Int $.port = 8080;
+ has Cro::Service $!service handles <stop>;
+
+ method start() {
+ my $vlc = route {
+ post -> 'play' { await self.vlc.command('pl_play') }
+ post -> 'pause' { await self.vlc.command('pl_pause') }
+ post -> 'stop' { await self.vlc.command('pl_stop') }
+
+ get -> 'status' {
+ my $status = await self.vlc.status();
+ content 'application/json', $status;
+ }
+ }
+
+ my $ir = route {
+ post -> $thing, $arg {
+ await self.lirc.send($thing, $arg);
+ }
+ }
+
+ my $application = route {
+ resources-from %?RESOURCES;
+
+ get -> { resource 'index.html' }
+ get -> 'ir.png' { resource 'ir.png' }
+ get -> 'ir.webmanifest' { resource 'ir.webmanifest' }
+
+ include :$vlc, :$ir;
+
+ around -> &handler {
+ handler();
+ CATCH {
+ default {
+ note "BOOM $_";
+ response.status = 500;
+ content 'application/json', %( error => "$_" );
+ }
+ }
+ }
+ };
+
+ $!service = Cro::HTTP::Server.new(
+ :port(self.port), :$application,
+ );
+
+ return $!service.start();
+ }
+}
diff --git a/media-control.raku b/media-control.raku
index 8110b18..3edd8ca 100644
--- a/media-control.raku
+++ b/media-control.raku
@@ -2,34 +2,11 @@
use v6.d;
use lib 'inst#local','file#lib';
use Config::TOML;
-use DB::SQLite;
-use Vlc::Client;
-use Lirc::Client;
-use Lirc::Commands;
use App::MediaControl;
-use App::MediaControl::DB;
my $config = from-toml(file=>'config.toml');
-my Vlc::Client $vlc .= new(
- password => $config<vlc><password>,
- base-uri => $config<vlc><base-uri>,
-);
-
-my Lirc::Client $lirc-client .= new();
-my Lirc::Commands $lirc .= new(client=>$lirc-client);
-
-my App::MediaControl::DB $db .= new(
- pool => DB::SQLite.new(
- filename => $config<db><filename>,
- ),
-);
-$db.ensure-schema();
-
-my App::MediaControl $app .= new(
- port => $config<server><port>,
- :$vlc, :$lirc,
-);
+my App::MediaControl $app .= new(:$config);
$app.start;