summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-04-13 12:17:19 +0200
committerDaniel Friesel <derf@finalrewind.org>2019-04-13 12:18:26 +0200
commit80a6317ac55de020f606a5ca114466d3f0100511 (patch)
tree9bd5104c3f07cda2f07f3a783e71e1e5b948f943 /lib
parent61b1ef398e275a110173ed77556ff211adc1ba82 (diff)
Use travelynx.conf for configuration and secrets
This avoids having to specify secrets in the environment, where they can leak easily.
Diffstat (limited to 'lib')
-rwxr-xr-xlib/Travelynx.pm72
-rw-r--r--lib/Travelynx/Helper/Sendmail.pm7
2 files changed, 42 insertions, 37 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 1965da4..02f5f1b 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -17,18 +17,6 @@ use Travelynx::Helper::Sendmail;
our $VERSION = qx{git describe --dirty} || 'experimental';
-my $cache_iris_main = Cache::File->new(
- cache_root => $ENV{TRAVELYNX_IRIS_CACHE} // '/tmp/dbf-iris-main',
- default_expires => '6 hours',
- lock_level => Cache::File::LOCK_LOCAL(),
-);
-
-my $cache_iris_rt = Cache::File->new(
- cache_root => $ENV{TRAVELYNX_IRISRT_CACHE} // '/tmp/dbf-iris-realtime',
- default_expires => '70 seconds',
- lock_level => Cache::File::LOCK_LOCAL(),
-);
-
sub check_password {
my ( $password, $hash ) = @_;
@@ -66,27 +54,18 @@ sub get_station {
sub startup {
my ($self) = @_;
- if ( $ENV{TRAVELYNX_SECRETS} ) {
- $self->secrets( [ split( qr{:}, $ENV{TRAVELYNX_SECRETS} ) ] );
- }
-
push( @{ $self->commands->namespaces }, 'Travelynx::Command' );
$self->defaults( layout => 'default' );
- $self->config(
- hypnotoad => {
- accepts => $ENV{TRAVELYNX_ACCEPTS} // 100,
- clients => $ENV{TRAVELYNX_CLIENS} // 10,
- listen => [ $ENV{TRAVELYNX_LISTEN} // 'http://*:8093' ],
- pid_file => $ENV{TRAVELYNX_PID_FILE} // '/tmp/travelynx.pid',
- workers => $ENV{TRAVELYNX_WORKERS} // 2,
- spare => $ENV{TRAVELYNX_SPARE} // 2,
- },
- );
-
$self->types->type( json => 'application/json; charset=utf-8' );
+ $self->plugin('Config');
+
+ if ( $self->config->{secrets} ) {
+ $self->secrets( $self->config->{secrets} );
+ }
+
$self->plugin(
authentication => {
autoload_user => 1,
@@ -117,6 +96,30 @@ sub startup {
$self->defaults( layout => 'default' );
$self->attr(
+ cache_iris_main => sub {
+ my ($self) = @_;
+
+ return Cache::File->new(
+ cache_root => $self->app->config->{cache}->{schedule},
+ default_expires => '6 hours',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+ }
+ );
+
+ $self->attr(
+ cache_iris_rt => sub {
+ my ($self) = @_;
+
+ return Cache::File->new(
+ cache_root => $self->app->config->{cache}->{realtime},
+ default_expires => '70 seconds',
+ lock_level => Cache::File::LOCK_LOCAL(),
+ );
+ }
+ );
+
+ $self->attr(
action_type => sub {
return {
checkin => 1,
@@ -321,12 +324,13 @@ sub startup {
$self->attr(
dbh => sub {
my ($self) = @_;
+ my $config = $self->app->config;
- my $dbname = $ENV{TRAVELYNX_DB_NAME} // 'travelynx_dev';
- my $host = $ENV{TRAVELYNX_DB_HOST} // 'localhost';
- my $port = $ENV{TRAVELYNX_DB_PORT} // '5432';
- my $user = $ENV{TRAVELYNX_DB_USER};
- my $pw = $ENV{TRAVELYNX_DB_PASSWORD};
+ my $dbname = $config->{db}->{database};
+ my $host = $config->{db}->{host} // 'localhost';
+ my $port = $config->{db}->{port} // 5432;
+ my $user = $config->{db}->{user};
+ my $pw = $config->{db}->{password};
return DBI->connect(
"dbi:Pg:dbname=${dbname};host=${host};port=${port}",
@@ -593,8 +597,8 @@ qq{select * from pending_mails where email = ? and num_tries > 1;}
$station = $station_matches[0][0];
my $status = Travel::Status::DE::IRIS->new(
station => $station,
- main_cache => $cache_iris_main,
- realtime_cache => $cache_iris_rt,
+ main_cache => $self->app->cache_iris_main,
+ realtime_cache => $self->app->cache_iris_rt,
lookbehind => 20,
datetime => DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $lookbehind ),
diff --git a/lib/Travelynx/Helper/Sendmail.pm b/lib/Travelynx/Helper/Sendmail.pm
index 6193884..09c8a0d 100644
--- a/lib/Travelynx/Helper/Sendmail.pm
+++ b/lib/Travelynx/Helper/Sendmail.pm
@@ -12,11 +12,11 @@ use Email::Simple;
sub new {
my ($class) = @_;
- return bless({}, $class);
+ return bless( {}, $class );
}
sub custom {
- my ($self, $to, $subject, $body) = @_;
+ my ( $self, $to, $subject, $body ) = @_;
my $reg_mail = Email::Simple->create(
header => [
@@ -28,7 +28,8 @@ sub custom {
body => encode( 'utf-8', $body ),
);
- if ($ENV{TRAVELYNX_DB_NAME} =~ m{travelynx_dev}) {
+ if ( $self->app->config->{db}->{database} =~ m{travelynx_dev} ) {
+
# Do not send mail in dev mode
say "sendmail to ${to}: ${subject}\n\n${body}";
return 1;