<% file .name %> | <% file .type %> | <% file .modified %> | <% END %> racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Route for Filebrowser
get ’/home’ => sub { my $files = autoindex(’/ ’);
template ’filebrowser ’, {directory => ’Home’, files => $files , }; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Configuration
Standard config.yml:
# template engine # simple: default and very basic template engine template: "simple"
Dropbox config.yml:
template: "template_toolkit"
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Routes and Keywords
I HTTP method
I get I post I ... I any
I Path
I Subroutine
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Routes
I String
I Named parameters I Wildcards
I Splat I Megasplat
I Regular expression
I Regular expression with captures
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
String
get ’/home’ => sub { my $files = autoindex(’/ ’);
template ’filebrowser ’, {directory => ’Home’, files => $files , }; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Named parameters
get ’/home/: file ’ => sub { my $files = autoindex(param( ’file ’));
template ’filebrowser ’, {directory => param(’file ’), files => $files , }; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Splat
get ’/images/covers/ ∗ . jpg ’ => sub { my ($isbn) = splat;
i f (− f "public/images/covers/$isbn.jpg") { return send_file "images/covers/$isbn.jpg"; }
status ’not_found’; forward 404; }
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Megasplat
https :// eshop. state .gov/ lostpwd / biz@linuxia .de/e642bd543b9907bd2c06aa485261cb1a849a9f23fc7324bff45ebd35f4efe2cb
get ’/lostpwd/∗∗ ’ => sub { my ($email, $hash) = splat;
form−>fill (email => $email, hash => $hash);
template( ’lostpwd_confirm ’ , form => $form); }
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Regular Expression
Catch-All (last route!)
any qr { . ∗ } => sub { ... };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Regular Expression with Captures
https ://dropbox.nite.si/~racke/talks/dancer−beamer . pdf
any qr{^/~(?[^/]+)/(?. ∗ ? ) / ? $ } => sub { my ($capts, $user, $file);
$capts = captures; $file = $capts −>{ f i l e } ; $user = $capts −>{user } ;
... };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future
Keywords
I get, post, any, put, del, ...
I request, params, param
I redirect, forward, status, header
I config, var, session
I from_json, to_json, from_xml, to_xml
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Customizing your application Templates Download/Upload Routes Security and Error Handling Keywords Deployment Conclusion and Future var(s) and session
Storing and retrieving data for the current request:
var bar => ’pivo’; $bar = var ’bar’; $bar = vars −>{bar } ;
Storing and retrieving data from the session:
session username => ’[email protected]’;
i f (! session( ’username’)) { redirect uri_for(’/login’); }
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Customizing your application
I Engines
I Hooks
I Plugins
I Middleware
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Engines
I Templates
I Sessions
I Logs
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Template Engines
I Dancer::Template::Toolkit
I Dancer::Template::Xslate
I Dancer::Template::TemplateFlute
I ... dozens more
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Configuration
template: "template_flute"
engines : template_flute: f i l t e r s : date : options : format : "%m/%d/%Y" s t r i c t : empty : 0
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Session Engines
Dancer::Session::YAML File based, development only Dancer::Session::Storable File based Dancer::Session::DBI Dancer::Session::Memcached Dancer::Session::MongoDB Dancer::Session::Cookie Session data stored encrypted in a cookie.
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Sessions: Storable
I Engine session: Storable
I Directory session_dir: /var/run/dancer-sessions
I Expiration session_expires: 8 hours
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Sessions: Cookie
I Engine session: Cookie
I Key session_cookie_key: kjsdf07234hjf0sdkflj12*&(@*jk
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Logger Engines
Dancer::Logger::Console Dancer::Logger::File File in logs/ directory, e.g. logs/production.log Dancer::Logger::Syslog Dancer::Logger::Log4perl
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Logger Configuration
I Engine logger: file
I Directory log_path: log
I File log_file: error.log
I Format logger_format: "%t [%P] %L @%D> %m in %f l. %l"
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Hooks
I What is a hook?
I before hook
I before_template_render hook
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
What is a Hook
I Events inside core and plugins
I Hook into the path of execution
I Subroutines in your application
I Multiple hook routines
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future before Hook
Password protected site:
hook ’before’ => sub { unless (session( ’user ’) | | request −>path eq ’/login’ | | request −>path =~ m%^/lostpwd% ){ redirect ’/login’; } };
Files in public aren’t protected!
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future before_template_render Hook
I template ’index’, {message => ’Hello’}
I add default tokens (params, vars, session)
I before_template_render hook
I rendering
I after_template_render hook
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future before_template_render Hook
hook before_template_render => sub { my $tokens = s h i f t ; my $menu ;
i f (session( ’user’)) { $menu = [{name => ’Logout’, url => ’/logout’}]; } else { $menu = [{name => ’Login’, url => ’/login’}]; }
$tokens −>{menu} = $menu ; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Plugins
I Features
I Keywords
I Routes
I Configuration
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Plugins on CPAN
I Count: more than 100
I Overview https://metacpan.org/module/Dancer::Plugins
I Search https://metacpan.org/search?q=dancer+plugin
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Plugins on CPAN
I Dancer::Plugin::Database database
I Dancer::Plugin::Email email
I Dancer::Plugin::Auth::Extensible require_login,require_role,... /login, /login-denied/
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Database Example
my $user = database−>quick_select( ’users ’ , {username => params−>{’username ’ }});
i f ( $user ) { session user => params−>{’username ’ }; }
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Database Configuration
plugins : Database : d r i v e r : Pg database: dropbox username: vienna password: nevairbe
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
More SQL Plugins
I Dancer::Plugin::DBIC
I Dancer::Plugin::SimpleCRUD
I ...
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Email Example
$message = template( ’lostpwd_email ’ , {password => $password, url => $url}, { l a y o u t => undef });
email ({type => ’html’, from => ’[email protected]’, to => param( ’email’), subject => ’Forgot password’, message => $message});
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Email Redirect
I Module: Email::Sender::Transport::Redirect
I Configuration:
plugins : Email : t r a n s p o r t : Sendmail : redirect_address: [email protected]
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Auth::Extensible require_login
get ’ / home/∗∗ ’ => require_login sub { ... };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Auth::Extensible require_role
get ’/accounts’ => require_role Administrator sub { template ’accounts’, ... };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Dancer::Plugin::Auth::Extensible Providers
I Config
I Unix
I Database
I LDAP
I Subclass D::P::Auth::Extensible::Provider::Base
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Write Your Own Plugin
package Dancer :: Plugin ::SSO;
use Dancer:: Plugin;
register sso_generate_token => sub { ... };
register_plugin;
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Hooks in Your Own Plugin
Plugin:
register_hook qw/sso_login sso_failure /;
Application:
hook sso_login => sub { my $user_ref = s h i f t ;
session user => $user_ref −>username ; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Middleware Example
#!/usr/bin/env perl
use Dancer ; use Dropbox ; use Plack:: Builder;
my $app = sub { my $env = s h i f t ; my $request = Dancer::Request−>new(env => $env); Dancer−>dance($request ); };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Middleware Example
b u i l d e r { enable SizeLimit => ( max_unshared_size_in_kb => 102400, # 100 Mb check_every_n_requests => 10, );
$app ; };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Engines Customizing your application Hooks Download/Upload Plugins Security and Error Handling Middleware Deployment Conclusion and Future
Middleware in Configuration
plack_middlewares : − − S i z e L i m i t − max_unshared_size_in_kb − 102400
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Download Customizing your application Upload Object Download/Upload Upload HTML Form Security and Error Handling Upload Route Deployment Conclusion and Future
Download/Upload
I File download
I Upload object
I Upload form + code
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Download Customizing your application Upload Object Download/Upload Upload HTML Form Security and Error Handling Upload Route Deployment Conclusion and Future
File Download
get ’ / home/∗∗ ’ => sub { my ($parts) = splat; my $path_relative = join (’/ ’, @$parts); my $path = autoindex_file_path($path_relative );
i f (− f $path ) { return send_file $path, system_path => 1; } e l s i f (−d $path ) { return template ’filebrowser ’, {directory => pop( @$parts ) , files => autoindex($path_relative )}; } };
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Download Customizing your application Upload Object Download/Upload Upload HTML Form Security and Error Handling Upload Route Deployment Conclusion and Future
Upload Object
upload( ’upload_file ’) => Dancer::Request::Upload
filename Filename sent by user basename Stripped filename size Size in bytes type Content type content File content copy_to Copies temporary file
racke Dancer Academy - From Zero to Hero Dance floor Templates, Routes and Keywords Download Customizing your application Upload Object Download/Upload Upload HTML Form Security and Error Handling Upload Route Deployment Conclusion and Future
Upload HTML-Form