Commit e50086bbd9ca379e3915dd9fd7031b6ef7db2981

Authored by Rodrigo Siqueira de Melo
1 parent f3c7a21c

API first draft

Signed-off-by: Athos Ribeiro <athoscribeiro@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Showing 1 changed file with 35 additions and 1 deletions   Show diff stats
spb_copr_status.psgi
@@ -5,6 +5,7 @@ use JSON; @@ -5,6 +5,7 @@ use JSON;
5 use Text::Template; 5 use Text::Template;
6 use LWP::UserAgent; 6 use LWP::UserAgent;
7 use Plack::Builder; 7 use Plack::Builder;
  8 +use Plack::Request;
8 9
9 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; 10 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
10 11
@@ -13,7 +14,7 @@ sub copr_info { @@ -13,7 +14,7 @@ sub copr_info {
13 $ua->timeout(10); 14 $ua->timeout(10);
14 $ua->env_proxy; 15 $ua->env_proxy;
15 $ua->ssl_opts(SSL_verify_mode => 0x00); 16 $ua->ssl_opts(SSL_verify_mode => 0x00);
16 - 17 +
17 my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/"); 18 my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
18 my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/"); 19 my $result_v5 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v5/monitor/");
19 20
@@ -91,14 +92,36 @@ sub build_html { @@ -91,14 +92,36 @@ sub build_html {
91 title => "SPB Copr Stats", 92 title => "SPB Copr Stats",
92 table_entries => info2html() 93 table_entries => info2html()
93 }; 94 };
  95 +
94 my $template = Text::Template->new( 96 my $template = Text::Template->new(
95 TYPE => 'FILE', 97 TYPE => 'FILE',
96 SOURCE => 'template.html.tt' 98 SOURCE => 'template.html.tt'
97 ); 99 );
  100 +
98 return $template->fill_in(HASH => $data); 101 return $template->fill_in(HASH => $data);
99 } 102 }
100 103
  104 +my %ROUTING = (
  105 + '/' => \&serve_html,
  106 + '/api' => \&serve_json
  107 + );
  108 +
101 my $app = sub { 109 my $app = sub {
  110 + my $env = shift;
  111 +
  112 + my $request = Plack::Request->new($env);
  113 + my $route = $ROUTING{$request->path_info};
  114 + if ($route) {
  115 + return $route->($env);
  116 + }
  117 + return [
  118 + '404',
  119 + [ 'Content-Type' => 'text/html' ],
  120 + [ '404 Not Found' ],
  121 + ];
  122 +};
  123 +
  124 +sub serve_html {
102 return [ 125 return [
103 '200', 126 '200',
104 [ 'Content-Type' => 'text/html'], 127 [ 'Content-Type' => 'text/html'],
@@ -106,6 +129,17 @@ my $app = sub { @@ -106,6 +129,17 @@ my $app = sub {
106 ]; 129 ];
107 }; 130 };
108 131
  132 +sub serve_json {
  133 + my $info = copr_info();
  134 + my $json = JSON->new->allow_nonref;
  135 + my $json_info = $json->encode($info);
  136 + return [
  137 + '200',
  138 + [ 'Content-Type' => 'application/json'],
  139 + [ $json_info ],
  140 + ];
  141 +};
  142 +
109 builder { 143 builder {
110 enable "Static", path => qr!^(/css|/js)!, pass_through => 1; 144 enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
111 $app; 145 $app;