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