Created
July 4, 2010 14:07
-
-
Save toritori0318/463484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use Data::UUID; | |
use DBI; | |
use Time::Piece; | |
use Mojolicious::Lite; # 'app', 'post', 'get', 'shagadelic' is exported | |
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__ | |
@@ index.html.ep | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
<html> | |
<head> | |
<title>nopaste</title> | |
<link rel="stylesheet" type="text/css" href="screen.css" /> | |
</head> | |
<body> | |
<div class="container"> | |
<h1><a href="<%= url_for("index") %>">Yet Another nopaste</a></h1> | |
<%= include 'form' %> | |
</div> | |
</body> | |
</html> | |
@@ form.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