Mojolicious

10,159 views
9,972 views

Published on

A introduction to the Mojolicious web framework.

Published in: Technology

Mojolicious

  1. 1. Mojolicious - Web development rethought Marcus Ramberg Nordaaker
  2. 2. Sebastian Riedel
  3. 3. Sebastian Riedel • twitter.com/kraih
  4. 4. Sebastian Riedel • twitter.com/kraih • Creator of Catalyst
  5. 5. Sebastian Riedel • twitter.com/kraih • Creator of Catalyst • Went over to the dark side (*cough*RoR*cough*)
  6. 6. Sebastian Riedel • twitter.com/kraih • Creator of Catalyst • Went over to the dark side (*cough*RoR*cough*) • This is what he brought back
  7. 7. mojolicious.org
  8. 8. Installation
  9. 9. Installation • cpan Mojolicious
  10. 10. Installation • cpan Mojolicious • There is no step 2
  11. 11. Installation • cpan Mojolicious • There is no step 2 • No dependencies beyond Perl 5.8.1+
  12. 12. Installation • cpan Mojolicious • There is no step 2 • No dependencies beyond Perl 5.8.1+ • Could also easily be bundled with your app.
  13. 13. With an unconfigured Perl
  14. 14. With an unconfigured Perl ★ $ curl -L cpanmin.us | perl - Mojolicious Fetching http://search.cpan.org/CPAN/ authors/id/K/KR/KRAIH/ Mojolicious-0.999922.tar.gz ... OK Configuring Mojolicious-0.999922 ... OK Building and testing Mojolicious-0.999922 for Mojolicious ... OK Successfully installed Mojolicious-0.999922
  15. 15. With an unconfigured Perl ★ $ curl -L cpanmin.us | perl - Mojolicious Fetching http://search.cpan.org/CPAN/ authors/id/K/KR/KRAIH/ Mojolicious-0.999922.tar.gz ... OK Configuring Mojolicious-0.999922 ... OK Building and testing Mojolicious-0.999922 for Mojolicious ... OK Successfully installed Mojolicious-0.999922 ★ Gratuitous App::cpanminus plug :)
  16. 16. $ mojolicious generate lite_app [exist] /Users/marcus [write] /Users/marcus/foosball [chmod] foosball 744 That was easy. Now let’s check out the contents of the file
  17. 17. Lite app #!/usr/bin/env perl use Mojolicious::Lite; get '/' => 'index'; get '/:groovy' => sub { my $self = shift; $self->render_text( $self->param('groovy')); }; shagadelic;
  18. 18. Lite app (enterprise edition) #!/usr/bin/env perl use Mojolicious::Lite; get '/' => 'index'; get '/:synergy' => sub { my $self = shift; $self->render_text( $self->param('synergy')); }; app->start;
  19. 19. View __DATA__ @@ index.html.ep % layout 'funky'; Yea baby! @@ layouts/funky.html.ep <!doctype html><html> <head><title>Funky!</title></head> <body><%== content %></body> </html>
  20. 20. Let’s fire her up: $ ./foosball usage: foosball COMMAND [OPTIONS] These commands are currently available: generate Generate files and directories from templates. routes Show available routes. cgi Start application with CGI backend. daemon Start application with HTTP 1.1 backend. daemon_prefork Start application with preforking HTTP 1.1 backend. fastcgi Start application with FastCGI backend. get Get file from URL. psgi Start application with PSGI backend. test Run unit tests. version Show versions of installed modules. See 'foosball help COMMAND' for more information on a specific command. $ ./foosball daemon Server available at http://Command-Central.local:3000.
  21. 21. Handles 3 different urls •/ • /* • /*/*
  22. 22. Features
  23. 23. Features ★ A powerful routes based dispatcher
  24. 24. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server)
  25. 25. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server) ★ Simple Template System
  26. 26. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server) ★ Simple Template System ★ JSON support built-in
  27. 27. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server) ★ Simple Template System ★ JSON support built-in ★ Elegant plugin system
  28. 28. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server) ★ Simple Template System ★ JSON support built-in ★ Elegant plugin system ★ Class reloader
  29. 29. Features ★ A powerful routes based dispatcher ★ Full HTTP 1/1 implementation (Client/ Server) ★ Simple Template System ★ JSON support built-in ★ Elegant plugin system ★ Class reloader ★ And a lot more
  30. 30. Routes - snakes and ladders # ::Lite get ‘/’ => ‘index’; # sub refs for functions post ‘/login’ => sub { .. }; # Placeholders & Actions: get ‘/:foo’ => sub {},‘ctrl’ # All together get '/everything/:stuff' => [stuff => qr/d+/] => {stuff => 23} => sub { shift->render('welcome'); }
  31. 31. Routes - snakes and ladders ladder sub { my $self = shift # Authenticated my $name = $self->param('name') || ''; return 1 if $name eq 'Bender'; # Not authenticated $self->render('denied'); return; }
  32. 32. More routes $r->route(‘/’)->to (controller=>‘foo’,action=> ‘bar’); ('lists#new', id => 1)->name('new'); ('/:controller/:action/:id')->to ('example#welcome', id => 1) # Bridges my $auth=$r->bridge->to(‘auth#check’); $auth->route(...)
  33. 33. HTTP 1.1 stack • HTTP Stack implemented based on RFCs • Full HTTP implementation, including pipelining • Does not use LWP, No request body in memory.
  34. 34. Classes Mojo::Base Mojo::ByteStream Mojo::Template, Mojo::JSON Mojo::Loader,Mojo::Log,Mojo::Path Mojo::URL,Mojo::Parameters Mojo::Content Mojo::Message::Request Mojo::Message::Response Mojo::Headers,Mojo::Cookie, Mojo::Date
  35. 35. Base Classes Mojo::Base Mojo::ByteStream Mojo::Template, Mojo::JSON Mojo::Loader,Mojo::Log,Mojo::Path Mojo::URL,Mojo::Parameters Mojo::Content Mojo::Message::Request Mojo::Message::Response Mojo::Headers,Mojo::Cookie, Mojo::Date
  36. 36. Mojo::Template % my $player=$self->stash(‘players’); %= $player; # print %== $player; # raw <%= player %> # inline style % # Normal comment <% # inline comment %> .ep - prepopulates stash for you .epl - same templates, less magic
  37. 37. Base Classes Mojo::Base Mojo::ByteStream Mojo::Template, Mojo::JSON Mojo::Loader,Mojo::Log,Mojo::Path Mojo::URL,Mojo::Parameters Mojo::Content Mojo::Message::Request Mojo::Message::Response Mojo::Headers,Mojo::Cookie, Mojo::Date
  38. 38. JSON support use Mojo::JSON; my $json=Mojo::JSON->new; my $str=$json->encode({foo => ‘bar’}); my $strct=$json->decode (‘[“foo”,”bar”’]);
  39. 39. Classes Mojo::Base Mojo::ByteStream Mojo::Template, Mojo::JSON Mojo::Loader,Mojo::Log,Mojo::Path Mojo::URL,Mojo::Parameters Mojo::Content Mojo::Message::Request Mojo::Message::Response Mojo::Headers,Mojo::Cookie, Mojo::Date
  40. 40. More Classes Mojo::Transaction, Mojo::Stateful Mojo::IOLoop Mojo::Client, Mojo::Server Mojo::Server::CGI Mojo::Server::FastCGI Mojo::Server::PSGI Mojo::Server::Daemon, ::Prefork Mojo::Command::*
  41. 41. More Classes Mojo::Transaction, Mojo::Stateful Mojo::IOLoop Mojo::Client, Mojo::Server Mojo::Server::CGI Mojo::Server::FastCGI Mojo::Server::PSGI Mojo::Server::Daemon, ::Prefork Mojo::Command::*
  42. 42. HTTP Client my $client=Mojo::Client->new; $client->get( ‘http://iusethis.com/new.rss’ => sub { my ($self,$tx)=@_; say $tx->res; })->process; #inside mojolicious $self->client->post(...); # also works with the ioloop.
  43. 43. Mojolicious classes Mojolicious Mojolicios::Lite Mojolicious::Command::* Mojolicious::Plugins Mojolicious::Plugin::* Mojolicious::Controller
  44. 44. Mojolicious classes Mojolicious Mojolicios::Lite Mojolicious::Command::* Mojolicious::Plugins Mojolicious::Plugin::* Mojolicious::Controller
  45. 45. Plugin system ★ Hook based ★ before_dispatch ★ after_dispatch ★ after_static_dispatch ★ after_build_tx ★ Also apropriate place to add types & renders & custom routes extension.
  46. 46. Mojolicious classes Mojolicious Mojolicios::Lite Mojolicious::Command::* Mojolicious::Plugins Mojolicious::Plugin::* Mojolicious::Controller
  47. 47. Mojolicious default plugins ★ Mojolicious::Plugin::AgentCondition - Agent Condition   ★ Mojolicious::Plugin::Charset - Default Charset   ★ Mojolicious::Plugin::DefaultHelpers - Default Helpers   ★ Mojolicious::Plugin::EpRenderer - EP Renderer   ★ Mojolicious::Plugin::EplRenderer -EPL Renderer  ★ Mojolicious::Plugin::HeaderCondition - Header Condition  ★ Mojolicious::Plugin::I18n Internationalization Plugin   ★ Mojolicious::Plugin::JsonConfig - JSON Configuration  ★ Mojolicious::Plugin::PodRenderer - POD Renderer  ★ Mojolicious::Plugin::PoweredBy - Powered By Plugin    ★ Mojolicious::Plugin::RequestTimer - Time requests
  48. 48. Mojolicious default plugins ★ Mojolicious::Plugin::AgentCondition - Agent Condition   ★ Mojolicious::Plugin::Charset - Default Charset   ★ Mojolicious::Plugin::DefaultHelpers - Default Helpers   ★ Mojolicious::Plugin::EpRenderer - EP Renderer   ★ Mojolicious::Plugin::EplRenderer -EPL Renderer  ★ Mojolicious::Plugin::HeaderCondition - Header Condition  ★ Mojolicious::Plugin::I18n Internationalization Plugin   ★ Mojolicious::Plugin::JsonConfig - JSON Configuration  ★ Mojolicious::Plugin::PodRenderer - POD Renderer  ★ Mojolicious::Plugin::PoweredBy - Powered By Plugin    ★ Mojolicious::Plugin::RequestTimer - Time requests
  49. 49. default helpers ★ dumper ★ param ★ stash ★ layout ★ include ★ content ★ extends ★ url_for
  50. 50. Mojolicious classes Mojolicious Mojolicios::Lite Mojolicious::Command::* Mojolicious::Plugins Mojolicious::Plugin::* Mojolicious::Controller
  51. 51. Mojolicious::Controller ->render ->render(template=>‘foo/bar’) ->render(controller=>‘foo’,action=>‘bar’) ->render(‘foo#bar’) ->render_text ->render_json ->render_inner ->render_partial ->pause / ->resume ->redirect_to / ->url_for
  52. 52. STOP THE PRESS! WEBSOCKET! HTML5 protocol for long running processes Mojo first Perl framework to add support perl -MMojolicious::Lite -e "websocket '/' => sub {shift->receive_message(sub { shift- >send_message(shift)})}; shagadelic 'daemon'"
  53. 53. Get the source • http://github.com/kraih/ mojo • Pull requests welcome • Just fork and play
  54. 54. Questions?
  55. 55. Questions? • Thanks for listening
  56. 56. Questions? • Thanks for listening • marcus@nordaaker.com

×