Skip to content

Instantly share code, notes, and snippets.

@Altreus
Created February 7, 2014 23:39
Show Gist options
  • Save Altreus/8874231 to your computer and use it in GitHub Desktop.
Save Altreus/8874231 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Web::Machine;
use Plack::App::Path::Router;
use Path::Router;
my $router = Path::Router->new();
$router->add_route(
'/hack/?:id/?:subresource' =>
defaults => {
id => undef,
subresource => undef,
},
target => sub {
my ($req, $id, $subresource) = @_;
Web::Machine->new(
resource => 'Hackmachine::Resource',
resource_args => [
id => $id,
subresource => $subresource,
]
)->call($req->env);
},
);
Plack::App::Path::Router->new( router => $router )->to_app;
package Hackmachine::Resource;
use strict;
use warnings;
use Data::Dumper;
use parent 'Web::Machine::Resource';
our @data = qw( hello hi hey howdy );
sub content_types_provided { [{ 'text/html' => 'to_html' }] }
sub init {
my ($self, $args) = @_;
@{$self}{qw(id subresource)} = @{$args}{qw(id subresource)};
$self->SUPER::init();
}
sub to_html {
my $self = shift;
q{<h1>} . $data[$self->{id}//0] . q{ world</h1>}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment