NAME

OVERVIEW

DEPLOYMENT

Getting Mojolicious and Mojolicious::Lite applications running on different platforms. Note that many real-time web features are based on the Mojo::IOLoop reactor, and therefore require one of the built-in web servers to be able to use them to their full potential.

Built-in web server

Mojolicious contains a very portable non-blocking I/O HTTP 1.1 and WebSocket server with Mojo::Server::Daemon. It is usually used during development and in the construction of more advanced web servers, but is solid and fast enough for small to mid sized applications.

$ ./script/myapp daemon
Server available at http://127.0.0.1:3000.

It has many configuration options and is known to work on every platform Perl works on.

$ ./script/myapp help daemon
...List of available options...

Another huge advantage is that it supports TLS and WebSockets out of the box.

But one of its biggest advantages is the support for effortless zero downtime software upgrades. That means you can upgrade Mojolicious, Perl or even system libraries at runtime without ever stopping the server or losing a single incoming connection, just by running the command above again.

You might also want to enable proxy support if you're using Hypnotoad behind a reverse proxy. This allows Mojolicious to automatically pick up the X-Forwarded-For, X-Forwarded-Host and X-Forwarded-HTTPS headers.

# myapp.conf
{hypnotoad => {proxy => 1}};

Nginx

One of the most popular setups these days is the built-in web server behind a Nginx reverse proxy.

Apache/CGI

CGI is supported out of the box and your Mojolicious application will automatically detect that it is executed as a CGI script.

ScriptAlias / /home/sri/myapp/script/myapp/

PSGI/Plack

PSGI is an interface between Perl web frameworks and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers. PSGI and Plack are inspired by Python's WSGI and Ruby's Rack. Mojolicious applications are ridiculously simple to deploy with Plack.

Plack provides many server and protocol adapters for you to choose from such as FCGI, SCGI and mod_perl. Make sure to run plackup from your applications home directory, otherwise libraries might not be found.

$ plackup ./script/myapp -s FCGI -l /tmp/myapp.sock

Because plackup uses a weird trick to load your script, Mojolicious is not always able to detect the applications home directory, if that's the case you can simply use the MOJO_HOME environment variable. Also note that app->start needs to be the last Perl statement in the application script for the same reason.

Some server adapters might ask for a .psgi file, if that's the case you can just point them at your application script because it will automatically act like one if it detects the presence of a PLACK_ENV environment variable.

Plack middleware

Wrapper scripts like myapp.fcgi are a great way to separate deployment and application logic.

Rewriting

Sometimes you might have to deploy your application in a blackbox environment where you can't just change the server configuration or behind a reverse proxy that passes along additional information with X-* headers. In such cases you can use a before_dispatch hook to rewrite incoming requests.

Application embedding

From time to time you might want to reuse parts of Mojolicious applications like configuration files, database connection or helpers for other scripts, with this little mock server you can just embed them.

REAL-TIME WEB

The real-time web is a collection of technologies that include Comet (long-polling), EventSource and WebSockets, which allow content to be pushed to consumers with long-lived connections as soon as it is generated, instead of relying on the more traditional pull model. All built-in web servers use non-blocking I/O and are based on the Mojo::IOLoop reactor, which provides many very powerful features that allow real-time web applications to scale up to thousands of clients.

Backend web services

Since Mojo::UserAgent is also based on the Mojo::IOLoop reactor, it won't block the built-in web servers when used non-blocking, even for high latency backend web services.

Timers

Another primary feature of the Mojo::IOLoop reactor are timers, which can for example be used to delay rendering of a response, and unlike sleep, won't block any other requests that might be processed in parallel.

EventSource web service

HTML5 EventSource is a special form of long-polling where you can directly send DOM events from servers to clients. It is uni-directional, that means you will have to use Ajax requests for sending data from clients to servers, the advantage however is low infrastructure requirements, since it reuses the HTTP protocol for transport.

The message event will be emitted for every new log message and the finish event right after the transaction has been finished.

Streaming multipart uploads

Mojolicious contains a very sophisticated event system based on Mojo::EventEmitter, with ready-to-use events on almost all layers, and which can be combined to solve some of hardest problems in web development.

Basic authentication

Decorating followup requests

Mojo::UserAgent can automatically follow redirects, the start event allows you direct access to each transaction right after they have been initialized and before a connection gets associated with them.

Non-blocking

Mojo::UserAgent has been designed from the ground up to be non-blocking, the whole blocking API is just a simple convenience wrapper. Especially for high latency tasks like web crawling this can be extremely useful, because you can keep many parallel connections active at the same time.

Command line

Don't you hate checking huge HTML files from the command line? Thanks to the mojo get command that is about to change. You can just pick the parts that actually matter with the CSS3 selectors from Mojo::DOM and JSON Pointers from Mojo::JSON::Pointer.

Extract just the information you really need from JSON data structures.

$ mojo get http://search.twitter.com/search.json /error

This can be an invaluable tool for testing your applications.

$ ./myapp.pl get /welcome 'head > title'

HACKS

Fun hacks you might not use very often but that might come in handy some day.

Faster tests

Don't you hate waiting for make test to finally finish? In newer Perl versions you can set the HARNESS_OPTIONS environment variable to take advantage of multiple cpu cores and run tests parallel.

$ HARNESS_OPTIONS=j9 make test
...

The j9 allows 9 tests to run at the same time, which makes for example the Mojolicious test suite finish 3 times as fast on a dual core laptop!

Adding commands to Mojolicious

By now you've propably used many of the built-in commands described in Mojolicious::Commands, but did you know that you can just add new ones and that they will be picked up automatically by the command line interface?

There are many more useful methods and attributes in Mojo::Command that you can use or overload.

$ mojo spy secret
The secret of this application is "Mojolicious::Lite".
$ ./myapp.pl spy secret
The secret of this application is "secr3t".

Running code against your application

Ever thought about running a quick oneliner against your Mojolicious application to test something? Thanks to the eval command you can do just that, the application instance itself can be accessed via app.

$ mojo generate lite_app
$ ./myapp.pl eval 'say app->static->root'

The verbose option will automatically print the return value to STDOUT.

Hello World

If every byte matters this is the smallest Hello World application you can write with Mojolicious::Lite.

use Mojolicious::Lite;
any {text => 'Hello World!'};
app->start;

It works because all routes without a pattern default to / and automatic rendering kicks in even if no actual code gets executed by the router. The renderer just picks up the text value from the stash and generates a response.

Hello World oneliner

The Hello World example above can get even a little bit shorter in an ojo oneliner.