| 1 | package CPAN::Mirror::Tiny::Server; | ||
| 2 | use 5.008001; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CPAN::Mirror::Tiny; | ||
| 7 | use File::Copy (); | ||
| 8 | use File::Spec; | ||
| 9 | use Plack::App::Directory; | ||
| 10 | use Plack::Builder; | ||
| 11 | use Plack::Request; | ||
| 12 | use Plack::Runner; | ||
| 13 | |||
| 14 | sub uploader { | ||
| 15 | my ($class, %args) = @_; | ||
| 16 | my $base = $args{base} or die; | ||
| 17 | my $tempdir = $args{tempdir} || File::Temp::tempdir(CLEANUP => 1); | ||
| 18 | my $compress_index = 1; | ||
| 19 | $compress_index = $args{compress_index} if exists $args{compress_index}; | ||
| 20 | |||
| 21 | my $cpan = CPAN::Mirror::Tiny->new(base => $base, tempdir => $tempdir); | ||
| 22 | return sub { | ||
| 23 | my $env = shift; | ||
| 24 | my $req = Plack::Request->new($env); | ||
| 25 | return [404, [], ['NOT FOUND']] if $req->path_info !~ m!\A/?\z!ms; | ||
| 26 | |||
| 27 | if ($req->method eq 'POST') { | ||
| 28 | eval { | ||
| 29 | my ($module, $author); | ||
| 30 | |||
| 31 | my $tempdir = $cpan->tempdir; | ||
| 32 | if (my $upload = $req->upload('pause99_add_uri_httpupload')) { | ||
| 33 | # request from CPAN::Uploader | ||
| 34 | $module = File::Spec->catfile($tempdir->as_string, $upload->filename); | ||
| 35 | File::Copy::move $upload->tempname, $module; | ||
| 36 | $author = $req->param('HIDDENNAME'); | ||
| 37 | } else { | ||
| 38 | $module = $req->param('module'); # can be a git repo. | ||
| 39 | $author = $req->param('author') || 'VENDOR'; | ||
| 40 | } | ||
| 41 | |||
| 42 | return [404, [], ['NOT FOUND']] if !$module && !$author; | ||
| 43 | $author = uc $author; | ||
| 44 | |||
| 45 | $cpan->inject($module, {author => $author}); | ||
| 46 | $cpan->write_index(compress => $compress_index); | ||
| 47 | }; if (my $err = $@) { | ||
| 48 | warn $err . ''; | ||
| 49 | return [500, [], [$err.'']]; | ||
| 50 | } | ||
| 51 | } else { | ||
| 52 | return [405, [], ['Method Not Allowed']]; | ||
| 53 | } | ||
| 54 | |||
| 55 | return [200, [], ['OK']]; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub start { | ||
| 60 | my ($class, @argv) = @_; | ||
| 61 | my $runner = Plack::Runner->new; | ||
| 62 | $runner->parse_options(@argv); | ||
| 63 | if ($runner->{help}) { | ||
| 64 | require Pod::Usage; | ||
| 65 | Pod::Usage::pod2usage(0); | ||
| 66 | } | ||
| 67 | if ($runner->{version}) { | ||
| 68 | my $c = "CPAN::Mirror::Tiny"; | ||
| 69 | printf "%s %s\n", $c, $c->VERSION; | ||
| 70 | exit; | ||
| 71 | } | ||
| 72 | my $base = shift @{ $runner->{argv} || []} or die "Missing base directory\n"; | ||
| 73 | my $app = builder { | ||
| 74 | mount "/upload" => $class->uploader(base => $base); | ||
| 75 | mount "/" => Plack::App::Directory->new(root => $base)->to_app; | ||
| 76 | }; | ||
| 77 | $runner->run($app); | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | __END__ |