From a165836816249cfb0d618489e85554a8d748f7e7 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Mon, 26 Apr 2021 15:35:38 -0400 Subject: [PATCH] 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;