From: Dan Brook Date: Sat, 7 May 2011 17:02:57 +0000 (+0100) Subject: Expose serialization via JSON. X-Git-Tag: 0.003002~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FGitalist.git;a=commitdiff_plain;h=f41fc74106e51d8055871cacdc1459be4f61f980;hp=1aae440e61b68d1f703a63ead38411f5bb428062 Expose serialization via JSON. The last of Tom's work is now integrated with all URLs that can will respond with JSON. Unfortuantely browsers (I'm looking at you Chrome) will cache per URL rather than URL + Content-Type so if one requests JSON for /repo/sha1/commit then navigates to said commit the browser will served the cached JSON rather than HTML. As such JSON is also exposed with the .json extension allowing JS to talk JSON without messing up certain browsers' (i.e Chrome's) caching. --- diff --git a/Makefile.PL b/Makefile.PL index 6a18510..efb6e76 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -64,6 +64,7 @@ requires 'Catalyst::Plugin::Static::Simple'; requires 'Catalyst::Plugin::Unicode::Encoding'; requires 'Catalyst::Plugin::SubRequest' => '0.15'; requires 'Catalyst::Action::RenderView'; +requires 'Catalyst::Action::REST'; requires 'Catalyst::Component::InstancePerContext'; requires 'Catalyst::Controller::ActionRole'; requires 'Catalyst::View::Component::SubInclude' => '0.07'; diff --git a/lib/Gitalist.pm b/lib/Gitalist.pm index a2bf1a5..bd2f533 100644 --- a/lib/Gitalist.pm +++ b/lib/Gitalist.pm @@ -29,8 +29,13 @@ __PACKAGE__->setup(); after prepare_path => sub { my ($ctx) = @_; + my $path = $ctx->req->uri->path; if ($ctx->req->param('a')) { - $ctx->request->uri->path('/legacy' . $ctx->request->uri->path); + $ctx->req->uri->path("/legacy$path"); + } + + if($path =~ s/[.]json$// && $ctx->req->content_type eq 'application/json') { + $ctx->req->uri->path($path); } }; diff --git a/lib/Gitalist/Controller/Root.pm b/lib/Gitalist/Controller/Root.pm index d598c1f..ddcc83a 100644 --- a/lib/Gitalist/Controller/Root.pm +++ b/lib/Gitalist/Controller/Root.pm @@ -68,7 +68,16 @@ Provides some help for the search form. sub search_help : Chained('base') Args(0) {} -sub end : ActionClass('RenderView') {} +sub end : ActionClass('Serialize') { + my ($self, $c) = @_; + # Give repository views the current HEAD. + if ($c->stash->{Repository}) { + $c->stash->{HEAD} = $c->stash->{Repository}->head_hash; + } + if ($c->stash->{data} && blessed $c->stash->{data}) { + $c->stash->{rest} = $c->stash->{data}->pack; + } +} sub error_404 : Action { my ($self, $c) = @_; @@ -76,6 +85,16 @@ sub error_404 : Action { $c->response->body('Page not found'); } +__PACKAGE__->config( + default => 'text/html', + map => { + 'application/json' => [qw/ JSON /], + map { $_ => [qw/ View Default /] } + qw( text/css text/html text/plain + application/atom+xml application/rss+xml application/rss ) + } +); + __PACKAGE__->meta->make_immutable; __END__