diff options
| author | Franck Cuny <franckcuny@gmail.com> | 2016-08-04 11:45:44 -0700 |
|---|---|---|
| committer | Franck Cuny <franckcuny@gmail.com> | 2016-08-04 11:45:44 -0700 |
| commit | 585b48b6a605cb71ef99dd767880e1b7ee5bf24e (patch) | |
| tree | c65377350d12bd1e62e0bdd58458c1044541c27b /posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org | |
| parent | Use Bullet list for the index. (diff) | |
| parent | Mass convert all posts from markdown to org. (diff) | |
| download | lumberjaph-585b48b6a605cb71ef99dd767880e1b7ee5bf24e.tar.gz | |
Merge branch 'convert-to-org'
Diffstat (limited to 'posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org')
| -rw-r--r-- | posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org b/posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org new file mode 100644 index 0000000..1ffab5e --- /dev/null +++ b/posts/2009-12-21-tatsumaki-or-how-to-write-a-nice-webapp-in-less-than-two-hours.org @@ -0,0 +1,115 @@ +Until today, I had a script named "lifestream.pl". This script was +triggered via cron once every hour, to fetch various feeds from services +I use (like github, identi.ca, ...) and to process the result through a +template and dump the result in a HTML file. + +Today I was reading Tatsumaki's code and some examples (Social and +Subfeedr). Tatsumaki is a "port" tornado (a non blocking server in +Python), based on Plack and AnyEvent. I though that using this to +replace my old lifestream script would be a good way to test it. Two +hours later I have a complete webapp that works (and the code is +available here). + +The code is really simple: first, I define an handler for my HTTP +request. As I have only one things to do (display entries), the handler +is really simple: + +#+BEGIN_SRC perl + package Lifestream::Handler; + use Moose; + extends 'Tatsumaki::Handler'; + + sub get { + my $self = shift; + my %params = %{$self->request->params}; + $self->render( + 'lifestream.html', + { memes => $self->application->memes($params{page}), + services => $self->application->services + } + ); + } + 1; +#+END_SRC + +For all the get request, 2 methods are called : memes and services. The +memes get a list of memes to display on the page. The services get the +list of the various services I use (to display them on a sidebar). + +Now, as I don't want to have anymore my lifestream.pl script in cron, I +will let Tatsumaki do the polling. For this, I add a service to my app, +which is just a worker. + +#+BEGIN_SRC perl + package Lifestream::Worker; + use Moose; + extends 'Tatsumaki::Service'; + use Tatsumaki::HTTPClient; + + sub start { + my $self = shift; + my $t; + $t = AE::timer 0, 1800, sub { + scalar $t; + $self->fetch_feeds; + }; + } + + sub fetch_feeds { + my ($self, $url) = @_; + Tatsumaki::HTTPClient->new->get( + $url, + sub { + #do the fetch and parsing stuff + } + ); + } +#+END_SRC + +From now, every 60 minutes, feeds will be checked. Tatsumaki::HTTPClient +is a HTTP client based on AnyEvent::HTTP. + +Let's write the app now + +#+BEGIN_SRC perl + package Lifestream; + + use Moose; + extends "Tatsumaki::Application"; + + use Lifestream::Handler; + use Lifestream::Worker; + + sub app { + my ($class, %args) = @_; + my $self = $class->new(['/' => 'Lifestream::Handler',]); + $self->config($args{config}); + $self->add_service(Lifestream::Worker->new(config => $self->config)); + $self; + } + + sub memes { + } + + sub services { + } +#+END_SRC + +The memes and services method called from the handler are defined here. +In the app method, I "attch" the "/" path to the handler, and I add the +service. + +and to launch the app + +#+BEGIN_SRC perl + my $app = Lifestream->app(config => LoadFile($config)); + require Tatsumaki::Server; + Tatsumaki::Server->new( + port => 9999, + host => 0, + )->run($app); +#+END_SRC + +And that's it, I now have a nice webapp, with something like only 200 +LOC. I will keep playing with Tatsumaki as I have more ideas (and +probably subfeedr too). Thanks to miyagawa for all this code. |
