Skip to content

Commit

Permalink
More refactoring of database connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jan 15, 2016
1 parent c11f0ce commit fa7b285
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/Lystyng.pm
Expand Up @@ -9,14 +9,16 @@ package Lystyng;
use Dancer2;
our $VERSION = '0.0.1';
use Dancer2::Plugin::DBIC qw[schema resultset];
use Lystyng::Schema;

defined $ENV{LYSTYNG_DB_USER} && defined $ENV{LYSTYNG_DB_PASS}
or die 'Must set LYSTYNG_DB_USER and LYSTYNG_DB_PASS';
Lystyng::Schema->check_env();

hook before => sub {
my $cfg = dancer_app->config;
$cfg->{plugins}{DBIC}{default}{user} = $ENV{LYSTYNG_DB_USER};
$cfg->{plugins}{DBIC}{default}{password} = $ENV{LYSTYNG_DB_PASS};
$cfg->{plugins}{DBIC}{default}{dsn} =
"dbi:mysql:dbname=$ENV{LYSTYNG_DB_NAME};hostname=$ENV{LYSTYNG_DB_SERVER}";
};

get '/' => sub {
Expand Down
6 changes: 5 additions & 1 deletion lib/Lystyng/Schema.pm
Expand Up @@ -17,7 +17,7 @@ __PACKAGE__->load_namespaces;

# You can replace this text with custom code or comments, and it will be preserved on regeneration

sub get_schema {
sub check_env {
my @errors;
foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
Expand All @@ -27,6 +27,10 @@ sub get_schema {
if (@errors) {
die("Missing connection info: @errors");
}
}

sub get_schema {
__PACKAGE__->check_env();

return __PACKAGE__->connect(
"dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
Expand Down

0 comments on commit fa7b285

Please sign in to comment.