From cc39f6fb596c15773a68ac049389f2ccfb430387 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Thu, 4 Jun 2020 17:36:32 +0200
Subject: [PATCH] Presub konfigurace do pi_tube.conf

---
 lib/PiTube.pm                | 26 ++++++++------------------
 lib/PiTube/Helpers/OAuth2.pm | 14 +++++---------
 pi_tube.conf                 | 29 ++++++++++++++++++++++++++++-
 templates/stream/403.html.ep |  2 +-
 4 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/lib/PiTube.pm b/lib/PiTube.pm
index e25230e..1af164f 100644
--- a/lib/PiTube.pm
+++ b/lib/PiTube.pm
@@ -12,36 +12,26 @@ sub startup {
     my $self = shift;
 
     # Nacteni konfigurace
-    my $config = $self->plugin('Config');
+    my $cfg = $self->plugin('Config');
+    $self->helper( cfg => sub { return $cfg; } );
 
-    $self->secrets([$ENV{SECRET}]);
+    $self->secrets($cfg->{session}{secrets});
 
     $self->plugin('PiTube::Helpers::OAuth2');
 
     my $home = Mojo::Home->new()->detect;
 
     my $pg = Mojo::Pg->new
-        ->dsn($ENV{DB_DSN})
-        ->username($ENV{DB_USERNAME})
-        ->password($ENV{DB_PASSWORD})
+        ->dsn($cfg->{database}{dsn})
+        ->username($cfg->{database}{user})
+        ->password($cfg->{database}{password})
     ;
-
     $pg->migrations->from_file("$home/sql/migrations.sql");
     $pg->migrations->migrate();
 
     # Spojeni s databazi
-    my $schema = PiTube::Schema->connect( {
-        dsn            => $ENV{DB_DSN},
-        user           => $ENV{DB_USERNAME},
-        password       => $ENV{DB_PASSWORD},
-        AutoCommit     => 1,
-        quote_char     => '"',
-        name_sep       => '.',
-        pg_enable_utf8 => 1,
-        on_connect_do  => [
-            "set timezone to 'Europe/Prague'",
-        ],
-    });
+    my $schema = PiTube::Schema->connect($cfg->{database});
+
     $self->helper( schema => sub { return $schema; } );
 
     $self->plugin('authentication', {
diff --git a/lib/PiTube/Helpers/OAuth2.pm b/lib/PiTube/Helpers/OAuth2.pm
index 130e5da..d895f8f 100644
--- a/lib/PiTube/Helpers/OAuth2.pm
+++ b/lib/PiTube/Helpers/OAuth2.pm
@@ -18,18 +18,18 @@ sub register {
 
     # get public key
 
-    my $tx  = $ua->get( $ENV{OAUTH_REALM_URL} );
+    my $tx  = $ua->get( $self->cfg->{oauth2}{realm_url} );
     my $res = $tx->result;
 
     if ($res->is_success) {
         $jwt = Mojo::JWT->new(
-            public => sprintf( KEY_FORMAT,  $res->json->{public_key})
+            public => sprintf( KEY_FORMAT,  $res->json->{public_key} )
         );
     }
 
-    # get urls
+    # get endpoints
 
-    $tx  = $ua->get( $ENV{OAUTH_REALM_URL} . '/.well-known/openid-configuration');
+    $tx  = $ua->get( $self->cfg->{oauth2}{realm_well_known} );
     $res = $tx->result;
 
     if ($res->is_success) {
@@ -37,13 +37,9 @@ sub register {
     }
 
     my $oauth2 = Net::OAuth2::Profile::WebServer->new(
-       name              => 'SSO',
-       scope             => 'profile',
-       client_id         => $ENV{OAUTH_CLIENT_ID},
-       client_secret     => $ENV{OAUTH_CLIENT_SECRET},
+       %{ $self->cfg->{oauth2}},
        authorize_url     => $discovered->{authorization_endpoint},
        access_token_url  => $discovered->{token_endpoint},
-       redirect_uri      => $ENV{BASE_URL} . '/oauth2',
     );
     $self->helper( oauth2 => sub { return $oauth2; } );
 
diff --git a/pi_tube.conf b/pi_tube.conf
index 6e8fc86..e907275 100644
--- a/pi_tube.conf
+++ b/pi_tube.conf
@@ -3,6 +3,33 @@
     workers  => 10,
     proxy    => 1,
     pid_file => '/var/tmp/hypnotoad.pid',
-    listen   => ['http://*:3000']
+    listen   => ['http://*:3000'],
+  },
+  session => {
+    secrets => [$ENV{SECRET}],
+  },
+  database => {
+    dsn            => $ENV{DB_DSN},
+    user           => $ENV{DB_USERNAME},
+    password       => $ENV{DB_PASSWORD},
+    AutoCommit     => 1,
+    quote_char     => '"',
+    name_sep       => '.',
+    pg_enable_utf8 => 1,
+    on_connect_do  => [
+        "set timezone to 'Europe/Prague'",
+    ],
+  },
+  oauth2 => {
+    name              => 'SSO',
+    scope             => 'profile',
+    client_id         => $ENV{OAUTH_CLIENT_ID},
+    client_secret     => $ENV{OAUTH_CLIENT_SECRET},
+    redirect_uri      => $ENV{BASE_URL} . '/oauth2',
+    realm_url         => $ENV{OAUTH_REALM_URL},
+    realm_well_known  => $ENV{OAUTH_REALM_URL} . '/.well-known/openid-configuration',
+  },
+  rtmp => {
+    base_url => $ENV{RTMP_BASE_URL},
   }
 };
diff --git a/templates/stream/403.html.ep b/templates/stream/403.html.ep
index 4fb0ee3..69c6705 100644
--- a/templates/stream/403.html.ep
+++ b/templates/stream/403.html.ep
@@ -5,6 +5,6 @@ Nemáte oprávnění ke sledování streamu <strong>"<%= $c->stash->{stream}->na
 </p>
 % if ( $c->is('publisher')) {
 <p>
-stream rtp url:  <%= $ENV{RTMP_BASE_URL} %>/<%= $c->stash->{stream}->key %>?token=<%= $c->current_user->token %>
+stream rtp url:  <%= $c->config->{rtmp}{base_url} %>/<%= $c->stash->{stream}->key %>?token=<%= $c->current_user->token %>
 </p>
 % }
-- 
GitLab