Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sukria/Dancer
Browse files Browse the repository at this point in the history
  • Loading branch information
xsawyerx committed Nov 18, 2010
2 parents b0ddcc6 + ed93c89 commit b35c176
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 76 deletions.
48 changes: 34 additions & 14 deletions lib/Dancer/Config.pm
Expand Up @@ -18,6 +18,7 @@ my $SETTINGS = {
# user defined mime types
mime_types => {},
};

sub settings {$SETTINGS}

my $setters = {
Expand Down Expand Up @@ -76,27 +77,48 @@ my $normalizers = {

sub normalize_setting {
my ($class, $setting, $value) = @_;
$value = $normalizers->{$setting}->($setting, $value)
if exists $normalizers->{$setting};

$value = $normalizers->{$setting}->($setting, $value)
if exists $normalizers->{$setting};

return $value;
}

# public accessor for get/set
sub setting {
my ($setting, $value) = @_;

if (@_ == 2) {
$value = _set_setting($setting, $value);
_trigger_hooks($setting, $value);
return $value;
}
else {
return _get_setting($setting);
}
}

sub _trigger_hooks {
my ($setting, $value) = @_;

$setters->{$setting}->(@_) if defined $setters->{$setting};
}

sub _set_setting {
my ($setting, $value) = @_;

return unless @_ == 2;

# normalize the value if needed
$value = Dancer::Config->normalize_setting($setting, $value)
if @_ == 2;
$value = Dancer::Config->normalize_setting($setting, $value);
$SETTINGS->{$setting} = $value;
return $value;
}

# run the hook if setter
$setters->{$setting}->(@_)
if (@_ == 2) && defined $setters->{$setting};
sub _get_setting {
my $setting = shift;

# setter/getter
(@_ == 2)
? $SETTINGS->{$setting} = $value
: $SETTINGS->{$setting};
return $SETTINGS->{$setting};
}

sub mime_types {
Expand Down Expand Up @@ -149,13 +171,11 @@ sub load_settings_from_yaml {

my $config = YAML::LoadFile($file)
or confess "Unable to parse the configuration file: $file";

setting($_, $config->{$_}) for (keys %{ $config });
_set_setting($_, $config->{$_}) for (keys %{$config});

return scalar(keys %$config);
}


sub load_default_settings {
$SETTINGS->{server} ||= $ENV{DANCER_SERVER} || '0.0.0.0';
$SETTINGS->{port} ||= $ENV{DANCER_PORT} || '3000';
Expand Down
75 changes: 14 additions & 61 deletions lib/Dancer/Deployment.pod
Expand Up @@ -397,63 +397,19 @@ RewriteRules in a .htaccess file directly within the application's directory,
which lets you add a new application without changing the Apache configuration.


=head2 Running on lighttpd
=head2 Running on lighttpd (CGI)

It is very easy to configure lighttpd to deploy Dancer applications. Multiple
applications can be deployed on the same server by setting up virtual hosts.
You can configure name based virtual hosts with conditional blocks that start
with:
To run as a CGI app on lighttpd, just create a soft link to the dispatch.cgi
script (created when you run dancer -a MyApp) inside your system's cgi-bin
folder. Make sure mod_cgi is enabled.

$HTTP["host"] == "myapp.example.com" { ...
ln -s /path/to/MyApp/public/dispatch.cgi /usr/lib/cgi-bin/mycoolapp.cgi

Or you can configure port based virtual hosts:
=head2 Running on lighttpd (FastCGI)

$SERVER["socket"] == ":5000" { ...
Make sure mod_fcgi is enabled. You also must have L<FCGI> installed.

There is no need to explicitly set the document root for the following examples.
It will be set to the public folder implicitly, since that is where the dispatch
scripts live. If you would like your document root to be elsewhere, you can add
this line inside of the conditional block:

server.document-root = "/somewhere/else/"


=head3 Running on lighttpd (CGI)

Make sure mod_cgi is enabled. This example uses a name based virtual host.

cgi.assign = ( ".cgi" => "" )
$HTTP["host"] == "myapp.example.com" {
alias.url += ("" => "/path/to/myapp/public/dispatch.cgi")
}


=head3 Running on lighttpd (FastCGI)

Make sure mod_fcgi is enabled. You also must install the FCGI module from
CPAN.

=head4 Script

This example uses a script based virtual host.

Your lighttpd configuration:

$HTTP["url"] == "^/app" {
fastcgi.server += (
".fcgi" => ((
"bin-path" => "/path/to/myapp/public/dispatch.fcgi",
"socket" => "/tmp/fcgi.sock",
))
)
alias.url += ("" => "/path/to/myapp/public/dispatch.fcgi")
}

=head4 TCP/IP

This example uses TCP/IP.

Your lighttpd configuration:
This example configuration uses TCP/IP:

$HTTP["url"] == "^/app" {
fastcgi.server += (
Expand All @@ -463,19 +419,15 @@ Your lighttpd configuration:
"port" => "5000",
"check-local" => "disable",
)
)
)
}
)
)
}

Launch your application
Launch your application:

plackup -s FCGI --port 8080 bin/app.pl

=head4 Unix socket

This example uses a socket.

Your lighttpd configuration:
This example configuration uses a socket:

$HTTP["url"] =~ "^/app" {
fastcgi.server += (
Expand All @@ -491,3 +443,4 @@ Your lighttpd configuration:
Launch your application:

plackup -s FCGI --listen /tmp/fcgi.sock bin/app.pl

19 changes: 18 additions & 1 deletion t/01_config/01_settings.t
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 5, import => ['!pass'];
use Test::More tests => 11, import => ['!pass'];

use Dancer ':syntax';

Expand All @@ -15,3 +15,20 @@ is(setting('foo'), 42, 'new value has been set');

# test the alias 'set'
is(set(bar => 43), 43, "setting bar with set");

# testing private interface
is( Dancer::Config::_set_setting( bar => 42 ),
42, 'setting bar with private interface' );
is( Dancer::Config::_get_setting('bar'),
42, 'getting bar with private interface' );

SKIP: {
skip "YAML and JSON are needed to run this test", 4
unless ( Dancer::ModuleLoader->load('YAML')
&& Dancer::ModuleLoader->load('JSON') );

ok my $serializer = Dancer::Config::_trigger_hooks( 'serializer', 'YAML' );
isa_ok( $serializer, 'Dancer::Serializer::YAML' );
ok $serializer = Dancer::Config::_trigger_hooks( 'serializer', 'JSON' );
isa_ok( $serializer, 'Dancer::Serializer::JSON' );
}

0 comments on commit b35c176

Please sign in to comment.