Dancing Tutorial Alberto Manuel Brand ao Sim oes ambs@perl.pt - - PowerPoint PPT Presentation

dancing tutorial
SMART_READER_LITE
LIVE PREVIEW

Dancing Tutorial Alberto Manuel Brand ao Sim oes ambs@perl.pt - - PowerPoint PPT Presentation

Dancing Tutorial Alberto Manuel Brand ao Sim oes ambs@perl.pt Braga Geek Nights March 1 st Alberto Sim oes Dancing Tutorial Part 0: Ballet Alberto Sim oes Dancing Tutorial Whats Dancer? A micro framework for writing web


slide-1
SLIDE 1

Dancing Tutorial

Alberto Manuel Brand˜ ao Sim˜

  • es

ambs@perl.pt Braga Geek Nights March 1st

Alberto Sim˜

  • es

Dancing Tutorial

slide-2
SLIDE 2

Part 0: Ballet

Alberto Sim˜

  • es

Dancing Tutorial

slide-3
SLIDE 3

What’s Dancer?

slide-4
SLIDE 4

A micro framework for writing web applications

slide-5
SLIDE 5

So, Yet Another Web Framework?

slide-6
SLIDE 6

Well, yes!

slide-7
SLIDE 7

CGI.pm webapps are spaghetti

slide-8
SLIDE 8

Catalyst is HUGE

slide-9
SLIDE 9

Ruby introduced something different

slide-10
SLIDE 10
slide-11
SLIDE 11

We needed something similar in Perl

slide-12
SLIDE 12

Definitely

slide-13
SLIDE 13

And they also imitated us!

slide-14
SLIDE 14

Available at http://perldancer.org and in GitHub.

slide-15
SLIDE 15

Part 1: Tango

Alberto Sim˜

  • es

Dancing Tutorial

slide-16
SLIDE 16

Installing

✞ ☎

$ sudo cpan Dancer

✝ ✆

  • r

✞ ☎

$ sudo cpan Task : : Dancer

✝ ✆

  • r yet

✞ ☎

$ cpanm −S Task : : Dancer

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-17
SLIDE 17

Bootstrap application

✞ ☎

$ dancer −a MyApp + MyApp/bin/app . pl + MyApp/config . yml + MyApp/environments/development . yml + MyApp/environments/production . yml + MyApp/views/index . tt + MyApp/views/layouts/main . tt + MyApp/lib/MyApp . pm + MyApp/public/javascripts/jquery . js + MyApp/public/css/style . css + MyApp/public/css/error . css + MyApp/public/images / . . . + MyApp/public /500. html + MyApp/public /404. html + MyApp/public/dispatch . fcgi + MyApp/public/dispatch . cgi + MyApp/Makefile . PL + MyApp/t/002 _index_route . t + MyApp/t/001 _base . t

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-18
SLIDE 18

What’s inside

✞ ☎

+ MyApp/bin/app . pl

✝ ✆

a standalone light server (starts server in port 3000); also used for fast-cgi or common cgi backends;

✞ ☎

+ MyApp/config . yml + MyApp/environments/development . yml + MyApp/environments/production . yml

✝ ✆

main configuration file (plugins, modules, etc); configuration files for production and development:

defines what to report, where to report, etc.

Alberto Sim˜

  • es

Dancing Tutorial

slide-19
SLIDE 19

What’s inside

✞ ☎

+ MyApp/bin/app . pl

✝ ✆

a standalone light server (starts server in port 3000); also used for fast-cgi or common cgi backends;

✞ ☎

+ MyApp/config . yml + MyApp/environments/development . yml + MyApp/environments/production . yml

✝ ✆

main configuration file (plugins, modules, etc); configuration files for production and development:

defines what to report, where to report, etc.

Alberto Sim˜

  • es

Dancing Tutorial

slide-20
SLIDE 20

What’s inside

✞ ☎

+ MyApp/views/index . tt + MyApp/views/layouts/main . tt

✝ ✆

Templates and layouts:

templates are page portions/scraps; layouts are full page designs (they are automagically filled by the template); ✞ ☎

+ MyApp/public/javascripts/jquery . js + MyApp/public/css/style . css + MyApp/public/css/error . css + MyApp/public/images / . . .

✝ ✆

public/static files:

javascript (Dancer ships with jsquery); cascade style sheets; images (for default design);

Alberto Sim˜

  • es

Dancing Tutorial

slide-21
SLIDE 21

What’s inside

✞ ☎

+ MyApp/views/index . tt + MyApp/views/layouts/main . tt

✝ ✆

Templates and layouts:

templates are page portions/scraps; layouts are full page designs (they are automagically filled by the template); ✞ ☎

