Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Created July 3, 2010 16:25
Show Gist options
  • Save toritori0318/462671 to your computer and use it in GitHub Desktop.
Save toritori0318/462671 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Data::UUID;
use DBI;
use Time::Piece;
use Mojolicious::Lite;
use Mojo::ByteStream 'b';
# mode
$ENV{MOJO_MODE}='production';
my $dbh = DBI->connect(
"dbi:SQLite:dbname=/tmp/nopaste.sqlite",
'',
'',
{RaiseError => 1, AutoCommit => 1 }
) or die $DBI::errstr;
eval{
$dbh->do(q{CREATE TABLE entry ( id varchar, body text, created_at text )});
};
my $uuid_gen = Data::UUID->new;
get '/' => 'index';
post '/post' => sub {
my $self = shift;
my $body = $self->param('body') or $self->redirect_to( 'index' );
my $uuid = $uuid_gen->create_str;
$dbh->do(q{insert into entry ( id , body, created_at) values (?, ?, ?)}, {}, $uuid, $body, localtime->datetime);
$self->redirect_to('entry', entry_id => $uuid);
} => 'post';
get '/entry/:entry_id' => sub {
my ( $self ) = @_;
my $entry_id = $self->stash('entry_id');
my $entry = $dbh->selectrow_hashref('SELECT body FROM entry WHERE id = ?', {}, $entry_id);
$self->render( 'entry', body => $entry->{body} );
} => 'entry';
app->start;
# cgiで使う場合はこっち
#app->start('cgi');
__DATA__
@@ layout.html.ep
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>nopaste</title>
<link rel="stylesheet" type="text/css" href="<%= url_for("index") %>static/screen.css" />
</head>
<body>
<div class="container">
<h1><a href="<%= url_for("index") %>">Yet Another nopaste</a></h1>
<%= content %>
</div>
</body>
</html>
@@ index.html.ep
<form action="<%= url_for("post") %>" method="post">
<p><textarea name="body" cols="60" rows="10"></textarea></p>
<p><input type="submit" value="no paste" /><p>
</form>
@@ entry.html.ep
<pre>
<%= $body %>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment