Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created January 29, 2010 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/289476 to your computer and use it in GitHub Desktop.
Save fujiwara/289476 to your computer and use it in GitHub Desktop.
# -*- 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