Leyland::Manual::StaticFiles - How to serve static files from your application
Pretty much every web application has static files, i.e. files which are not generated dynamically. These are mostly CSS, JavaScript, images, videos, etc.
How to serve static files from your application depends on the way you run the application and your personal preferences. During development and testing, when you're most likely to run your application by simply using plackup, and the easiest way to serve static files would be to let the Static middleware provided with Plack do it. Simply add the following to the builder section of app.psgi (or create that section if it doesn't already exist):
plackup
builder
app.psgi
use Plack::Builder; ... rest of app.psgi ... my $app = MyLeylandApp->new->to_app; ... rest of app.psgi ... builder { enable 'Static', path => qr{^/((images|js|css)/|favicon\.ico$|apple-touch-icon\.png$)}, root => './public/'; $app; };
In builder, we enable Plack::Middleware::Static, and tell it to serve any requests to paths that start with "images", "js" or "css" (plus the files "favicon.ico" and "apple-touch-icon.png") from the "public/" directory of your application.
There are other Plack middlewares for serving static files. Some are more suitable for production/deployment environments, such as Plack::App::MCCS.
When deploying your application, it is likely that you will run the application behind a frontend webserver like Apache, nginx, lighttpd or Cherokee. If so, you might find it better to let them serve the static files of your application. I will not go into detail how to perform this, as every web server is configured differently. If you are doing this, then you don't need the static middleware.
A good option when doing this, however, is enabling the Static middleware only when running on the "development" environment (or if not running on the "deployment" environment), like so:
builder { enable_if { $ENV{PLACK_ENV} eq 'development' } 'Static', path => qr{^/((images|js|css)/|favicon\.ico$|apple-touch-icon\.png$)}, root => './public/'; }
And that's pretty much it.
Read Leyland::Manual::Localization to learn how to localize your applications or return to the table of contents.
Ido Perlmuter, <ido at ido50.net>
<ido at ido50.net>
Please report any bugs or feature requests to bug-Leyland at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Leyland. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-Leyland at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Leyland::Manual::StaticFiles
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Leyland
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Leyland
CPAN Ratings
http://cpanratings.perl.org/d/Leyland
Search CPAN
http://search.cpan.org/dist/Leyland/
Copyright 2010-2014 Ido Perlmuter.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Leyland, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Leyland
CPAN shell
perl -MCPAN -e shell install Leyland
For more information on module installation, please visit the detailed CPAN module installation guide.