Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Created August 8, 2010 06:40
Show Gist options
  • Save toritori0318/513680 to your computer and use it in GitHub Desktop.
Save toritori0318/513680 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; # '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, title text, body text, created_at text )});
};
my $uuid_gen = Data::UUID->new;
get '/' => sub {
my $self = shift;
my $entrys = $dbh->selectall_arrayref(q|SELECT id, created_at, title FROM entry order by created_at desc limit 30|, +{ Slice => +{} });
$self->render( 'index', entrys => $entrys );
} => 'index';
post '/post' => sub {
my $self = shift;
my $title = $self->param('title') ;
my $body = $self->param('body') or $self->redirect_to( 'index' );
my $uuid = $uuid_gen->create_str;
$dbh->do(q{insert into entry ( id , title, body, created_at) values (?, ?, ?, ?)}, {}, $uuid, $title, $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>
<% if($entrys && scalar @$entrys > 0) { %>
<div class="list">
<table>
<tr><th>date</th><th>title</th><th>link</th></tr>
<% foreach my $entry (@$entrys) { %>
<tr>
<td><%= $entry->{created_at} %></td>
<td><%= $entry->{title} ? $entry->{title} : "(no title)" %></td>
<td><a href=<%= url_for("entry") %>/<%= $entry->{id} %>><%= $entry->{id} %></a></td>
</tr>
<% } %>
</table>
</div>
<% } %>
</body>
</html>
@@ form.html.ep
<form action="<%= url_for("post") %>" method="post">
<p>title <input type="text" name="title" /></p>
<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