Permalink
Browse files

Creating tutorial app repo using code from Dancer::Tutorial.

@TierraDelFuego pointed out in issue 735 that the tutorial app was not up to
date:

https://github.com/sukria/Dancer/issues/735

I'm moving the full code from the tutorial into a GitHub repo which users can
simply clone and run, then updating the tutorial to reference it, and updating
it where needed.
  • Loading branch information...
0 parents commit 3d02567fcb0c82a4f0e0bac3ca9973866021501e @bigpresh bigpresh committed Jan 29, 2012
Showing with 176 additions and 0 deletions.
  1. +117 −0 dancr.pl
  2. +5 −0 schema.sql
  3. +23 −0 views/layouts/main.tt
  4. +11 −0 views/login.tt
  5. +20 −0 views/show_entries.tt
117 dancr.pl
@@ -0,0 +1,117 @@
+#!/usr/bin/perl
+
+use Dancer;
+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 'username' => 'admin';
+set 'password' => 'password';
+set 'layout' => 'main';
+
+my $flash;
+
+sub set_flash {
+ my $message = shift;
+
+ $flash = $message;
+}
+
+sub get_flash {
+
+ my $msg = $flash;
+ $flash = "";
+
+ return $msg;
+}
+
+sub connect_db {
+ 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;
+}
+
+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');
+};
+
+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'),
+ };
+};
+
+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 '/';
+};
+
+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,
+ };
+
+};
+
+get '/logout' => sub {
+ session->destroy;
+ set_flash('You are logged out.');
+ redirect '/';
+};
+
+init_db();
+start;
+
@@ -0,0 +1,5 @@
+create table if not exists entries (
+ id integer primary key autoincrement,
+ title string not null,
+ text string not null
+);
@@ -0,0 +1,23 @@
+<!doctype html>
+<html>
+<head>
+ <title>Dancr</title>
+ <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 %>
+ </div>
+ <% IF msg %>
+ <div class=flash> <% msg %> </div>
+ <% END %>
+ <% content %>
+</div>
+</body>
+</html>
@@ -0,0 +1,11 @@
+<h2>Login</h2>
+<% 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>
+ <dt>Password:
+ <dd><input type=password name=password>
+ <dd><input type=submit value=Login>
+ </dl>
+</form>
@@ -0,0 +1,20 @@
+<% 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>
+ <dt>Text:
+ <dd><textarea name=text rows=5 cols=40></textarea>
+ <dd><input type=submit value=Share>
+ </dl>
+ </form>
+<% END %>
+<ul class=entries>
+<% 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 %>
+</ul>

0 comments on commit 3d02567

Please sign in to comment.