Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ap/plack-app-hash
Serve up the contents of a hash as a website
https://github.com/ap/plack-app-hash
perl plack psgi
Last synced: 10 days ago
JSON representation
Serve up the contents of a hash as a website
- Host: GitHub
- URL: https://github.com/ap/plack-app-hash
- Owner: ap
- Created: 2012-09-13T05:12:15.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2022-09-04T15:59:02.000Z (over 2 years ago)
- Last Synced: 2024-11-06T03:03:57.912Z (about 2 months ago)
- Topics: perl, plack, psgi
- Language: Perl
- Homepage: https://metacpan.org/release/Plack-App-Hash
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
Awesome Lists containing this project
README
use 5.006; use strict; use warnings;
package Plack::App::Hash;
our $VERSION = '1.001';
BEGIN { require Plack::Component; our @ISA = 'Plack::Component' }
use Plack::Util ();
use Array::RefElem ();
use HTTP::Status ();
#use Digest::SHA;use Plack::Util::Accessor qw( content headers auto_type default_type );
sub prepare_app {
my $self = shift;
require Plack::MIME if $self->auto_type;
}sub call {
my $self = shift;
my $env = shift;my $path = $env->{'PATH_INFO'};
$path =~ s!\A/!!;my $content = $self->content;
return $self->error( 404 ) unless $content and exists $content->{ $path };
return $self->error( 500 ) if ref $content->{ $path };my $headers = ( $self->headers || $self->headers( {} ) )->{ $path };
if ( not defined $headers ) {
$headers = [];
}
elsif ( not ref $headers ) {
require JSON::MaybeXS;
$headers = JSON::MaybeXS::decode_json $headers;
}return $self->error( 500 ) if 'ARRAY' ne ref $headers;
{
my $auto = $self->auto_type;
my $default = $self->default_type;
last unless $auto or $default;
last if Plack::Util::header_exists $headers, 'Content-Type';
$auto &&= Plack::MIME->mime_type( $path );
push @$headers, 'Content-Type' => $_ for $auto || $default || ();
}if ( not Plack::Util::header_exists $headers, 'Content-Length' ) {
push @$headers, 'Content-Length' => length $content;
}my @body;
Array::RefElem::av_push @body, $content->{ $path };
return [ 200, $headers, \@body ];
}sub error {
my $status = pop;
my $pkg = __PACKAGE__;
my $body = [ qq(\n$pkg $status) . HTTP::Status::status_message $status ];
return [ $status, [
'Content-Type' => 'text/html',
'Content-Length' => length $body->[0],
], $body ];
}1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Plack::App::Hash - Serve up the contents of a hash as a website
=head1 SYNOPSIS
use Plack::App::Hash;
my $app = Plack::App::Hash->new(
content => { '' => 'Hello World!' },
default_type => 'text/plain',
)->to_app;=head1 DESCRIPTION
This PSGI application responds to HTTP requests by looking up the request path
in a hash and returning the value of that key (if found) as the response body.This is useful for cases like inlining the content of a boilerplate static site
into a single-page-application-in-a-module, or serving up a tied DBM hash that
other programs can update while the web app itself contains very little logic
E in short, for one-off hacks and scaling down.=head1 CONFIGURATION
=over 4
=item C
The content of your site. Each key-value pair will be one resource in your URI
space. The key is its URI path without leading slash, the value is the content
of the resource. Values must not be references.=item C (optional)
The headers of your resources. As in C, each key is a URI path without
leading slash. The value of a key may be either an array reference or a string
containing a JSON encoding of an array. In either case it is taken to mean the
L header array for the resource.=item C (optional)
If true, a C header value will be computed automatically for any
responses which do not already have one by way of the C hash.=item C (optional)
The C value to use for any responses which would not otherwise
have one, whether by matching C or by C fallback.=back
=cut