Introduction to Mojolicio.us and Dancer2

Adam M Dutko
March 20, 2015

Overview


Web Servers

PSGI: Perl webServer Gateway Interface

Dancer2 Example

Mojolicio.us Example

Web Servers

Web Servers


Dedicated Server Process

Thread OR Process (or pools of one or the other)

Traditional Context (not event driven)

Request and Response

Response/Status Codes

Apache, nginx, lighttpd, others and yes even IIS

PSGI: Perl webServer Gateway Interface

PSGI: Perl webServer Gateway Interface


Standardized Interface

Web Servers <==> Apps/Frameworks

Wrapper around legacy code

Many Popular Compliant Frameworks

PSGI "Application(s)"

Hash Reference (IN) and Array Reference (OUT)

Hello World

Hello World


         my $app = sub {
             return 
             [   200,  #<--- STATUS
                   
                 ['Content-Type' => 'text/plain'],  #<--- HEADERS
                    
                 ["hello, world\n"]  #<--- BODY
             ];
         }

         plackup hello.psgi

        

Example

Dancer2

Dancer2

Source: http://perldancer.org/

Environment

Setup our Environment




          % curl -L https://cpanmin.us | perl - App::cpanminus
          % cpanm -i DigitalOcean 
          % cpanm -i Modern::Perl 
          % cpanm -i Class::XSAccessor 
          % cpanm -i Devel::Declare::MethodInstaller::Simple 
          % cpanm -i Sub::Name
          % cpanm --reinstall B::Hooks::OP::Check
        

new_vm.pl

            #!/usr/bin/env perl 
            use Modern::Perl '2011';
            use DigitalOcean;

            ## TODO: You should really add more error handling.
          
            my $client_id = 'CHANGE_ME';
            my $api_key   = 'CHANGE_ME';
             
            my $do = DigitalOcean->new(
                client_id      => $client_id,
                api_key        => $api_key,
                wait_on_events => 1
            );  

            my $new_droplet = $do->create_droplet(
                name      => $ARGV[0],
                size_id   => ,
                image_id  => , 
                region_id => ,
            );

          

Dancer2 Host Setup

Dancer2 Host Setup


          % perl scripts/new_vm.pl dancer2.clevelandpm.org
 
          ...

          % ssh root@NEWIP
          % Password: 
          
          % Changing password for root.
          % (current) UNIX password:
          % Enter new UNIX password:
          % Retype new UNIX password:

          root@dancer2:~ apt-get update
          
          root@dancer2:~ apt-get -y install build-essential 

          root@dancer2:~ curl -L https://cpanmin.us | perl - App::cpanminus 

          root@dancer2:~ cpanm Dancer2 (takes a while on the $5 VM)
          root@dancer2:~# cpanm Dancer2
          --> Working on Dancer2
          ...
          Successfully installed Dancer2-0.159002
          95 distributions installed

          WHOA!
        

Dancer2 Host Setup (Cont.)


          root@dancer2:~# dancer2
          dancer2 

          Available commands:

            commands: list the application's commands
                help: display a command's help screen

                 gen: create new Dancer2 application

        

Dancer2 Host Setup (Cont.)

          
          root@dancer2:~# dancer2 gen cleveland
          Error: Application name must be defined
          Usage: dancer2 

          dancer2 gen [-adopx] [long options...]
            -a STR --application STR    application name
            -d STR --directory STR      application folder (default: same as
                                        application name)
            -p STR --path STR           application path (default: current
                                        directory)
            -o --overwrite              overwrite existing files
            -x --no-check               don't check latest Dancer2 version
                                        (requires internet)
        

Dancer2 Host Setup (Cont.)


          ## Create our app
          root@dancer2:~# dancer2 gen -a cleveland
          + cleveland
          ...
          + cleveland/views/layouts/main.tt
          root@dancer2:~#
          
          ## Look around at the layout
          root@dancer2:~/cleveland# plackup bin/app.psgi
          HTTP::Server::PSGI: Accepting connections at http://0:5000/

          ## Open your web browser and check our new app is working 
          http://$IP_ADDRESS_OF_VM$:5000

        

Mojolicio.us

Source: http://mojolicio.us/

Mojolicio.us Host Setup

Mojolicio.us Host Setup


          % perl scripts/new_vm.pl mojo.clevelandpm.org

          ...

          % ssh root@NEWIP
          % Password: 
          
          % Changing password for root.
          % (current) UNIX password:
          % Enter new UNIX password:
          % Retype new UNIX password:

          root@mojo:~ apt-get update

          root@mojo:~ apt-get -y install build-essential 

          root@mojo:~ curl -L https://cpanmin.us | perl - App::cpanminus

          ## Might take a while!
          root@mojo:~ cpanm Mojolicious

        

Mojolicio.us Host Setup (Cont.)


          root@mojo:~ vi cleveland.pl
              use Mojolicious::Lite;

              get '/' => {text => 'I ♥ Mojolicious!'};

              app->start;
          :wq


          ## Look around at the layout
          root@mojo:~ MOJO_LISTEN=http://NEWIP:3000 morbo cleveland.pl
          Server available at http://NEWIP:3000

          ## Open your web browser and check our new app is working 
          http://$IP_ADDRESS_OF_VM$:3000
         
          #Lite vs Fullstack 

        

Mojolicio.us Host Setup (Cont.)

         root@mojo:~# mojo --help
         Usage: APPLICATION COMMAND [OPTIONS]
         
         Tip: CGI and PSGI environments can be automatically detected very often and
              work without commands.
              
         Options (for all commands):
            -h, --help          Get more information on a specific command
                --home    Path to your applications home directory, defaults to
                                the value of MOJO_HOME or auto detection
            -m, --mode    Operating mode for your application, defaults to the
                                value of MOJO_MODE/PLACK_ENV or "development"
                                                                   
         Commands:
          cgi       Start application with CGI
          cpanify   Upload distribution to CPAN
          daemon    Start application with HTTP and WebSocket server
          eval      Run code against application
          generate  Generate files and directories from templates
          get       Perform HTTP request
          inflate   Inflate embedded files to real files
          prefork   Start application with preforking HTTP and WebSocket server
          psgi      Start application with PSGI
          routes    Show available routes
          test      Run tests
          version   Show versions of available modules
                                                                        
         See 'APPLICATION help COMMAND' for more information on a specific command. 
        

Questions???

Sources