| 1 | package Mojolicious::Plugin::BoardStreams; | ||
| 2 | 1 | ▶ | use v5.44; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | our $VERSION = "v0.0.59"; | ||
| 5 | |||
| 6 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use BoardStreams::Hub; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Mojo::Pg; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Scalar::Util qw(blessed); | ||
| 11 | |||
| 12 | sub register ($self, $app, $conf) { | ||
| 13 | my $pg = $conf->{pg} // die "The BoardStreams plugin needs a 'pg' option (a Mojo::Pg object or connection string)\n"; | ||
| 14 | $pg = Mojo::Pg->new($pg) unless blessed $pg; | ||
| 15 | |||
| 16 | my $hub = BoardStreams::Hub->new( | ||
| 17 | pg => $pg, | ||
| 18 | log => $app->log, | ||
| 19 | map { $_ => $conf->{$_} } grep { defined $conf->{$_} } qw(retention retry_delay heartbeat_interval identify listener_check_interval), | ||
| 20 | ); | ||
| 21 | $app->helper(boardstreams => sub { $hub }); | ||
| 22 | |||
| 23 | my $routes = $conf->{routes} // $app->routes; | ||
| 24 | $routes->websocket($conf->{route} // '/boardstreams')->to(cb => sub ($c) { $hub->serve($c) }) | ||
| 25 | ->name('boardstreams'); | ||
| 26 | |||
| 27 | # Create the tables now, unless asked to wait. Either way the hub | ||
| 28 | # creates them when they are first needed, which also covers | ||
| 29 | # PostgreSQL being down right now. | ||
| 30 | unless ($conf->{lazy_migrate}) { | ||
| 31 | eval { $hub->migrate; 1 } or $app->log->warn("BoardStreams: cannot reach PostgreSQL yet: $@"); | ||
| 32 | } | ||
| 33 | |||
| 34 | return $hub; | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; | ||