Skip to content

adapt tutorial for Dancer 2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is the code from Dancer::Tutorial as a working example for your convenience. This version of the tutorial is intended for Dancer 2.x.

(There are not so many differences between Dancer 1 and 2 for the application developer, so Dancer::Tutorial remains largely the same as in Dancer 1.x.)
163 changes: 80 additions & 83 deletions dancr.pl
Original file line number Diff line number Diff line change
@@ -1,117 +1,114 @@
#!/usr/bin/perl

use Dancer;
use Dancer 2.0;
use DBI;
use File::Spec;
use File::Slurp;
use Template;

set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
set 'session' => 'Simple';
set 'template' => 'template_toolkit';
set 'logger' => 'console';
set 'log' => 'debug';
set 'show_errors' => 1;
set 'startup_info' => 1;
set 'warnings' => 1;
set 'logger' => 'console'; #not default value?
set 'log' => 'info';
set 'show_errors' => 1; #not yet implemented in D2
set 'startup_info' => 1; #not yet implemented in D2
set 'warnings' => 1; #?
set 'username' => 'admin';
set 'password' => 'password';
set 'layout' => 'main';

my $flash;

sub set_flash {
my $message = shift;

$flash = $message;
my $message = shift;
$flash = $message;
}

sub get_flash {

my $msg = $flash;
$flash = "";

return $msg;
my $msg = $flash;
$flash = "";

return $msg;
}

sub connect_db {
my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
die $DBI::errstr;
return $dbh;
my $dbh = DBI->connect("dbi:SQLite:dbname=" . setting('database'))
or die $DBI::errstr;

return $dbh;
}

sub init_db {
my $db = connect_db();
my $schema = read_file('./schema.sql');
$db->do($schema) or die $db->errstr;
my $db = connect_db();
my $schema = read_file('./schema.sql');
$db->do($schema) or die $db->errstr;
}

hook before_template => sub {
my $tokens = shift;
$tokens->{'css_url'} = request->base . 'css/style.css';
$tokens->{'login_url'} = uri_for('/login');
$tokens->{'logout_url'} = uri_for('/logout');
my $tokens = shift;

$tokens->{'css_url'} = request->base . 'css/style.css';
$tokens->{'login_url'} = uri_for('/login');
$tokens->{'logout_url'} = uri_for('/logout');
};

get '/' => sub {
my $db = connect_db();
my $sql = 'select id, title, text from entries order by id desc';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute or die $sth->errstr;
template 'show_entries.tt', {
'msg' => get_flash(),
'add_entry_url' => uri_for('/add'),
'entries' => $sth->fetchall_hashref('id'),
};
my $db = connect_db();
my $sql = 'select id, title, text from entries order by id desc';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute or die $sth->errstr;
template 'show_entries.tt',
{ 'msg' => get_flash(),
'add_entry_url' => uri_for('/add'),
'entries' => $sth->fetchall_hashref('id'),
};
};

post '/add' => sub {
if ( not session('logged_in') ) {
send_error("Not logged in", 401);
}
my $db = connect_db();
my $sql = 'insert into entries (title, text) values (?, ?)';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'title'}, params->{'text'}) or die $sth->errstr;
set_flash('New entry posted!');
redirect '/';
if (not session('logged_in')) {
send_error("Not logged in", 401);
}

my $db = connect_db();
my $sql = 'insert into entries (title, text) values (?, ?)';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'title'}, params->{'text'}) or die $sth->errstr;

set_flash('New entry posted!');
redirect '/';
};

any ['get', 'post'] => '/login' => sub {
my $err;

if ( request->method() eq "POST" ) {
# process form input
if ( params->{'username'} ne setting('username') ) {
$err = "Invalid username";
}
elsif ( params->{'password'} ne setting('password') ) {
$err = "Invalid password";
}
else {
session 'logged_in' => true;
set_flash('You are logged in.');
return redirect '/';
}
}

# display login form
template 'login.tt', {
'err' => $err,
};

my $err;

if (request->method() eq "POST") {

# process form input
if (params->{'username'} ne setting('username')) {
$err = "Invalid username";
}
elsif (params->{'password'} ne setting('password')) {
$err = "Invalid password";
}
else {
session 'logged_in' => true;
set_flash('You are logged in.');
return redirect '/';
}
}

# display login form
template 'login.tt', {'err' => $err,};

};

get '/logout' => sub {
session->destroy;
set_flash('You are logged out.');
redirect '/';
session->destroy;
set_flash('You are logged out.');
redirect '/';
};

init_db();
start;

20 changes: 10 additions & 10 deletions views/layouts/main.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
<html>
<head>
<title>Dancr</title>
<link rel=stylesheet type=text/css href="<% css_url %>">
<link rel=stylesheet type=text/css href="[% css_url %]">
</head>
<body>
<div class=page>
<h1>Dancr</h1>
<div class=metanav>
<% IF not session.logged_in %>
<a href="<% login_url %>">log in</a>
<% ELSE %>
<a href="<% logout_url %>">log out</a>
<% END %>
[% IF not session.logged_in %]
<a href="[% login_url %]">log in</a>
[% ELSE %]
<a href="[% logout_url %]">log out</a>
[% END %]
</div>
<% IF msg %>
<div class=flash> <% msg %> </div>
<% END %>
<% content %>
[% IF msg %]
<div class=flash> [% msg %] </div>
[% END %]
[% content %]
</div>
</body>
</html>
4 changes: 2 additions & 2 deletions views/login.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h2>Login</h2>
<% IF err %><p class=error><strong>Error:</strong> <% err %><% END %>
<form action="<% login_url %>" method=post>
[% IF err %]<p class=error><strong>Error:</strong> [% err %][% END %]
<form action="[% login_url %]" method=post>
<dl>
<dt>Username:
<dd><input type=text name=username>
Expand Down
18 changes: 9 additions & 9 deletions views/show_entries.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% IF session.logged_in %>
<form action="<% add_entry_url %>" method=post class=add-entry>
[% IF session.logged_in %]
<form action="[% add_entry_url %]" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
Expand All @@ -8,13 +8,13 @@
<dd><input type=submit value=Share>
</dl>
</form>
<% END %>
[% END %]
<ul class=entries>
<% IF entries.size %>
<% FOREACH id IN entries.keys.nsort %>
<li><h2><% entries.$id.title %></h2><% entries.$id.text %>
<% END %>
<% ELSE %>
[% IF entries.size %]
[% FOREACH id IN entries.keys.nsort %]
<li><h2>[% entries.$id.title %]</h2>[% entries.$id.text %]
[% END %]
[% ELSE %]
<li><em>Unbelievable. No entries here so far</em>
<% END %>
[% END %]
</ul>