Skip to content

Instantly share code, notes, and snippets.

@valitnon
Forked from jberger/blocking_paste.pl
Created October 12, 2018 21:23
Show Gist options
  • Select an option

  • Save valitnon/b1c0ae82d163cc1b9a085afa5e39c78a to your computer and use it in GitHub Desktop.

Select an option

Save valitnon/b1c0ae82d163cc1b9a085afa5e39c78a to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mango;
helper mango => sub { state $mango = Mango->new($ENV{PASTEDB}) };
helper pastes => sub { shift->mango->db->collection('pastes') };
helper count => sub { shift->pastes->find->count };
helper next => sub {
state $count = shift->count || 0;
return $count++;
};
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 = {
_id => $self->next,
title => $title,
content => $content,
};
my $oid = $self->pastes->save($doc);
$self->redirect_to( show => id => "$oid" );
};
get '/:id' => sub {
my $self = shift;
my $id = $self->stash('id') + 0;
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';
%= tag h1 => $doc->{title}
%= tag div => class => 'well' => begin
%= tag code => class => 'prettyprint', style => 'background-color:inherit;' => 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
%= tag button => type => 'submit', class=>'btn btn-primary' => 'Paste'
% end
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mango;
helper mango => sub { state $mango = Mango->new($ENV{PASTEDB}) };
helper pastes => sub { shift->mango->db->collection('pastes') };
helper count => sub { shift->pastes->find->count };
helper next => sub {
state $count = shift->count || 0;
return $count++;
};
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 = {
_id => $self->next,
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 = $self->stash('id') + 0;
$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 );
});
};
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';
%= tag h1 => $doc->{title}
%= tag div => class => 'well' => begin
%= tag code => class => 'prettyprint', style => 'background-color:inherit;' => 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
%= tag button => type => 'submit', class=>'btn btn-primary' => 'Paste'
% end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment