Last active
August 29, 2015 14:13
-
-
Save mlampret/bc24df674b4a9cff6eac to your computer and use it in GitHub Desktop.
Mojolicious Plugin for virtual version directory.
This file contains hidden or 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 Mojolicious::Plugin::VersionDir; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
use strict; | |
use warnings; | |
my $c = undef; | |
my $app = undef; | |
my $conf = undef; | |
sub register { | |
my ($self, $app_param, $args) = @_; | |
$app = $app_param; | |
$conf = $args; | |
$conf->{version} ||= time; | |
$app->helper( | |
'version_dir' => sub { | |
my $self = shift; | |
my $class_name = shift; | |
$class_name ||= 'Mojolicious::Plugin::VersionDir'; | |
unless ($class_name =~ m/[A-Z]/) { | |
my $namespace = ref($self->app) . '::'; | |
$namespace = '' if $namespace =~ m/^Mojolicious::Lite/; | |
$class_name = join '' => $namespace, Mojo::ByteStream->new($class_name)->camelize; | |
} | |
my $e = Mojo::Loader->new->load($class_name); | |
Carp::croak qq/Can't load validator '$class_name': / . $e->message if ref $e; | |
Carp::croak qq/Can't find validator '$class_name'/ if $e; | |
Carp::croak qq/Wrong validator '$class_name' isa/ unless $class_name->isa($class_name); | |
return $class_name->new(%$conf, @_); | |
} | |
); | |
$app->hook( before_routes => sub { | |
$c = shift; | |
}); | |
$app->routes->get('/vd/:version/*rel_path')->name('version_dir')->to( | |
cb => sub { | |
my $self = shift; | |
$self->render_static($self->param('rel_path')); | |
} | |
); | |
} | |
sub path { | |
my $self = shift; | |
return '/vd/' . $conf->{version}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This plugin will simulate subdirectories in your public directory. It is useful to force browser to reload static files. Usage:
In App startup():
In Template:
Will generate: