Created
April 19, 2010 08:35
-
-
Save fujiwara/370848 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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