From a165836816249cfb0d618489e85554a8d748f7e7 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Mon, 26 Apr 2021 15:35:38 -0400 Subject: [PATCH 1/3] not happy with this, going to almost immediately revert it and then bring small pieces back --- app.pl | 5 ++++ app.cfg => config/base.toml | 0 config/development.toml | 0 cpanfile | 1 + lib/App.pm | 6 +++-- lib/App/Config.pm | 30 ----------------------- lib/App/Controller/Paste.pm | 5 ++-- lib/App/Model/Paste.pm | 2 -- lib/App/Plugins/TomlConfig.pm | 46 +++++++++++++++++++++++++++++++++++ 9 files changed, 58 insertions(+), 37 deletions(-) rename app.cfg => config/base.toml (100%) create mode 100644 config/development.toml delete mode 100644 lib/App/Config.pm create mode 100644 lib/App/Plugins/TomlConfig.pm diff --git a/app.pl b/app.pl index 57193e5..f671b29 100755 --- a/app.pl +++ b/app.pl @@ -5,6 +5,11 @@ use warnings; use lib 'lib'; use Mojolicious::Commands; +use Mojolicious::Plugins; + +my $plugins = Mojolicious::Plugins->new; +push @{$plugins->namespaces}, 'App::Plugins'; + # Start command line interface for application Mojolicious::Commands->start_app('App'); diff --git a/app.cfg b/config/base.toml similarity index 100% rename from app.cfg rename to config/base.toml diff --git a/config/development.toml b/config/development.toml new file mode 100644 index 0000000..e69de29 diff --git a/cpanfile b/cpanfile index 09068cc..bdef12c 100644 --- a/cpanfile +++ b/cpanfile @@ -13,3 +13,4 @@ requires 'Mojolicious::Plugin::RemoteAddr'; requires 'App::EvalServerAdvanced::Protocol'; requires 'Future::Mojo'; requires 'Regexp::Assemble'; +requires 'Hash::Merge'; diff --git a/lib/App.pm b/lib/App.pm index ca9e1f4..5620285 100644 --- a/lib/App.pm +++ b/lib/App.pm @@ -7,7 +7,6 @@ use v5.22; use Mojo::Base 'Mojolicious'; use Mojolicious::Plugin::TtRenderer; -use App::Config; use App::Controller::Paste; use App::Controller::Eval; use App::Controller::Apiv1; @@ -20,10 +19,13 @@ use App::Model::Languages; sub startup { my $self = shift; - $self->config($cfg->{mojolicious}); + # TODO get this to load the proper stuff from the config into the app + my $cfg_plg = $self->plugin('TomlConfig'); + # $self->config($cfg->{mojolicious}); $self->plugin('RemoteAddr'); + $self->plugin('tt_renderer' => { template_options => { PRE_CHOMP => 1, diff --git a/lib/App/Config.pm b/lib/App/Config.pm deleted file mode 100644 index 86be0f9..0000000 --- a/lib/App/Config.pm +++ /dev/null @@ -1,30 +0,0 @@ -package App::Config; - -use strict; -use warnings; - -use Exporter qw/import/; -use Data::Dumper; -use FindBin qw($Bin); - -use TOML; - -our @EXPORT=qw/$cfg/; - -our $cfg = do { - my $toml = do {open(my $fh, "<", "$Bin/app.cfg"); local $/; <$fh>}; -# With error checking - my ($data, $err) = from_toml($toml); - unless ($data) { - die "Error parsing toml: $err"; - } - $data; -}; - -sub get_config { - my $key = shift; - - return $cfg->{$key}; -} - -1; diff --git a/lib/App/Controller/Paste.pm b/lib/App/Controller/Paste.pm index 64743cf..eed7dfe 100644 --- a/lib/App/Controller/Paste.pm +++ b/lib/App/Controller/Paste.pm @@ -3,7 +3,6 @@ package App::Controller::Paste; use strict; use warnings; -use App::Config; use Mojo::Base 'Mojolicious::Controller'; use Mojo::IOLoop; use Mojo::Promise; @@ -40,7 +39,7 @@ sub to_root { sub root { my $c = shift; $c->stash({languages => $c->languages->get_languages}); - $c->stash({pastedata => q{}, channels => $cfg->{announce}{channels}, page_tmpl => 'editor.html'}); + $c->stash({pastedata => q{}, channels => $c->app->config('announce')->{channels}, page_tmpl => 'editor.html'}); $c->render("page"); }; @@ -53,7 +52,7 @@ sub edit_paste { if ($row->{when}) { $c->stash({languages => $c->languages->get_languages}); - $c->stash({pastedata => $row->{paste}, channels =>$cfg->{announce}{channels}}); + $c->stash({pastedata => $row->{paste}, channels =>$c->app->config('announce')->{channels}}); $c->stash({page_tmpl => 'editor.html'}); $c->render(template => 'page'); diff --git a/lib/App/Model/Paste.pm b/lib/App/Model/Paste.pm index 02cd5b6..e599d95 100644 --- a/lib/App/Model/Paste.pm +++ b/lib/App/Model/Paste.pm @@ -4,7 +4,6 @@ use strict; use warnings; use DBI; -use App::Config; use Mojo::Base '-base'; use DateTime; use Mojo::Pg; @@ -12,7 +11,6 @@ use Regexp::Assemble; # TODO config for dbname # has 'dbh' => sub {DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", {RaiseError => 1, sqlite_unicode => 1})}; -my $cfg = App::Config::get_config('database'); has 'pg' => sub { Mojo::Pg->new($cfg->{dsc}); diff --git a/lib/App/Plugins/TomlConfig.pm b/lib/App/Plugins/TomlConfig.pm new file mode 100644 index 0000000..17f2a1e --- /dev/null +++ b/lib/App/Plugins/TomlConfig.pm @@ -0,0 +1,46 @@ +package App::Plugins::TomlConfig; +use Mojo::Base 'Mojolicious::Plugin::Config'; + +use v5.24; +use strict; +use warnings; + +use Data::Dumper; + +use TOML; +use Hash::Merge; +use Syntax::Keyword::Try; +use Path::Tiny; + +sub parse { + my ($self, $content, $file, $conf, $app) = @_; + + my $merged_config; + + try { + my $env_file = path($file)->child($env.".cfg"); + my $base_file = path($file)->child('base.cfg'); + + my $base_config_data = $base_file->slurp_utf8(); + my $env_config_data = $env_file->slurp_utf8(); + + my ($base_config, $base_error) = from_toml($base_config_data); + die "$base_file: $base_error" if $base_error; + + my $env_config = from_toml($env_config_data); + die "$env_file: $env_error" if $env_error; + + $merged_config = merge($base_config, $env_config); + } catch($e) { + die "Unable to process config file: $e"; + } + + die Dumper($merged_config); + return $merged_config; +} + +# TODO figure out what i want here +sub register { shift->SUPER::register(shift, {%{shift()}}) } + + +1; From 68e63c2cf85db5ceeb7076b16e23760b18c3b9a1 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Mon, 26 Apr 2021 15:35:46 -0400 Subject: [PATCH 2/3] Revert "not happy with this, going to almost immediately revert it and then bring small pieces back" This reverts commit a165836816249cfb0d618489e85554a8d748f7e7. --- config/base.toml => app.cfg | 0 app.pl | 5 ---- config/development.toml | 0 cpanfile | 1 - lib/App.pm | 6 ++--- lib/App/Config.pm | 30 +++++++++++++++++++++++ lib/App/Controller/Paste.pm | 5 ++-- lib/App/Model/Paste.pm | 2 ++ lib/App/Plugins/TomlConfig.pm | 46 ----------------------------------- 9 files changed, 37 insertions(+), 58 deletions(-) rename config/base.toml => app.cfg (100%) delete mode 100644 config/development.toml create mode 100644 lib/App/Config.pm delete mode 100644 lib/App/Plugins/TomlConfig.pm diff --git a/config/base.toml b/app.cfg similarity index 100% rename from config/base.toml rename to app.cfg diff --git a/app.pl b/app.pl index f671b29..57193e5 100755 --- a/app.pl +++ b/app.pl @@ -5,11 +5,6 @@ use warnings; use lib 'lib'; use Mojolicious::Commands; -use Mojolicious::Plugins; - -my $plugins = Mojolicious::Plugins->new; -push @{$plugins->namespaces}, 'App::Plugins'; - # Start command line interface for application Mojolicious::Commands->start_app('App'); diff --git a/config/development.toml b/config/development.toml deleted file mode 100644 index e69de29..0000000 diff --git a/cpanfile b/cpanfile index bdef12c..09068cc 100644 --- a/cpanfile +++ b/cpanfile @@ -13,4 +13,3 @@ requires 'Mojolicious::Plugin::RemoteAddr'; requires 'App::EvalServerAdvanced::Protocol'; requires 'Future::Mojo'; requires 'Regexp::Assemble'; -requires 'Hash::Merge'; diff --git a/lib/App.pm b/lib/App.pm index 5620285..ca9e1f4 100644 --- a/lib/App.pm +++ b/lib/App.pm @@ -7,6 +7,7 @@ use v5.22; use Mojo::Base 'Mojolicious'; use Mojolicious::Plugin::TtRenderer; +use App::Config; use App::Controller::Paste; use App::Controller::Eval; use App::Controller::Apiv1; @@ -19,13 +20,10 @@ use App::Model::Languages; sub startup { my $self = shift; - # TODO get this to load the proper stuff from the config into the app - my $cfg_plg = $self->plugin('TomlConfig'); - # $self->config($cfg->{mojolicious}); + $self->config($cfg->{mojolicious}); $self->plugin('RemoteAddr'); - $self->plugin('tt_renderer' => { template_options => { PRE_CHOMP => 1, diff --git a/lib/App/Config.pm b/lib/App/Config.pm new file mode 100644 index 0000000..86be0f9 --- /dev/null +++ b/lib/App/Config.pm @@ -0,0 +1,30 @@ +package App::Config; + +use strict; +use warnings; + +use Exporter qw/import/; +use Data::Dumper; +use FindBin qw($Bin); + +use TOML; + +our @EXPORT=qw/$cfg/; + +our $cfg = do { + my $toml = do {open(my $fh, "<", "$Bin/app.cfg"); local $/; <$fh>}; +# With error checking + my ($data, $err) = from_toml($toml); + unless ($data) { + die "Error parsing toml: $err"; + } + $data; +}; + +sub get_config { + my $key = shift; + + return $cfg->{$key}; +} + +1; diff --git a/lib/App/Controller/Paste.pm b/lib/App/Controller/Paste.pm index eed7dfe..64743cf 100644 --- a/lib/App/Controller/Paste.pm +++ b/lib/App/Controller/Paste.pm @@ -3,6 +3,7 @@ package App::Controller::Paste; use strict; use warnings; +use App::Config; use Mojo::Base 'Mojolicious::Controller'; use Mojo::IOLoop; use Mojo::Promise; @@ -39,7 +40,7 @@ sub to_root { sub root { my $c = shift; $c->stash({languages => $c->languages->get_languages}); - $c->stash({pastedata => q{}, channels => $c->app->config('announce')->{channels}, page_tmpl => 'editor.html'}); + $c->stash({pastedata => q{}, channels => $cfg->{announce}{channels}, page_tmpl => 'editor.html'}); $c->render("page"); }; @@ -52,7 +53,7 @@ sub edit_paste { if ($row->{when}) { $c->stash({languages => $c->languages->get_languages}); - $c->stash({pastedata => $row->{paste}, channels =>$c->app->config('announce')->{channels}}); + $c->stash({pastedata => $row->{paste}, channels =>$cfg->{announce}{channels}}); $c->stash({page_tmpl => 'editor.html'}); $c->render(template => 'page'); diff --git a/lib/App/Model/Paste.pm b/lib/App/Model/Paste.pm index e599d95..02cd5b6 100644 --- a/lib/App/Model/Paste.pm +++ b/lib/App/Model/Paste.pm @@ -4,6 +4,7 @@ use strict; use warnings; use DBI; +use App::Config; use Mojo::Base '-base'; use DateTime; use Mojo::Pg; @@ -11,6 +12,7 @@ use Regexp::Assemble; # TODO config for dbname # has 'dbh' => sub {DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", {RaiseError => 1, sqlite_unicode => 1})}; +my $cfg = App::Config::get_config('database'); has 'pg' => sub { Mojo::Pg->new($cfg->{dsc}); diff --git a/lib/App/Plugins/TomlConfig.pm b/lib/App/Plugins/TomlConfig.pm deleted file mode 100644 index 17f2a1e..0000000 --- a/lib/App/Plugins/TomlConfig.pm +++ /dev/null @@ -1,46 +0,0 @@ -package App::Plugins::TomlConfig; -use Mojo::Base 'Mojolicious::Plugin::Config'; - -use v5.24; -use strict; -use warnings; - -use Data::Dumper; - -use TOML; -use Hash::Merge; -use Syntax::Keyword::Try; -use Path::Tiny; - -sub parse { - my ($self, $content, $file, $conf, $app) = @_; - - my $merged_config; - - try { - my $env_file = path($file)->child($env.".cfg"); - my $base_file = path($file)->child('base.cfg'); - - my $base_config_data = $base_file->slurp_utf8(); - my $env_config_data = $env_file->slurp_utf8(); - - my ($base_config, $base_error) = from_toml($base_config_data); - die "$base_file: $base_error" if $base_error; - - my $env_config = from_toml($env_config_data); - die "$env_file: $env_error" if $env_error; - - $merged_config = merge($base_config, $env_config); - } catch($e) { - die "Unable to process config file: $e"; - } - - die Dumper($merged_config); - return $merged_config; -} - -# TODO figure out what i want here -sub register { shift->SUPER::register(shift, {%{shift()}}) } - - -1; From 075f7a9136ab3ca212f5cd4eb0c17d27989c1f0e Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Mon, 26 Apr 2021 15:46:34 -0400 Subject: [PATCH 3/3] make environment configs --- cpanfile | 1 + app.cfg => etc/base.cfg | 1 - etc/development.cfg | 2 ++ etc/production.cfg | 2 ++ lib/App/Config.pm | 36 +++++++++++++++++++++------ lib/App/Plugins/TomlConfig.pm | 46 +++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) rename app.cfg => etc/base.cfg (89%) create mode 100644 etc/development.cfg create mode 100644 etc/production.cfg create mode 100644 lib/App/Plugins/TomlConfig.pm diff --git a/cpanfile b/cpanfile index 09068cc..025e7ac 100644 --- a/cpanfile +++ b/cpanfile @@ -13,3 +13,4 @@ requires 'Mojolicious::Plugin::RemoteAddr'; requires 'App::EvalServerAdvanced::Protocol'; requires 'Future::Mojo'; requires 'Regexp::Assemble'; +requires 'Syntax::Keyword::Try'; diff --git a/app.cfg b/etc/base.cfg similarity index 89% rename from app.cfg rename to etc/base.cfg index 8563894..8671ba6 100644 --- a/app.cfg +++ b/etc/base.cfg @@ -4,7 +4,6 @@ evalserver=true blogspam=false [database] -dsc="postgresql://perlbot_pastebin:wrorkEvopCagyadMoighinIgiloinnAl:drepHodNorchoibTessiraypGacWobjoolbyewd9OsofogerObhypBeurvackidnipBifreTwusGikghiavratuckTujtie@localhost/perlbot_pastes_dev" [mojolicious.hypnotoad] listen=["http://localhost:3001"] diff --git a/etc/development.cfg b/etc/development.cfg new file mode 100644 index 0000000..37286e9 --- /dev/null +++ b/etc/development.cfg @@ -0,0 +1,2 @@ +[database] +dsc="postgresql://perlbot_pastebin:wrorkEvopCagyadMoighinIgiloinnAl:drepHodNorchoibTessiraypGacWobjoolbyewd9OsofogerObhypBeurvackidnipBifreTwusGikghiavratuckTujtie@localhost/perlbot_pastes_dev" diff --git a/etc/production.cfg b/etc/production.cfg new file mode 100644 index 0000000..29fa5bf --- /dev/null +++ b/etc/production.cfg @@ -0,0 +1,2 @@ +[database] +dsc="postgresql://perlbot_pastebin:wrorkEvopCagyadMoighinIgiloinnAl:drepHodNorchoibTessiraypGacWobjoolbyewd9OsofogerObhypBeurvackidnipBifreTwusGikghiavratuckTujtie@localhost/perlbot_pastes" diff --git a/lib/App/Config.pm b/lib/App/Config.pm index 86be0f9..4ca1dcf 100644 --- a/lib/App/Config.pm +++ b/lib/App/Config.pm @@ -1,5 +1,6 @@ package App::Config; +use v5.24; use strict; use warnings; @@ -8,17 +9,38 @@ use Data::Dumper; use FindBin qw($Bin); use TOML; +use Hash::Merge qw/merge/; +use Syntax::Keyword::Try; +use Path::Tiny; our @EXPORT=qw/$cfg/; +my $cfg_dir = path($Bin)->child('etc'); + +our $env = $ENV{MOJO_MODE} // $ENV{PLACK_ENV} // "development"; + our $cfg = do { - my $toml = do {open(my $fh, "<", "$Bin/app.cfg"); local $/; <$fh>}; -# With error checking - my ($data, $err) = from_toml($toml); - unless ($data) { - die "Error parsing toml: $err"; - } - $data; + my $merged_config; + + try { + my $env_file = path($cfg_dir)->child($env.".cfg"); + my $base_file = path($cfg_dir)->child('base.cfg'); + + my $base_config_data = $base_file->slurp_utf8(); + my $env_config_data = $env_file->slurp_utf8(); + + my ($base_config, $base_error) = from_toml($base_config_data); + die "$base_file: $base_error" if $base_error; + + my ($env_config, $env_error) = from_toml($env_config_data); + die "$env_file: $env_error" if $env_error; + + $merged_config = merge($base_config, $env_config); + } catch($e) { + die "Unable to process config file: $e"; + } + + $merged_config; }; sub get_config { diff --git a/lib/App/Plugins/TomlConfig.pm b/lib/App/Plugins/TomlConfig.pm new file mode 100644 index 0000000..17f2a1e --- /dev/null +++ b/lib/App/Plugins/TomlConfig.pm @@ -0,0 +1,46 @@ +package App::Plugins::TomlConfig; +use Mojo::Base 'Mojolicious::Plugin::Config'; + +use v5.24; +use strict; +use warnings; + +use Data::Dumper; + +use TOML; +use Hash::Merge; +use Syntax::Keyword::Try; +use Path::Tiny; + +sub parse { + my ($self, $content, $file, $conf, $app) = @_; + + my $merged_config; + + try { + my $env_file = path($file)->child($env.".cfg"); + my $base_file = path($file)->child('base.cfg'); + + my $base_config_data = $base_file->slurp_utf8(); + my $env_config_data = $env_file->slurp_utf8(); + + my ($base_config, $base_error) = from_toml($base_config_data); + die "$base_file: $base_error" if $base_error; + + my $env_config = from_toml($env_config_data); + die "$env_file: $env_error" if $env_error; + + $merged_config = merge($base_config, $env_config); + } catch($e) { + die "Unable to process config file: $e"; + } + + die Dumper($merged_config); + return $merged_config; +} + +# TODO figure out what i want here +sub register { shift->SUPER::register(shift, {%{shift()}}) } + + +1;