diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b1d388d0e078054f3f35ff27a3088d3d9a7b35ee..8820f86075c3558b2e403e298502c5242a405015 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ image: docker:20.10.9 variables: DOCKER_TLS_CERTDIR: "/certs" - IMAGE_VER: 2.1.2 + IMAGE_VER: 2.2.0 services: - docker:20.10.9-dind diff --git a/Dockerfile b/Dockerfile index fb83dbc41284f9eaeeb2a98deb21c272670d4a38..01b3d7f5886919ce6657df69d3e1419b3599bc1d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,7 @@ RUN cpanm \ Media::Info \ Mojolicious@8.73 \ Mojo::Pg \ + Mojo::Redis \ Mojo::JWT \ Mojolicious::Plugin::Authentication \ Mojolicious::Plugin::Authorization @@ -39,5 +40,4 @@ RUN npm update && npm install && npm run build && rm -rf mode_modules USER nobody EXPOSE 3000 -#CMD hypnotoad -f /opt/PiTube/script/pitube CMD /opt/PiTube/script/pitube daemon diff --git a/lib/PiTube.pm b/lib/PiTube.pm index 4517d6bb101f786479e9e947319bf8a46aef5227..5c605a9d2d4567fcfb5e21fe1575b2af5108954c 100644 --- a/lib/PiTube.pm +++ b/lib/PiTube.pm @@ -2,10 +2,10 @@ package PiTube; use Mojo::Base 'Mojolicious'; use Mojo::Pg; +use Mojo::Redis; use Mojolicious::Plugin::Authentication; use Mojolicious::Plugin::Authorization; use Net::OAuth2::Profile::WebServer; -use Redis; use PiTube::Schema; sub startup { @@ -24,7 +24,10 @@ sub startup { $self->plugin('PiTube::Helpers::OAuth2'); # Pripojeni na redis - my $redis = Redis->new( %{ $cfg->{redis} } ); +# my $redis = Redis->new( %{ $cfg->{redis} } ); +# $self->helper( redis => sub { return $redis; } ); + + my $redis = Mojo::Redis->new( 'redis://' . $cfg->{redis}{server} ); $self->helper( redis => sub { return $redis; } ); # migrace schematu diff --git a/lib/PiTube/Controller/Nginx.pm b/lib/PiTube/Controller/Nginx.pm index 28f791d3030d175398db030545e6542d1b8d778c..78ef8247be892cf70127e5e24425304e811bb610 100644 --- a/lib/PiTube/Controller/Nginx.pm +++ b/lib/PiTube/Controller/Nginx.pm @@ -1,7 +1,6 @@ package PiTube::Controller::Nginx; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Pg::PubSub; use feature 'signatures'; no warnings qw{ experimental::signatures }; @@ -9,7 +8,6 @@ no warnings qw{ experimental::signatures }; use constant HLS => qr/hls\/([a-z0-9\-]+)(_\w+)?(\/\w+)?\.(m3u8|ts)$/i; sub callback_rtmp($c) { - my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); if ( $c->param('call') =~ /publish/ ) { my $name = $c->param('name'); @@ -43,7 +41,7 @@ sub callback_rtmp($c) { is_live => ( $c->param('call') =~ /done/ ) ? 'f':'t', }); - $pubsub->json('streams')->notify( streams => { + $c->redis->pubsub->json('streams')->notify( streams => { call => $c->param('call'), stream_id => $stream->id, }); diff --git a/lib/PiTube/Controller/Websockets.pm b/lib/PiTube/Controller/Websockets.pm index 0dbc90763c17576673efd4e08aabb72e31852735..4267ff40ad28715bb6382b12d7ce3e70b02227a9 100644 --- a/lib/PiTube/Controller/Websockets.pm +++ b/lib/PiTube/Controller/Websockets.pm @@ -1,7 +1,7 @@ package PiTube::Controller::Websockets; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Pg::PubSub; +use Mojo::JSON qw(encode_json); use feature 'signatures'; no warnings qw{ experimental::signatures }; @@ -10,7 +10,7 @@ use constant SOCKET_INACTIVITY_TIMEOUT => 300; sub main { my $c = shift; - my $ip = $c->tx->remote_address; +# my $ip = $c->tx->remote_address; my $key = $c->req->headers->header('Sec-WebSocket-Key'); $c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT); @@ -20,25 +20,25 @@ sub main { $c->res->headers->add('Sec-WebSocket-Extensions' => 'permessage-deflate'); } - my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); - - $pubsub->listen(streams => sub($pubsub, $payload) { - $c->send($payload); - }); + $c->redis->pubsub->json('streams')->listen( + streams => sub($pubsub, $payload) { + $c->send({json => $payload}); + } + ); $c->on(json => sub( $c, $message ) { if ( $message->{stream} ) { - $c->redis->set( + $c->redis->db->set( join (':', ('live', $message->{stream}, $key)), 'live', 'EX', 16 ); - my $count = $c->redis->keys( 'live:' . $message->{stream} . ':*' ); - $c->send({json => { watchers => $count }}); + my $keys = $c->redis->db->keys( 'live:' . $message->{stream} . ':*' ); + $c->send(encode_json({ watchers => scalar @{$keys} })); } }); $c->on(finish => sub ($c, $code, $reason = undef) { - $pubsub->unlisten('streams'); + $c->redis->pubsub->unlisten('streams'); $c->app->log->debug("WebSocket closed with status $code"); }); }