Skip to content

Instantly share code, notes, and snippets.

@punytan
Created July 9, 2012 07:51
Show Gist options
  • Save punytan/3074869 to your computer and use it in GitHub Desktop.
Save punytan/3074869 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Plack::Request;
use Plack::MIME;
use Plack::App::Directory;
use FindBin;
my $java_bin = `which java` =~ s/[\n\r]//gr
or die 'Install Java :(';
my $graphviz_dot = `which dot` =~ s/[\n\r]//gr
or warn 'Running without graphviz';
my $plant_uml_jar = "$FindBin::Bin/plantuml.jar";
unless (-f $plant_uml_jar) {
print "Initializing...";
system qq(wget -O $plant_uml_jar http://jaist.dl.sourceforge.net/project/plantuml/plantuml.jar);
exit;
}
sub {
my $req = Plack::Request->new(shift);
my $path = ($req->path_info =~ s/[\/\\\0]//gr);
if ($path eq '/' || $path eq 'favicon.ico' || not $path) {
return Plack::App::Directory->new->to_app->($req->env);
}
if ($path =~ /uml$/ && $req->query_parameters->{render}) {
my $cmd = join " ",
"env",
"JAVA_BIN='$java_bin'",
"GRAPHVIZ_DOT='$graphviz_dot'",
"$java_bin -jar $plant_uml_jar",
"-SdefaultFontName='Hiragino Kaku Gothic Pro W3'",
"-charset utf8",
"'$path'";
system $cmd;
$path =~ s/uml$/png/;
}
my $content_type = Plack::MIME->mime_type($path) || 'text/plain; charset=UTF-8';
open my $image, '<', $path or die $!;
return [200, ['Content-Type' => $content_type], $image];
};

Optional

If you need graphviz, install Xcode and dispatch following command.

brew update
brew install gts
brew install graphviz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment