Commit 2fbbd6dd2363f707cea47063e757ff9d7e24014b

Authored by Athos
2 parents f3c7a21c e50086bb

Merge pull request #1 from rodrigosiqueira/master

API first draft
Showing 1 changed file with 35 additions and 1 deletions   Show diff stats
spb_copr_status.psgi
... ... @@ -5,6 +5,7 @@ use JSON;
5 5 use Text::Template;
6 6 use LWP::UserAgent;
7 7 use Plack::Builder;
  8 +use Plack::Request;
8 9  
9 10 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
10 11  
... ... @@ -13,7 +14,7 @@ sub copr_info {
13 14 $ua->timeout(10);
14 15 $ua->env_proxy;
15 16 $ua->ssl_opts(SSL_verify_mode => 0x00);
16   -
  17 +
17 18 my $result_v4 = $ua->get("http://copr.fedoraproject.org/api/coprs/softwarepublico/v4/monitor/");
18 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 92 title => "SPB Copr Stats",
92 93 table_entries => info2html()
93 94 };
  95 +
94 96 my $template = Text::Template->new(
95 97 TYPE => 'FILE',
96 98 SOURCE => 'template.html.tt'
97 99 );
  100 +
98 101 return $template->fill_in(HASH => $data);
99 102 }
100 103  
  104 +my %ROUTING = (
  105 + '/' => \&serve_html,
  106 + '/api' => \&serve_json
  107 + );
  108 +
101 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 125 return [
103 126 '200',
104 127 [ 'Content-Type' => 'text/html'],
... ... @@ -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 143 builder {
110 144 enable "Static", path => qr!^(/css|/js)!, pass_through => 1;
111 145 $app;
... ...