Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created April 19, 2010 08:35
Show Gist options
  • Save fujiwara/370848 to your computer and use it in GitHub Desktop.
Save fujiwara/370848 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Hitagi;
use File::Temp qw/ tempfile /;
use IO::File::WithPath;
my ($client) = grep { -e $_ } qw(
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient
/usr/bin/emacsclient.emacs-snapshot
);
die "client not found" unless $client;
get "/status" => sub {
my $res = res(200);
$res->content_type("text/plain");
$res->body("OK");
$res->finalize;
};
post "/edit" => sub {
my $req = shift;
# Request body をテンポラリファイルに書き込む
my ($tmpfh, $tmpfile) = tempfile();
while ( read $req->body, my $buf, 4096 ) {
print $tmpfh $buf;
}
close $tmpfh;
# emacsclient 起動
system($client, $tmpfile) != 0
and warn $!;
# テンポラリファイルの内容を送信
my $res = res(200);
$res->content_type("text/plain");
$res->content_length( -s $tmpfile );
my $body = IO::File::WithPath->new($tmpfile);
unlink $tmpfile;
$res->body($body);
$res->finalize;
};
star;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment