public
Last active

  • Download Gist
blocking_paste.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#!/usr/bin/env perl
 
use Mojolicious::Lite;
use Mango;
use Mango::BSON 'bson_oid';
 
helper mango => sub { state $mango = Mango->new($ENV{PASTEDB}) };
helper pastes => sub { shift->mango->db->collection('pastes') };
 
get '/' => 'submit';
 
post '/' => sub {
my $self = shift;
my $title = $self->param('title') || 'Untitled';
my $content = $self->param('content')
or return $self->redirect_to('/');
my $doc = {
title => $title,
content => $content,
};
my $oid = $self->pastes->save($doc);
$self->redirect_to( show => id => "$oid" );
};
 
get '/:id' => sub {
my $self = shift;
my $id = bson_oid $self->stash('id');
my $doc = $self->pastes->find_one({ _id => $id })
or return $self->redirect_to('/');
$self->stash( doc => $doc );
} => 'show';
 
app->start;
 
__DATA__
 
@@ layouts/basic.html.ep
 
<!DOCTYPE html>
<html>
<head>
<title><%= title =%></title>
%= stylesheet '//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css'
</head>
<body>
<div class="container">
<%= content =%>
</div>
</body>
</html>
 
@@ show.html.ep
 
% title $doc->{title};
% layout 'basic';
 
%= stylesheet begin
pre.prettyprint {
background-color:inherit;
border:none;
}
% end
%= tag h1 => $doc->{title}
%= tag div => class => 'well' => begin
%= tag pre => class => 'prettyprint' => begin
<%= $doc->{content} =%>
% end
% end
%= javascript 'https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js'
 
@@ submit.html.ep
 
% title 'Paste your content';
% layout 'basic';
 
%= form_for '/' => role => form => method => POST => begin
%= tag div => class => 'form-group' => begin
%= tag label => for => 'title' => 'Title'
%= text_field 'title', class => 'form-control'
% end
%= tag div => class => 'form-group' => begin
%= tag label => for => 'content' => 'Paste Content'
%= text_area 'content', class => 'form-control'
% end
%= submit_button 'Paste' => class => 'btn btn-primary'
% end
nonblocking_paste.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#!/usr/bin/env perl
 
use Mojolicious::Lite;
use Mango;
use Mango::BSON 'bson_oid';
 
helper mango => sub { state $mango = Mango->new($ENV{PASTEDB}) };
helper pastes => sub { shift->mango->db->collection('pastes') };
 
get '/' => 'submit';
 
post '/' => sub {
my $self = shift;
my $title = $self->param('title') || 'Untitled';
my $content = $self->param('content')
or return $self->redirect_to('/');
my $doc = {
title => $title,
content => $content,
};
$self->render_later;
$self->pastes->save($doc, sub {
my ($coll, $err, $oid) = @_;
$self->redirect_to( show => id => "$oid" );
});
};
 
get '/:id' => sub {
my $self = shift;
my $id = bson_oid $self->stash('id');
$self->render_later;
$self->pastes->find_one({ _id => $id }, sub {
my ($coll, $err, $doc) = @_;
return $self->redirect_to('/') if ( $err or not $doc );
$self->render( show => doc => $doc );
});
} => 'show';
 
app->start;
 
__DATA__
 
@@ layouts/basic.html.ep
 
<!DOCTYPE html>
<html>
<head>
<title><%= title =%></title>
%= stylesheet '//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css'
</head>
<body>
<div class="container">
<%= content =%>
</div>
</body>
</html>
 
@@ show.html.ep
 
% title $doc->{title};
% layout 'basic';
 
%= stylesheet begin
pre.prettyprint {
background-color:inherit;
border:none;
}
% end
%= tag h1 => $doc->{title}
%= tag div => class => 'well' => begin
%= tag pre => class => 'prettyprint' => begin
<%= $doc->{content} =%>
% end
% end
%= javascript 'https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js'
 
@@ submit.html.ep
 
% title 'Paste your content';
% layout 'basic';
 
%= form_for '/' => role => form => method => POST => begin
%= tag div => class => 'form-group' => begin
%= tag label => for => 'title' => 'Title'
%= text_field 'title', class => 'form-control'
% end
%= tag div => class => 'form-group' => begin
%= tag label => for => 'content' => 'Paste Content'
%= text_area 'content', class => 'form-control'
% end
%= submit_button 'Paste' => class => 'btn btn-primary'
% end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.