Permalink
Browse files

initial commit.

  • Loading branch information...
0 parents commit 372ff0cc14abb56e990de4c7e03646f4ae862379 @yusukebe committed Apr 20, 2012
@@ -0,0 +1,17 @@
+.*
+*~
+!.gitignore
+!.perltidyrc
+blib
+pm_to_blib
+Makefile*
+!Makefile.PL
+MANIFEST*
+!MANIFEST.SKIP
+*META.*
+nopaste.conf
+log/*
+.DS_Store
+._*
+.Spotlight-V100
+.Trashes
@@ -0,0 +1,3 @@
+package Nopaste::DB;
+use DBIx::Skinny;
+1;
@@ -0,0 +1,35 @@
+package Nopaste::DB::Schema;
+use DBIx::Skinny::Schema;
+use DateTime;
+use DateTime::Format::Strptime;
+use DateTime::Format::MySQL;
+
+sub pre_insert_hook {
+ my ( $class, $args ) = @_;
+ $args->{created_on} = DateTime->now( time_zone => 'Asia/Tokyo' );
+}
+
+install_inflate_rule '^.+_on$' => callback {
+ inflate {
+ my $value = shift;
+ my $dt = DateTime::Format::Strptime->new(
+ pattern => '%Y-%m-%d %H:%M:%S',
+ time_zone => container('timezone'),
+ )->parse_datetime($value);
+ return DateTime->from_object( object => $dt );
+ };
+ deflate {
+ my $value = shift;
+ return DateTime::Format::MySQL->format_datetime($value);
+ };
+};
+
+install_table entry => schema {
+ pk 'id';
+ columns qw/id title body created_on/;
+ trigger pre_insert => \&pre_insert_hook;
+};
+
+install_utf8_columns qw/title body/;
+
+1;
@@ -0,0 +1,15 @@
+package Nopaste::Web;
+use Mojo::Base 'Mojolicious';
+use Nopaste::DB;
+
+sub startup {
+ my $self = shift;
+ my $config = $self->plugin('Config', { file => 'nopaste.conf' });
+ $self->attr( db => sub { Nopaste::DB->new( $config->{db} ) } );
+ my $r = $self->routes;
+ $r->get('/')->to('root#index');
+ $r->post('/')->to('root#post');
+ $r->route('/paste/:id')->to('root#entry');
+}
+
+1;
@@ -0,0 +1,58 @@
+package Nopaste::Web::Root;
+use Mojo::Base 'Mojolicious::Controller';
+use FormValidator::Lite;
+FormValidator::Lite->load_constraints(qw/Japanese/);
+use HTML::FillInForm::Lite;
+use Data::GUID::URLSafe;
+use Text::VimColor;
+use Encode;
+use utf8;
+
+sub index {
+ my $self = shift;
+ $self->stash->{error_messages} = undef;
+}
+
+sub post {
+ my $self = shift;
+ my $validator = FormValidator::Lite->new($self->req);
+ $validator->set_message(
+ 'title.not_null' => 'Title is Empty',
+ 'body.not_null' => 'Body is Empty',
+ );
+ my $res = $validator->check(
+ title => [qw/NOT_NULL/],
+ body => [qw/NOT_NULL/],
+ );
+ if ($validator->has_error) {
+ my @messages = $validator->get_error_messages;
+ $self->stash->{error_messages} = \@messages;
+ my $html = $self->render_partial('root/index')->to_string;
+ return $self->render_text(
+ HTML::FillInForm::Lite->fill(\$html, $self->req->params),
+ format => 'html',
+ );
+ }
+
+ my $entry = $self->app->db->insert('entry',{
+ id => Data::GUID->new->as_base64_urlsafe,
+ title => $self->req->param('title'),
+ body => $self->req->param('body'),
+ });
+
+ $self->redirect_to('/paste/' . $entry->id );
+}
+
+sub entry {
+ my $self = shift;
+ my $entry = $self->app->db->single('entry',{ id => $self->stash->{id} });
+ unless( $entry ){
+ $self->render_not_found;
+ }
+
+ my $syntax = Text::VimColor->new( filetype=> 'perl', string => encode_utf8($entry->body) );
+ $self->stash->{code} = decode_utf8($syntax->html);
+ $self->stash->{entry} = $entry;
+}
+
+1;
@@ -0,0 +1,7 @@
++{
+ db => {
+ dsn => 'dbi:mysql:nopaste',
+ username => 'root',
+ password => undef,
+ }
+};
Oops, something went wrong.

0 comments on commit 372ff0c

Please sign in to comment.