Skip to content

Instantly share code, notes, and snippets.

@bduggan
Created March 20, 2013 19:25
Show Gist options
  • Save bduggan/5207651 to your computer and use it in GitHub Desktop.
Save bduggan/5207651 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
app->hook(after_dispatch => sub {
my $c = shift;
$c->res->headers->header('Access-Control-Allow-Origin' => '*');
} );
my @refs = map
+{
doi => "doi_$_",
authors => [ "author:$_", "author:".($_+1) ],
journal => "The great journal with doi $_",
pages => (int rand 100)." to ".((int rand 100) + 100)
}, 0..1000;
my @authors = map
+{ name => "name of author $_",
id => "author:$_"
}, 0..1000;
get '/references' => sub {
shift->render_json(\@refs);
};
get '/reference/:doi' => sub {
my $c = shift;
my $doi = $c->stash('doi');
$doi =~ s/doi_//;
$c->render_json($refs[$doi]);
} => "reference";
get '/author/:authorid' => sub {
my $c = shift;
my $author = $c->stash('authorid');
$author =~ s/author://;
$c->stash(author => $authors[$author]);
$c->respond_to(
json => sub { shift->render_json($authors[$author]) },
html => { template => 'author' },
any => { template => 'author' },
);
} => "author";
get '/' => 'index';
app->start;
__DATA__
@@ index.html.ep
<html>
<head>
%= javascript '/mojo/jquery/jquery.js';
%= javascript 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js';
%= stylesheet "http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css";
</head>
<body>
<pre>
Sample:
<span id='foo'></span>
</pre>
<script type='text/template' id='ref_template'>
journal : <b><%%= journal %><b>
</script>
%= javascript begin
$(document).ready(function() {
$("#foo").html(
_.template(
$('#ref_template').html(),{ journal : "foo" }
)
);
});
%= end
% my $walk = begin
% my ($walk, $node, $depth) = @_;
<tr>
<td>
% my $pattern = $node->pattern->pattern || '/';
% $pattern = "+$pattern" if $depth;
<%= ' ' x $depth %><%= $pattern %>
</td>
<td>
<%= uc(join ',', @{$node->via || []}) || '*' %>
</td>
<td>
% my $name = $node->name;
<%= $node->has_custom_name ? qq{"$name"} : $name %>
</td>
</tr>
% $depth++;
%= $walk->($walk, $_, $depth) for @{$node->children};
% $depth--;
% end
<table class='table table-condensed table-bordered table-striped'>
<caption>GCIS API</caption>
<tr>
<th>Route</th>
<th>Methods</th>
<th>Name</th>
</tr>
%= $walk->($walk, $_, 0) for @{app->routes->children};
</table>
</body>
</html>
@@ exception.html.ep
%== $exception
@@ author.html.ep
This is a really cool author page for author <%= stash 'authorid' %>.
<pre>
My name is <b><%= $author->{name} %></b>.
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment