Skip to content

Commit

Permalink
Sample Dancer2 app
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Mar 29, 2015
1 parent 11db1f9 commit bbb8c65
Show file tree
Hide file tree
Showing 29 changed files with 1,065 additions and 0 deletions.
23 changes: 23 additions & 0 deletions code/dancer2/BookWeb/MANIFEST
@@ -0,0 +1,23 @@
MANIFEST
cpanfile
Makefile.PL
MANIFEST.SKIP
config.yml
public/dispatch.fcgi
public/500.html
public/dispatch.cgi
public/favicon.ico
public/404.html
public/images/perldancer-bg.jpg
public/images/perldancer.jpg
public/css/style.css
public/css/error.css
public/javascripts/jquery.js
t/001_base.t
t/002_index_route.t
lib/BookWeb.pm
views/index.tt
views/layouts/main.tt
environments/production.yml
environments/development.yml
bin/app.psgi
17 changes: 17 additions & 0 deletions code/dancer2/BookWeb/MANIFEST.SKIP
@@ -0,0 +1,17 @@
^\.git\/
maint
^tags$
.last_cover_stats
Makefile$
^blib
^pm_to_blib
^.*.bak
^.*.old
^t.*sessions
^cover_db
^.*\.log
^.*\.swp$
MYMETA.*
^.gitignore
^.svn\/
^BookWeb-
26 changes: 26 additions & 0 deletions code/dancer2/BookWeb/Makefile.PL
@@ -0,0 +1,26 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;

# Normalize version strings like 6.30_02 to 6.3002,
# so that we can do numerical comparisons on it.
my $eumm_version = $ExtUtils::MakeMaker::VERSION;
$eumm_version =~ s/_//;

