Skip to content

Commit

Permalink
add DBIish plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler committed Feb 12, 2024
1 parent 5f43d8a commit 6a01f03
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.precomp
.gitconfig
sdist
3 changes: 2 additions & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"Humming-Bird::Plugin": "lib/Humming-Bird/Plugin.rakumod",
"Humming-Bird::Plugin::Config": "lib/Humming-Bird/Plugin/Config.rakumod",
"Humming-Bird::Plugin::Logger": "lib/Humming-Bird/Plugin/Logger.rakumod",
"Humming-Bird::Plugin::Session": "lib/Humming-Bird/Plugin/Session.rakumod"
"Humming-Bird::Plugin::Session": "lib/Humming-Bird/Plugin/Session.rakumod",
"Humming-Bird::Plugin::DBIish": "lib/Humming-Bird/Plugin/DBIish.rakumod"
},
"resources": [
],
Expand Down
7 changes: 5 additions & 2 deletions lib/Humming-Bird/Backend/HTTPServer.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ my constant $RN = "\r\n".encode.Buf;

has Channel:D $.requests .= new;
has Lock $!lock .= new;
has IO::Socket::Async::ListenSocket $!socket handles <close>;
has @!connections;

method !timeout {
Expand Down Expand Up @@ -66,11 +67,13 @@ method !respond(&handler) {
}

method listen(&handler) {
react whenever signal(SIGINT) { self.close; }

react {
self!timeout;
self!respond(&handler);

whenever IO::Socket::Async.listen($.addr // '0.0.0.0', $.port) -> $connection {
$!socket = IO::Socket::Async.listen($.addr // '0.0.0.0', $.port);
whenever $!socket -> $connection {
my %connection-map := {
socket => $connection,
last-active => now
Expand Down
6 changes: 3 additions & 3 deletions lib/Humming-Bird/Core.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ sub listen(Int:D $port, Str:D $addr = '0.0.0.0', :$no-block, :$timeout = 3, :$ba
require ::($fq);
CATCH {
default {
die "It doesn't look like $fq is a valid plugin? Are you sure it's installed? $_";
die "It doesn't look like $fq is a valid plugin? Are you sure it's installed?\n\n$_";
}
}
}
Expand All @@ -327,9 +327,9 @@ sub listen(Int:D $port, Str:D $addr = '0.0.0.0', :$no-block, :$timeout = 3, :$ba

say(
colored('Humming-Bird', 'green'),
" listening on port http://$addr:$port"
" listening on port http://$addr:$port",
"\n"
);
say '';
say(
colored('Warning', 'yellow'),
': Humming-Bird is currently running in DEV mode, please set HUMMING_BIRD_ENV to PROD or PRODUCTION to enable PRODUCTION mode.',
Expand Down
54 changes: 54 additions & 0 deletions lib/Humming-Bird/Plugin/DBIish.rakumod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use MONKEY-TYPING;
use Humming-Bird::Plugin;
use Humming-Bird::Core;

unit class Humming-Bird::Plugin::DBIish does Humming-Bird::Plugin;

my %databases;

method register($server, %routes, @middleware, @advice, **@args) {
my $database-name = 'default';
my @database-args;

if @args.elems == 1 {
if @args[0].isa(Array) || @args[0].isa(List) {
@database-args = |@args[0];
} else {
$database-name = @args[0];
}
} else {
$database-name = @args[0];
@database-args = |@args[1];
}

try {
require ::('DBIish');
CATCH {
default {
die 'DBIish is not installed, to use Humming-Bird::Plugin::DBIish, please install DBIish. "zef install DBIish"'
}
}
}

without @database-args {
die 'Invalid configuration for Humming-Bird::Plugin::DBIish, please provide arguments.'
}

if (%databases.keys.elems == 0) {
augment class Humming-Bird::Glue::HTTPAction {
method db(Str $database = 'default') {
%databases{$database};
}
}
}

use DBIish;
%databases{$database-name} = DBIish.connect(|@database-args);

CATCH {
default {
die 'Failed to setup Humming-Bird::Plugin::DBIish cause: $_';
}
}
}

39 changes: 39 additions & 0 deletions lib/Humming-Bird/Plugin/HotReload.rakumod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use MONKEY-TYPING;
use Humming-Bird::Plugin;
use Humming-Bird::Core;
use Humming-Bird::Backend;

unit class Humming-Bird::Plugin::HotReload does Humming-Bird::Plugin;

class Humming-Bird::Backend::HotReload does Humming-Bird::Backend {
has $.backend handles <port addr timeout>;
has Channel:D $!reload-chan .= new;
method listen(&handler) {
self!observe();
self!start-server();

react whenever $!reload-chan -> $reload {
$.backend.close;
self!start-server();
}
}

method !start-server {
start {
listen(self.port, self.addr, );
}
}

method !observe {

}
}

method register($server is rw, %routes, @middleware, @advice, **@args) {
$server = Humming-Bird::Backend::HotReload.new(backend => $server);
CATCH {
default {
warn 'Failed to find or parse your ".humming-bird.json" configuration. Ensure your file is well formed, and does exist.';
}
}
}
41 changes: 41 additions & 0 deletions t/opt/plugin-dbiish.rakutest
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use v6;
use lib 'lib';
use Test;
use Humming-Bird::Core;
use Humming-Bird::Glue;
use Humming-Bird::Backend;
use Humming-Bird::Middleware;
use Humming-Bird::Advice;
use Humming-Bird::Plugin::DBIish;

try {
require ::('DBIish');

CATCH {
default {
warn 'DBIish not installed, skipping Humming-Bird::Plugin::DBIish tests.';
skip-rest;
}
}
}

use DBDish::TestMock::Connection;

class TestBackend does Humming-Bird::Backend {
method listen(&handler) {
return; # Does nothing
}
}

lives-ok sub { plugin('DBIish', ['TestMock']); }, 'Does default plugin not die?';
lives-ok sub { plugin('DBIish', 'other-db', ['TestMock']); }, 'Does other-db plugin not die?';\
lives-ok sub { listen(8080, :backend(TestBackend)); }, 'Does plugin register ok?';
ok Humming-Bird::Glue::HTTPAction.^can('db'), 'Did plugin properly run?';
my $action = Humming-Bird::Glue::HTTPAction.new;

ok $action.^can('db')[0].($action);
ok $action.^can('db')[0].($action, 'other-db');
ok $action.^can('db')[0].($action).isa(DBDish::TestMock::Connection);
ok $action.^can('db')[0].($action, 'other-db').isa(DBDish::TestMock::Connection);

done-testing;

0 comments on commit 6a01f03

Please sign in to comment.