My blog is where I keep my notes. Most of the time updates will happen in bursts of activity. All links go offsite to blog.leecarmichael.com at Blogger since Google has a very good service.
16:27:00 28-Mar-2013
This just came up on Dancer's mailing list and I've been sitting on this code (and post) for a few months since the project I was working on didn't need it.
Basically Dancer's Template Toolkit view requires you completely alter it or use the default setting. This is pain if you just want to add to it but not have to manage all the changes.
I created a new TT view class, to get inside the TT init and add to the INCLUDE_PATH instead of replace it. The code was a bit involved and I extended it a bit by adding a customization flag in each environment (if needed).
Here is the code and config that i used to do this
I don't recall all the other issues that i ran into while doing this. It was over 6 months ago but post questions if you have them
__END__08:40:00 25-Jan-2013
I was able to find complete documentation on running dancer with nginx using FastCGI. In the dancer deployment it discusses using Apache and fastcgi or nginx with proxy. Often, using nginx as proxy to starman(or some other plack webserver) is probably the right thing. But sometimes using fastcgi is good idea too :) I'm running some benchmarks but it seems like memory usage is a bit smaller with FCGI but I still need to do more digging on this.
Lets get all the parts install and then configure them
Note: These parts will be very dependent on your system. I'm assuming a debian/ubuntu system below.
First you need to have nginx and dancer installed. For nginx i would recommend doing something like:
apt-get install nginx
or building and install from source.
Next, install need Perl modules:
cpanm + Dancer's Makefile.PL
I like to install using Dancer's app generated Makefile.PL along with cpanm to manage Perl dependencies.
Setup your Makefile.PL:
use strict;
use warnings;
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'AppName',
AUTHOR => q{your name},
VERSION_FROM => 'lib/ExApp.pm',
ABSTRACT => 'example dancer app',
($ExtUtils::MakeMaker::VERSION >= 6.3002
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer' => 1.3095,
# required to run in fcgi mode
'Plack' => 0,
'FCGI' => 0,
'FCGI::ProcManager' => 0,
# application specific
'Template' => 0,
'Dancer::Plugin::Database' => 0,
'DateTime' => 0,
'DateTime::Format::MySQL' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'example-app-name-*' },
);
You can do: cpanm --installdeps .
Other options....
cpanm Dancer Plack FCGI FCGI::ProcManagerOr
cpan install Dancer Plack FCGI FCGI::ProcManager
Now, lets configure nginx nginx.conf. It isn't a large change from http://wiki.nginx.org/NginxFcgiExample. There are three main changes below.
Example Configuration:
server {
listen 80;
server_name fastcgi-example.leecarmichael.com;
root /home/user/dancer_app/public;
access_log logs/fcgi-example.access.log main;
# serve static content from nginx not dancer
location = /favicon.ico {
}
location /images {
}
location /js {
}
location /css {
}
# serve all other stuff from appserver
location / {
set $script "";
set $path_info $uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $request_filename;
# plack FCGI handler requires these changes
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# can use port as well
fastcgi_pass unix:/tmp/myapp-fcgi.sock;
}
}
Start or restart nginx.
On debian: /etc/init.d/nginx restart
Startup your app server (make sure you'ave changed to /home/user/dancer_app):
plackup -E development -s FCGI -a bin/app.pl -S /tmp/myapp-fcgi.sock -D
See plackup for all the options. I added -D and usually add --error-log but a start up script is probably a better long term choice
After writing all this, i stumbled across a pretty helpful reference in the Plack docs on Plack::Handler::FCGI which replicates the fast-cgi nginx.conf above.
__END__
10:53:00 07-Oct-2012
Dancer does lots of great things. It has a nice clean way to define routes to handle AJAX routes using the plugin Dancer::Plugin::Ajax.
ajax routes are defined in a clear way:
ajax '/stuff' => sub {
# do work and return
};
This plugin technique allows for clear way to separate between ajax and other types of actions for the same route.
ajax '/stuff' => sub {
# do ajax-y stuff here
};
get '/stuff' => sub {
# handle html response
};
Unfortunately, the one negative with Dancer::Plugin::Ajax is that is assumes all responses will be XML.
A quick fix is to manually set the content type in each ajax handler.
or add set it as a general option in your main before hook or in each prefix route handler like:
ajax '/stuff' => sub {
content_type('application/json');
# do work
};
package WebApp;
hook before => sub {
if ( request->is_ajax ) {
content_type('application/json');
}
};
Both of these solutions feel kind of clunky due to the level of duplication. Our app returns JSON or HTML snippets and never uses XML. This redundancy in code led me to creating a patch for Dancer that allows default Ajax content_type to be set in the config file. (see issue 840).
Config Example:
__END__
plugins:
ajax:
content_type: 'application/json'
15:27:00 25-Sep-2012
$(function() {
$('.blueberry').blueberry({hoverpause: true});
});