summaryrefslogtreecommitdiff
path: root/log/2011-02-16-less-code-more-dance.org
blob: 2384dd9653aa29c359d97df79bd7998a19df0db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: Less code more dance
date: 2011-02-16
layout: post
category: log
---

Yes, I've migrated my blog framework again. After several migrations,
there's finally a web framework that works for me. It's called Dancer,
and as redundant as this might sound; [[http://perldancer.org/][Dancer]], is an effortless web
framework.

Last year, I started writing my own mini-blog engine with several
modules from CPAN; however, the engine became complicated and robust.
Processing different tasks with more than 5 modules, I was calling
more objects than actually outputting any useful data. I've known
about Dancer since its early development, but never expected to run in
my VPS hosting, [[http://nearlyfreespeech.net][NearlyFreeSpeech]].

*How does it work*\\
Dancer works like [[http://www.sinatrarb.com/intro][Sinatra]] for Ruby. Just like Sinatra,
Dancer works with routes. I like to call them, dance moves.

*For every request, there's a dance move...*\\
A route is an HTTP request with an URL pattern. For every request, there is an associated block of code
that will handle a task in your web-app.

Simple example:\\
#+begin_src perl
#!/usr/bin/env perl
use Dancer;

get '/' => sub {
    ...do something
    ...display something
    ...call something
};

dance;
#+end_src

Working example:\\
#+begin_src perl
#!/usr/bin/env perl
use Dancer;

get '/feed/:format' => sub {
    my $db = connect_db();
    my $sth = $db->prepare($sql) or
        die $db->errstr;
    $sth->execute or die $sth->errstr;
    my $feed = create_feed(
        format  => params->{format},
        entries => _get_entries(
                $sth->fetchall_arrayref({}))
                );
    $sth->finish();
    return $feed;
};

dance;
#+end_src

To sum it up, the first example shows the structure of a route. The
second, shows a working example of my mini-blog engine feed request.
To get the working example, there has to be a request to
[[http://log.gnusosa.net/feed/rss][http://log.gnusosa.net/feed/rss]] or [[http://log.gnusosa.net/feed/atom][http://log.gnusosa.net/feed/atom]];
which ever is the case, the route will display the desired feed
format.

It took me more time studying Dancer's features and specs, than the
time it took for me to write the whole mini-blog app. In my case, my
runtime errors were caused by external modules. Debugging was an easy
task thanks to the logs generated by the Dancer core, and the error
message from Dancer's development environment. By the time, I finished
writing the main routes, there were several examples on how to deploy
the web-app in a CGI environment.

*An environment becomes a dance floor...*\\
Dancer comes with a standalone development server, that can bring up
your apps at http://127.0.0.0:3000/ . This is a handy feature that
helps to develop without a testing server, that way your focus stays
on building the web-app, instead, of focusing on transferring files.

Easy as running:\\
#+begin_src sh
$ cd App
$ perl bin/app.pl
>> Dancer server 16222 listening on http://0.0.0.0:3000
== Entering the development dance floor ...
#+end_src

Like most recent Web frameworks for Perl, Dancer supports [[http://plackperl.org/][Plack]]/[[http://plackperl.org/][PSGI]],
and work together as a built-in script that deploys the web-app in any
webserver.

*In the dance floor...*\\
Dancer is known to be fast, and minimalistic. PSGI compatible, Dancer
can be deployed in any recent webserver, including, the dominant
Apache. I've seen several instances of Dancer using Nginx, Lighttpd,
and even Apache with mod_proxy. However, many people don't recommend
robust web-apps in a CGI deployment, and mid-size apps in Fast-CGI.
Many add to this discussion that Dancer is capable of handling robust,
complicated, and resourceful web apps. I subscribe to that idea; with
a good webserver in a fast enviroment like Fast-CGI or in a lean
webserver like Nginx, Dancer can show its moves.

To get a better idea of the cases in which Dancer can be opted for,
there is the [[http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod][Dancer::Cookbook]], and the [[http://advent.perldancer.org/2010][Dancer Advent Calendar]]. For big
projects, there is always [[http://www.mojolicious.org/][Mojolicious]].

This log was made with Dancer, and is deployed in a CGI enviroment on
Apache in a VPS webserver.