Created
March 9, 2010 15:33
-
-
Save ysasaki/326702 to your computer and use it in GitHub Desktop.
Mojolicious - send static file not in public path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::Controller; | |
use strict; | |
use warnings; | |
use base 'Mojolicious::Controller'; | |
use Path::Class; | |
sub sendfile { | |
my $self = shift; | |
my %args = @_; | |
if ( -e $args{file} and -r _ ) { | |
$self->res->headers->content_type($args{content_type}); | |
$self->res->body(scalar file($args{file})->slurp); | |
$self->res->code(200) unless $self->res->code; | |
} | |
else { | |
$self->res->headers->content_type('text/plain'); | |
$self->res->body('500 Internal ServerError'); | |
$self->res->code(500); | |
} | |
} | |
package MyApp::Example; | |
use strict; | |
use warnings; | |
use base 'MyApp::Controller'; | |
sub hoge { | |
my $self = shift; | |
$self->sendfile( | |
file => $self->app->home->rel_file('test.txt'), | |
content_type => 'text/plain' | |
); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment