Created
January 29, 2010 05:22
-
-
Save fujiwara/289476 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode:perl -*- | |
use strict; | |
use warnings; | |
use feature qw/ :5.10 /; | |
use File::Temp qw/ tempfile /; | |
use IO::File::WithPath; | |
my ($client) = grep { -e $_ } qw( | |
/usr/bin/emacsclient.emacs-snapshot | |
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient | |
); | |
die "client not found" unless $client; | |
sub { | |
my $env = shift; | |
my ($status, $headers, $body) | |
= ( 200, [ "Conetnt-Type" => "text/plain" ], undef ); | |
given ($env->{PATH_INFO}) { | |
when (qr{^/status}) { | |
$body = [ "OK" ]; | |
} | |
when (qr{^/edit}) { | |
# HTTP body をテンポラリファイルに書き込む | |
my ($tmpfh, $tmpfile) = tempfile(); | |
my $buf; | |
print $tmpfh $buf while read $env->{"psgi.input"}, $buf, 4096; | |
close $tmpfh; | |
# emacsclient 起動 | |
system($client, $tmpfile) != 0 | |
and warn $!; | |
# テンポラリファイルの内容を送信 | |
push @$headers, [ "Content-Length" => -s $tmpfile ]; | |
$body = IO::File::WithPath->new($tmpfile); | |
unlink $tmpfile; | |
} | |
default { | |
$status = 404; | |
$body = [ "NotFound" ]; | |
} | |
} | |
return [ $status, $headers, $body ]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment