| 1 | package Catalyst::Controller::Public; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | extends 'Catalyst::Controller'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | our $VERSION = '0.004'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | has show_debugging => (is=>'ro', required=>1, default=>sub {0}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | has cache_control => (is=>'ro', isa=>'Str', predicate=>'has_cache_control'); | |||||||||||||
| 10 | 1 | has content_types => (is=>'ro', isa=>'ArrayRef[Str]', predicate=>'has_content_types'); | |||||||||||||
| 11 | 1 | has chain_base_action => (is=>'ro', isa=>'Str', predicate=>'has_chain_base_action'); | |||||||||||||
| 12 | 1 | has chain_pathpart => (is=>'ro', isa=>'Str', predicate=>'has_chain_pathpart'); | |||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | has at => ( | |||||||||||||
| 15 | is=>'ro', | ||||||||||||||
| 16 | isa=>'Str', | ||||||||||||||
| 17 | required=>1, | ||||||||||||||
| 18 | default=>'/:namespace/:args'); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | ▶ | after 'register_actions' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=19.2
| |||||||||||||||
| 21 | 1 | my ($self, $app) = @_; | |||||||||||||
| 22 | 1 | ▶ | my %base_path_attributes = $self->has_chain_base_action ? | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 23 | ( | ||||||||||||||
| 24 | Chained => [$self->chain_base_action], | ||||||||||||||
| 25 | PathPart => [$self->has_chain_pathpart ? $self->chain_pathpart : $self->action_namespace], | ||||||||||||||
| 26 | Args => [] | ||||||||||||||
| 27 | ) : (Path => [ $self->action_namespace ]); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | my $action = $self->create_action( | ||||||||||||||
| 30 | name => 'serve_file', | ||||||||||||||
| 31 | 1 | ▶ | code => sub { }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 1 | ▶ | reverse => $self->action_namespace . '/' .'serve_file', | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 33 | namespace => $self->action_namespace, | ||||||||||||||
| 34 | class => ref($self), | ||||||||||||||
| 35 | attributes => { | ||||||||||||||
| 36 | %base_path_attributes, | ||||||||||||||
| 37 | Does => ['Catalyst::ActionRole::Public'], | ||||||||||||||
| 38 | At => [$self->at], | ||||||||||||||
| 39 | ShowDebugging => [$self->show_debugging], | ||||||||||||||
| 40 | ( $self->has_cache_control ? (CacheControl => [$self->cache_control]) : ()), | ||||||||||||||
| 41 | ( $self->has_content_types ? (ContentTypes => $self->content_types) | ||||||||||||||
| 42 | : ()), | ||||||||||||||
| 43 | }); | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | $app->dispatcher->register( $app, $action ); | |||||||||||||
| 46 | 1 | }; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 1 | ▶ | sub uri_args { | ||||||||||||
|
Subroutine
uri_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 49 | 1 | my $self = shift; | |||||||||||||
| 50 | 1 | return $self->action_for('serve_file'), @_; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 54 | |||||||||||||||
| 55 | =head1 NAME | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Catalyst::Controller::Public - mount a public url to files in your Catalyst project | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =head1 SYNOPSIS | ||||||||||||||
| 60 | |||||||||||||||
| 61 | package MyApp::Controller::Public; | ||||||||||||||
| 62 | |||||||||||||||
| 63 | use Moose; | ||||||||||||||
| 64 | extends 'Catalyst::Controller::Public'; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Will create an action that matches '$HOST/public/@args', for example like a URL | ||||||||||||||
| 69 | 'localhost/public/a/b/c/d.js', that will serve file $c->{root} . '/public' . '/a/b/c/d.js'. | ||||||||||||||
| 70 | |||||||||||||||
| 71 | Will also set content type, length and Last-Modified HTTP headers as needed. | ||||||||||||||
| 72 | If the file does not exist, will not match (allowing possibly other actions to match). | ||||||||||||||
| 73 | |||||||||||||||
| 74 | You can create a URL for a static file programmtically via the following: | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub myaction :Local { | ||||||||||||||
| 77 | my ($self, $c) = @_; | ||||||||||||||
| 78 | my $static_url = $c->uri_for(controller('Public')->uri_args('example.txt')); | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | If you are using Chaining then you will probably need to specify the 'root' chain | ||||||||||||||
| 82 | |||||||||||||||
| 83 | package MyApp::Controller::Public; | ||||||||||||||
| 84 | |||||||||||||||
| 85 | use Moose; | ||||||||||||||
| 86 | extends 'Catalyst::Controller::Public'; | ||||||||||||||
| 87 | |||||||||||||||
| 88 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 89 | __PACKAGE__->config(chain_base_action=>'/root'); | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||||||
| 92 | |||||||||||||||
| 93 | This is a simple controller that uses L<Catalyst::ActionRole::Public> | ||||||||||||||
| 94 | to create a single public folder for you webapplication, doing as much | ||||||||||||||
| 95 | of the right thing as possible. Useful for starting a new project, although | ||||||||||||||
| 96 | I imagine as time goes on you'll probably want something stronger. | ||||||||||||||
| 97 | |||||||||||||||
| 98 | This controller doesn't do anything like compile LESS to CSS, etc. If you | ||||||||||||||
| 99 | are looking for that you might find L<Catalyst::Controller::SimpleCAS> has | ||||||||||||||
| 100 | more power for what you wish. This is really aimed at helping people move | ||||||||||||||
| 101 | away from L<Catalyst::Plugin::Static::Simple> which I really don't want | ||||||||||||||
| 102 | to support anymore :) | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head1 METHODS | ||||||||||||||
| 105 | |||||||||||||||
| 106 | This controller defines the following methods | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 uri_args | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Used as a helper to correctly generate a URI. For example: | ||||||||||||||
| 111 | |||||||||||||||
| 112 | sub myaction :Local { | ||||||||||||||
| 113 | my ($self, $c) = @_; | ||||||||||||||
| 114 | my $static_url = $c->uri_for(controller('Public') | ||||||||||||||
| 115 | ->uri_args('example.txt')); | ||||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 ATTRIBUTES | ||||||||||||||
| 119 | |||||||||||||||
| 120 | This controller defines the following configuration attributes. They | ||||||||||||||
| 121 | are pretty much all just wrappers for the same configuration options for | ||||||||||||||
| 122 | the L<Catalyst::ActionRole::Public> | ||||||||||||||
| 123 | |||||||||||||||
| 124 | has chain_base_action => (is=>'ro', isa=>'Str', predicate=>'has_chain_base_action'); | ||||||||||||||
| 125 | has chain_pathpart => (is=>'ro', isa=>'Str', predicate=>'has_chain_pathpart'); | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =head2 chain_base_action | ||||||||||||||
| 128 | |||||||||||||||
| 129 | Should be the full private path of the root chain action. This is the value you'd | ||||||||||||||
| 130 | normally put into the subroutine attribute 'Chained'. If you are doing | ||||||||||||||
| 131 | the normal thing this is probably called '/root'. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | If you leave this undefined we assume you are not using chaining and we will create | ||||||||||||||
| 134 | a new base path for this controller / action to match. | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =head2 chain_pathpart | ||||||||||||||
| 137 | |||||||||||||||
| 138 | The value for 'PathPart' in your chaining declaration. This defaults to the namespace, | ||||||||||||||
| 139 | which is often correct but if you are doing something fussy you might need control | ||||||||||||||
| 140 | over it. | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head2 at | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Template used to control how we build the path to find your public file. | ||||||||||||||
| 145 | You probably want to leave this alone if you are seeking the most simple | ||||||||||||||
| 146 | thing (which this controller is aimed at). See the documentation for 'At' | ||||||||||||||
| 147 | over in L<Catalyst::ActionRole::Public> if you really need to mess with this | ||||||||||||||
| 148 | (and you might want the increased control that action role gives you anyway. | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head2 content_types | ||||||||||||||
| 151 | |||||||||||||||
| 152 | Content types that we allow to be served. By default we allow all standard | ||||||||||||||
| 153 | types (might be more than you want, if your public directory contains things | ||||||||||||||
| 154 | you don't want the public to see. | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =head2 show_debugging | ||||||||||||||
| 157 | |||||||||||||||
| 158 | Enabled developer debugging output. Default to 0 (false, no debugging). Change | ||||||||||||||
| 159 | to 1 if you want extended debugging info. | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head2 cache_control | ||||||||||||||
| 162 | |||||||||||||||
| 163 | Used to set the Cache-Control HTTP header (useful for caching your static assets). | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Example values "public, max-age=600" | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =head1 AUTHOR | ||||||||||||||
| 168 | |||||||||||||||
| 169 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||
| 170 | |||||||||||||||
| 171 | =head1 SEE ALSO | ||||||||||||||
| 172 | |||||||||||||||
| 173 | L<Catalyst>, L<Catalyst::Controller>, L<Plack::App::Directory>, | ||||||||||||||
| 174 | L<Catalyst::Controller::Assets>. L<Catalyst::Controller::SimpleCAS> | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||
| 179 | |||||||||||||||
| 180 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 181 | the same terms as Perl itself. | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =cut | ||||||||||||||