mojolicious behind apache

This is the only way I got an Mojolicious application to work behind an apache2 web server using mod_proxy.

Setting the base will make sure that your routes match the incoming URLs and that the right URL is used in redirects:

my $base = 'http://localhost:3000/app1';

hook 'before_dispatch' => sub {
        shift->req->url->base(Mojo::URL->new($base));
};

When using the style sheet or JavaScript helper a leading slash is important, else the path will be wrong:

%= stylesheet '/css/foo.css'

And this is the apache config snippet:

ProxyPass /app1 http://localhost:3000
ProxyPassReverse /app1 http://localhost:3000/app1

Note the missing /app1 at the end of the ProxyPass line.

The complete Mojolicious code:

#!/usr/bin/env perl
use Mojolicious::Lite;

use Time::HiRes qw/gettimeofday/;

my $base = 'http://localhost:3000/app1';

hook 'before_dispatch' => sub {
        shift->req->url->base(Mojo::URL->new($base));
};

under sub {
        shift->stash(now => join('.', gettimeofday()));
};

get '/' => sub {
        my $self = shift;
        $self->render('index');
} => 'index';

get '/foo' => sub {
        my $self = shift;
        $self->render('foo');
};

get '/redirect' => sub {
        my $self = shift;
        $self->redirect_to('foo');
};
 app->start;

__DATA__

@@ foo.html.ep
% layout 'default';
% title 'Foo';

@@ index.html.ep
% layout 'default';
% title 'index';

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
    %= stylesheet '/css/foo.css'
    <title><%= title %></title>
  </head>
  <body>
    <h1>Welcome to <%= title %></h1>
    <p>Now: <%= stash('now') %></p>
    <%= content %>
    <hr/>
    <ul>
     <li><%= link_to 'Index' => 'index' %></li>
     <li><%= link_to 'Foo' => 'foo' %></li>
     <li><%= link_to 'Redirect' => 'redirect' %>: Will redirect to foo</li>
    </ul>
  </body>
</html>

mojolicious and linuxinotify2

This is a tiny example on how to use Linux::Inotify2 from within a Mojolicious application to watch a file for modification. Note: this will not work with morbo, as no inotify will be delivered to the app. Update: it works with morbo if you use our $io = … instead of my $io = …. The reason for that is, my $io goes out of scope as app->start() does not block.

You will need these modules:

  • Linux::Inotify2 - scalable directory/file change notification

  • EV - perl interface to libev, a high performance full-featured event loop

  • AnyEvent - the DBI of event loop programming

  • And of course Mojolicious

Steps to see the magic happen:

  1. Save the code below to /tmp/i.pl

  2. Create file to be watched: touch /tmp/foo

  3. Run your app (again, morbo will not work): /tmp/i.pl daemon

  4. In another shell, update the watched file: touch /tmp/foo

This is what Mojolicious should log:

Code:

#!/usr/bin/perl

use Mojolicious::Lite;

use Linux::Inotify2;
use EV;
use AnyEvent;

sub file_changed {
        app->log->debug("file changed");
}

sub setup_notify {
        my $inotify = new Linux::Inotify2;
        $inotify->watch('/tmp/foo', IN_ALL_EVENTS, 
                \&file_changed);

        my $io = AnyEvent->io(
                fh => $inotify->{fd},
                poll => 'r',
                cb => sub { $inotify->poll }
        );

        return $io;
}

get '/' => sub { shift->render_text('index') };

our $io = setup_notify();

app->start();

hover 0 7 2 available

This release fixes an interoperability issue with the Events Manager plugin. Thanks to Marcus Sykes for helping me narrow down the problem.

hover 0 7 1 available

This release fixes undefined constant/variable warnings, an error during checks triggerd by a missing table, the has_cap deprecation warning and a bug where the activation hook may not be run.

Also Maintenance options have been added.

hover 0 7 0 available

This release fixes the hidden screen options problem, updates the included xajax to the latest stable version and disables websnapr support.

Also the script has been split up in smaller pieces, so that only what is needed is loaded.

hover 0 6 1 available

Hover 0.6.13 adds support for predefined image popups and fixes a couple of bugs.

Image Popups

In the past hover could generate popups for all HTML tags (including ) from their title attribute. To have the same popup for an image you had to enter the same text every time you inserted that image. While this worked, it was tedious, error prone and hard to change later on.

Hover 0.6.13 now let’s you define a popup for an image once and every time you use that image, it will get the same popup. Popups are assigned to Images by their absolut URL.

Bugs fixed

  • cleanup and fixed plugin activation error detection, table creation and database checks

  • correctly set database schema version on first time install

  • HTML encode aposthrophe in popups, fixing possible javascript error

hover and images in tag popups

It is possible to add images to popups generated for tags, e.g. for links. Just hover over the “wordpress” category listed on the right.

What you need to do is to partially HTML encode your, well, HTML.

In order to display the image as shown in the category, enter this in the category’s description.

&lt;img src=\"images/powered/wordpress\" /&gt;

Be sure to amend the image path to your setup/image.

imapfoo 0 3 0 available

Changes in this version of imapfoo:

* added --attach-checksum
        Generates and attaches checksum for attached files
* added --attach-random
        Attach a randomly generated file