Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active October 12, 2018 21:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jberger/6884022 to your computer and use it in GitHub Desktop.
Save jberger/6884022 to your computer and use it in GitHub Desktop.
#!/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
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment