Hi! Please consider following me on twitter: @hanekomu.
2009年06月11日
Sending DBI trace output to FirePHP with HTTP::Engine
As promised last time, here is a complete example of how to send DBI trace output to FirePHP in an HTTP::Engine demo web application.
First we create a very small example SQLite database. Let's model a company
that has two departments with two employees each. Create a file called
create.sql and enter the following SQL commands:
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT,
department_id INTEGER
);
INSERT INTO departments (id, name) VALUES (1, 'publishing');
INSERT INTO departments (id, name) VALUES (2, 'accounting');
INSERT INTO employees (id, name, department_id)
VALUES (1, 'Yamamura Masayoshi', 1);
INSERT INTO employees (id, name, department_id)
VALUES (2, 'Ueda Akira', 1);
INSERT INTO employees (id, name, department_id)
VALUES (3, 'Matsumoto Daichi', 2);
INSERT INTO employees (id, name, department_id)
VALUES (4, 'Yasuda Ryota', 2);
Now we create the database itself:
sqlite3 kaisha.db <create.sql
The web application is pretty simple as well. We create an
HTTP::Engine object and run it; the handler connects to the
database, runs a simple query, creates an HTML table from the results and sends
it in a response back to the engine.
#!/usr/bin/env perl use strict; use warnings; use DBI; use HTTP::Engine; use HTTP::Engine::FirePHP; my $engine = HTTP::Engine->new( interface => { module => 'ServerSimple', args => { host => 'localhost', port => '1984', }, request_handler => \&handler, }, )->run; sub handler { my $req = shift; my $dbh = DBI->connect('dbi:SQLite:dbname=kaisha.db', '', ''); my $res = HTTP::Engine::Response->new; $dbh->trace(2, $res->get_fire_php_fh); my $r = $dbh->selectall_arrayref(' SELECT E.name, D.name FROM employees E, departments D WHERE D.id = E.department_id '); $dbh->disconnect; my $body = "<table>\n" . (join "\n" => map { sprintf "<tr><td>%s</td><td>%s</td></tr>\n", @$_ } @$r ) . "</table>\n"; $res->status(200); $res->body($body); $res; }
The relevant command to send DBI trace output to FirePHP is:
$dbh->trace(2, $res->get_fire_php_fh);
In version 0.02, HTTP::Engine::FirePHP gives
HTTP::Engine::Response the method get_fire_php_fh,
which will return a filehandle that has been opened to a PerlIO::via::ToFirePHP layer, so you don't even need to do
that manually anymore.
With this one line, DBI knows that it should trace all calls and send the trace output to the filehandle. The trace output will automatically be put into the HTTP response headers so FirePHP can display them. Here is what the Firebug console looks like when we make a request to the server:
Tags: DBI, FirePHP, HTTP::Engine, web.
posted at: 14:54 | path: /dev | permalink | 0 comments | 0 trackbacks
2009年06月07日
Logging to Firefox with HTTP::Engine::FirePHP
I have released HTTP::Engine::FirePHP on CPAN; the development version is on Github.
If you are developing a web application and don't want to or can't check the error log, the traditional way is to include debug messages in the HTML page. However, this messes up the layout and mixes content with logging; the two really need to be separate.
FirePHP is a Firebug plugin which enables you to log to your Firebug Console by sending certain HTTP headers in the HTTP response. FirePHP is not just useful for PHP, though; any server-side application that can manipulate HTTP headers can log to Firebug.
The FirePHP response headers use the Wildfire protocol. The CPAN module FirePHP::Dispatcher can generate these headers.
This module then integrates FirePHP::Dispatcher with HTTP::Engine. By simply using this module,
HTTP::Engine::Response gets a fire_php() accessor
through which you can log to FirePHP.
Here is an example:
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use HTTP::Engine; use HTTP::Engine::Response; use HTTP::Engine::FirePHP; HTTP::Engine->new( interface => { module => 'ServerSimple', args => { host => 'localhost', port => '10012', }, request_handler => sub { my $req = shift; my $res = HTTP::Engine::Response->new(body => 'Hello'); $res->fire_php->info('Hello from HTTP::Engine'); $res->fire_php->warn(sprintf 'path was %s', $req->uri->path); $res->fire_php->log(Dumper $req); $res; }, }, )->run;
When you load the response into Firefox, open the Firebug Console and you will find the logged messages there.
When you restart the HTTP::Engine-based server, be sure to do a
shift-reload of the relevant page in Firefox; this ensures that headers aren't
cached. If you don't do this, you might see remnant headers from previous
responses.
Tags: FirePHP, HTTP::Engine, web.
posted at: 14:54 | path: /dev | permalink | 0 comments | 0 trackbacks
2008年11月25日
HTTP::Engine Conference #1 and Shibuya.pm #10
This week sees two conferences in Tokyo — they both will only last for a few hours. The first is HTTP::Engine conference #1, or HEcon1 for short. Only about 20 to 30 attendees are expected, so it's more like a tech meet with a common theme. The talks will of course be in Japanese, but you will probably get the gist from the slides. They will be streamed live over ustream and will probably be uploaded to Nico Nico Douga.
The second conference is the Shibuya.pm #10 meeting, and it is rather larger in scale. It too will only last for a few hours, but more than 100 attendees are expected, and there will be remote venues in Osaka/Kyoto and in Fukuoka. These talks will be streamed as well.
Both conferences will start in the evening in Tokyo, so they will be nice for me to watch around lunchtime at work.
Tags: Asia, conferences, HTTP::Engine.
posted at: 20:16 | path: /misc | permalink | 0 comments | 0 trackbacks