+ MyApp/public/javascripts/jquery . js + MyApp/public/css/style . css + MyApp/public/css/error . css + MyApp/public/images / . . .

✝ ✆

public/static files:

javascript (Dancer ships with jsquery); cascade style sheets; images (for default design);

Alberto Sim˜

  • es

Dancing Tutorial

slide-22
SLIDE 22

What’s inside

✞ ☎

+ MyApp/public /500. html + MyApp/public /404. html

✝ ✆

pages for 500 and 404 errors;

✞ ☎

+ MyApp/public/dispatch . fcgi + MyApp/public/dispatch . cgi

✝ ✆

wrappers to configure fast-cgi and cgi backends;

will be back on this later (future talk?);

Alberto Sim˜

  • es

Dancing Tutorial

slide-23
SLIDE 23

What’s inside

✞ ☎

+ MyApp/public /500. html + MyApp/public /404. html

✝ ✆

pages for 500 and 404 errors;

✞ ☎

+ MyApp/public/dispatch . fcgi + MyApp/public/dispatch . cgi

✝ ✆

wrappers to configure fast-cgi and cgi backends;

will be back on this later (future talk?);

Alberto Sim˜

  • es

Dancing Tutorial

slide-24
SLIDE 24

What’s inside

✞ ☎

+ MyApp/Makefile . PL + MyApp/t/002 _index_route . t + MyApp/t/001 _base . t

✝ ✆

Main module Makefile:

usefull to make module of your application

Your test suite;

✞ ☎

+ MyApp/lib/MyApp . pm

✝ ✆

Your application!

Alberto Sim˜

  • es

Dancing Tutorial

slide-25
SLIDE 25

What’s inside

✞ ☎

+ MyApp/Makefile . PL + MyApp/t/002 _index_route . t + MyApp/t/001 _base . t

✝ ✆

Main module Makefile:

usefull to make module of your application

Your test suite;

✞ ☎

+ MyApp/lib/MyApp . pm

✝ ✆

Your application!

Alberto Sim˜

  • es

Dancing Tutorial

slide-26
SLIDE 26

Part 2: Flamenco

Alberto Sim˜

  • es

Dancing Tutorial

slide-27
SLIDE 27

Traffic Control

Your MyApp/lib/MyApp.pm includes:

✞ ☎

package MyApp ; use Dancer ’ : syntax ’ ;

  • ur $VERSION = ’ 0.1 ’ ;

get ’ / ’ = > sub { template ’ index ’ ; }; true ;

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-28
SLIDE 28

How to test it?

Start the standalone server,

✞ ☎

[ ambs@rachmaninoff MyApp ] $ bin/app . pl

✝ ✆

that shows debug info,

✞ ☎

[ 9 3 8 7 ] core @0 .000017 > loading Dancer : : Handler : : Standalone handler in /opt/lib/perl5/site_perl /5.12.3/ Dancer/Handler . pm l . 39 [ 9 3 8 7 ] core @0 .000442 > loading handler ’ Dancer : : Handler : : Standalone ’ in /opt/ l o c a l /lib/perl5/site_perl /5.12.3/ Dancer . pm l . 230 > > Dancer 1.3011 server 9387 listening

  • n http : / / 0 . 0 . 0 . 0 : 3 0 0 0

\subsection { Entering the development dance floor . . . }

✝ ✆

and open a browser in the specified port.

Alberto Sim˜

  • es

Dancing Tutorial

slide-29
SLIDE 29

It Works!

slide-30
SLIDE 30

Route Handlers

A Dancer app is a collection of route handlers; A route handler is, basically, a sub; It is bound to an http method; And to a path or a path pattern; Example

get ’ / ’ = > sub { . . . }; post ’ / submit /: f i l e ’ = > sub { . . . }; del ’ / r e s o u r c e /∗ ’ = > sub { . . . };

Alberto Sim˜

  • es

Dancing Tutorial

slide-31
SLIDE 31

Route Handlers

A Dancer app is a collection of route handlers; A route handler is, basically, a sub; It is bound to an http method; And to a path or a path pattern; Example

get ’ / ’ = > sub { . . . }; post ’ / submit /: f i l e ’ = > sub { . . . }; del ’ / r e s o u r c e /∗ ’ = > sub { . . . };

Alberto Sim˜

  • es

Dancing Tutorial

slide-32
SLIDE 32

Route Handlers

Static patterns (paths):

✞ ☎

get ’ / ’ = > sub { . . . }; get ’ / about ’ = > sub { . . . };

✝ ✆

Patterns with named tokens:

✞ ☎