WriteMakefile(
NAME => 'BookWeb',
AUTHOR => q{YOUR NAME <youremail@example.com>},
VERSION_FROM => 'lib/BookWeb.pm',
ABSTRACT => 'YOUR APPLICATION ABSTRACT',
($eumm_version >= 6.3001
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer2' => 0.159003,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'BookWeb-*' },
);
9 changes: 9 additions & 0 deletions code/dancer2/BookWeb/bin/app.psgi
@@ -0,0 +1,9 @@
#!/usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";

use BookWeb;
BookWeb->to_app;
43 changes: 43 additions & 0 deletions code/dancer2/BookWeb/config.yml
@@ -0,0 +1,43 @@
# This is the main configuration file of your Dancer2 app
# env-related settings should go to environments/$env.yml
# all the settings in this file will be loaded at Dancer's startup.

# Your application's name
appname: "BookWeb"

# The default layout to use for your application (located in
# views/layouts/main.tt)
layout: "main"

# when the charset is set to UTF-8 Dancer2 will handle for you
# all the magic of encoding and decoding. You should not care
# about unicode within your app when this setting is set (recommended).
charset: "UTF-8"

session: Cookie

# template engine
# simple: default and very basic template engine
# template_toolkit: TT

# template: "simple"

template: "template_toolkit"
engines:
template:
template_toolkit:
start_tag: '<%'
end_tag: '%>'
session:
Cookie:
secret_key: somerandomnonsense
default_duration: 604800

plugins:
DBIC:
book:
schema_class: BookWeb::Schema
dsn: dbi:mysql:database=books
user: books
pass: README

11 changes: 11 additions & 0 deletions code/dancer2/BookWeb/cpanfile
@@ -0,0 +1,11 @@
requires "Dancer2" => "0.159003";

recommends "YAML" => "0";
recommends "URL::Encode::XS" => "0";
recommends "CGI::Deurl::XS" => "0";
recommends "HTTP::Parser::XS" => "0";

on "test" => sub {
requires "Test::More" => "0";
requires "HTTP::Request::Common" => "0";
};
23 changes: 23 additions & 0 deletions code/dancer2/BookWeb/environments/development.yml
@@ -0,0 +1,23 @@
# configuration file for development environment

# the logger engine to use
# console: log messages to STDOUT (your console where you started the
# application server)
# file: log message to a file in log/
logger: "console"

# the log level for this environment
# core is the lowest, it shows Dancer2's core log messages as well as yours
# (debug, info, warning and error)
log: "core"

# should Dancer2 consider warnings as critical errors?
warnings: 1

# should Dancer2 show a stacktrace when an error is caught?
# if set to yes, public/500.html will be ignored and either
# views/500.tt, 'error_template' template, or a default error template will be used.
show_errors: 1

# print the banner
startup_info: 1
16 changes: 16 additions & 0 deletions code/dancer2/BookWeb/environments/production.yml
@@ -0,0 +1,16 @@
# configuration file for production environment

# only log warning and error messsages
log: "warning"

# log message to a file in logs/
logger: "file"

# don't consider warnings critical
warnings: 0

# hide errors
show_errors: 0

# disable server tokens in production environments
no_server_tokens: 1
139 changes: 139 additions & 0 deletions code/dancer2/BookWeb/lib/BookWeb.pm
@@ -0,0 +1,139 @@
package BookWeb;
use Dancer2;
#use Dancer2::Session;
use Dancer2::Plugin::DBIC;
use Net::Amazon;
use DateTime;

our $VERSION = '0.1';

sub get_amazon {
return Net::Amazon->new(
token => $ENV{AMAZON_KEY},
secret_key => $ENV{AMAZON_SECRET},
associate_tag => $ENV{AMAZON_ASSTAG},
locale => 'uk',
) or die "Cannot connect to Amazon\n";
}

my %public_path = map { $_ => 1 } ('/', '/login', '/search');

hook before => sub {
if (! session('logged_in') and
! $public_path{request->path_info}) {
var requested_path => request->path_info;
forward '/login';
}
};

get '/' => sub {
my $books_rs = schema->resultset('Book');

my @reading = $books_rs->search({
started => { '!=', undef },
ended => undef,
});

my @read = $books_rs->search({
ended => { '!=', undef },
});

my @to_read = $books_rs->search({
started => undef,
});

template 'index', {
reading => \@reading,
read => \@read,
to_read => \@to_read,
};
};

get '/start/:isbn' => sub {

my $books_rs = schema->resultset('Book');
my $book = $books_rs->find({ isbn => param('isbn')});

if ($book) {
$book->update({started => DateTime->now});
}

return redirect '/';
};

get '/end/:isbn' => sub {

my $books_rs = schema->resultset('Book');
my $book = $books_rs->find({ isbn => param('isbn')});

if ($book) {
$book->update({ended => DateTime->now});
}

return redirect '/';
};

get '/add/:isbn' => sub {
my $author_rs = schema->resultset('Author');

my $amz = get_amazon();

# Search for the book at Amazon
my $resp = $amz->search(asin => param('isbn'));

unless ($resp->is_success) {
die 'Error: ', $resp->message;
}

my $book = $resp->properties;
my $title = $book->ProductName;
my $author_name = ($book->authors)[0];
my $imgurl = $book->ImageUrlMedium;

# Find or create the author
my $author = $author_rs->find_or_create({
name => $author_name,
});

# Add the book to the author
$author->add_to_books({
isbn => param('isbn'),
title => $title,
image_url => $imgurl,
});

return redirect '/';
};

post '/search' => sub {
my $amz = get_amazon();

my $resp = $amz->search(
keyword => param('search'),
mode => 'books',
);

my %data;
$data{search} = param('search');
if ($resp->is_success) {
$data{books} = [ $resp->properties ];
} else {
$data{error} = $resp->message;
}

template 'results', \%data;
};

get '/login' => sub {
template 'login', { path => vars->{requested_path } };
};

post '/login' => sub {
if (params->{user} eq 'reader' && params->{pass} eq 'letmein') {
session 'logged_in' => 1;
}

redirect params->{path} || '/';
};

true;
20 changes: 20 additions & 0 deletions code/dancer2/BookWeb/lib/BookWeb/Schema.pm
@@ -0,0 +1,20 @@
use utf8;
package BookWeb::Schema;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Schema';

__PACKAGE__->load_namespaces;


# Created by DBIx::Class::Schema::Loader v0.07042 @ 2015-03-29 16:32:33
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:NG4hEMUGO2NB1sOwP35hUw


# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;

0 comments on commit bbb8c65

Please sign in to comment.