get ’ /book /: i d ’ = > sub { # do something with params−>{i d } . . . }; get ’ / user /: group /: uid ’ = > sub { # use params−>{uid } and params−>{group } here };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-33
SLIDE 33

Route Handlers

Static patterns (paths):

✞ ☎

get ’ / ’ = > sub { . . . }; get ’ / about ’ = > sub { . . . };

✝ ✆

Patterns with named tokens:

✞ ☎

get ’ /book /: i d ’ = > sub { # do something with params−>{i d } . . . }; get ’ / user /: group /: uid ’ = > sub { # use params−>{uid } and params−>{group } here };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-34
SLIDE 34

Route Handlers

Patterns with anonymous tokens:

✞ ☎

get ’ / f i l e /∗.∗ ’ = > sub { my ( $file , $ext ) = splat ; . . . } get ’ /show/∗/∗ ’ = > sub { my ( $cat , $subcat ) = splat ; };

✝ ✆

Regular expressions:

✞ ☎

get qr{post /(\ d+)−(\d+)−(\d+)} = > sub { my ( $year , $month , $day ) = splat ; }

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-35
SLIDE 35

Route Handlers

Patterns with anonymous tokens:

✞ ☎

get ’ / f i l e /∗.∗ ’ = > sub { my ( $file , $ext ) = splat ; . . . } get ’ /show/∗/∗ ’ = > sub { my ( $cat , $subcat ) = splat ; };

✝ ✆

Regular expressions:

✞ ☎

get qr{post /(\ d+)−(\d+)−(\d+)} = > sub { my ( $year , $month , $day ) = splat ; }

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-36
SLIDE 36

Part 3: Mambo

Alberto Sim˜

  • es

Dancing Tutorial

slide-37
SLIDE 37

Templating

Dancer has plugins for most templating systems: Mason, Template Toolkit and others. Default is a Simple template system, built-in Dancer. Use the template inside the route:

✞ ☎

get ’ / user /: name ’ = > sub { template ’ p r o f i l e ’ = > { username = > params−>{name} }; };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-38
SLIDE 38

Templating

Dancer has plugins for most templating systems: Mason, Template Toolkit and others. Default is a Simple template system, built-in Dancer. Use the template inside the route:

✞ ☎

get ’ / user /: name ’ = > sub { template ’ p r o f i l e ’ = > { username = > params−>{name} }; };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-39
SLIDE 39

Rerouting

You can reroute by: Passing the control to the next matching handler:

✞ ☎

get ’ / l a z y ’ = > sub { pass and r e t u r n false ; };

✝ ✆

Redirecting to other URI:

✞ ☎

get ’ / f o r b i d d e n ’ = > sub { r e t u r n redirect ’ / b e t t e r / pl ac e ’ };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-40
SLIDE 40

Rerouting

You can reroute by: Passing the control to the next matching handler:

✞ ☎

get ’ / l a z y ’ = > sub { pass and r e t u r n false ; };

✝ ✆

Redirecting to other URI:

✞ ☎

get ’ / f o r b i d d e n ’ = > sub { r e t u r n redirect ’ / b e t t e r / pl ac e ’ };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-41
SLIDE 41

Rerouting

You can reroute by: Passing the control to the next matching handler:

✞ ☎

get ’ / l a z y ’ = > sub { pass and r e t u r n false ; };

✝ ✆

Redirecting to other URI:

✞ ☎

get ’ / f o r b i d d e n ’ = > sub { r e t u r n redirect ’ / b e t t e r / pl ac e ’ };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-42
SLIDE 42

Serving Files

You can serve a static file:

✞ ☎

get ’ /dowload /: f i l e ’ = > sub { my $file = params−>{file }; pass and r e t u r n false u n l e s s −f $file ; send_file $file ; };

✝ ✆

If the content is generated, just change content-type:

✞ ☎

get ’ /readme . t x t ’ = > sub { content_type ’ t e x t / p l a i n ’ ; r e t u r n ’ t h i s i s p l a i n t e x t ’ };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-43
SLIDE 43

Serving Files

You can serve a static file:

✞ ☎

get ’ /dowload /: f i l e ’ = > sub { my $file = params−>{file }; pass and r e t u r n false u n l e s s −f $file ; send_file $file ; };

✝ ✆

If the content is generated, just change content-type:

✞ ☎

get ’ /readme . t x t ’ = > sub { content_type ’ t e x t / p l a i n ’ ; r e t u r n ’ t h i s i s p l a i n t e x t ’ };

✝ ✆

Alberto Sim˜

  • es

Dancing Tutorial

slide-44
SLIDE 44

Thanks to Alexis Sukrieh (I stole some slides)