Skip to content

Commit

Permalink
Simple Dancer app that just allows users to register, log in and log …
Browse files Browse the repository at this point in the history
…out.
  • Loading branch information
davorg committed Jan 31, 2014
1 parent f750bc6 commit de0198b
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 162 deletions.
1 change: 1 addition & 0 deletions Build.PL
Expand Up @@ -9,6 +9,7 @@ my $build = Module::Build->new(
Moose => 0,
YAML => 0,
Dancer => 1,
Dancer::Plugin::DBIC => 0,
MooseX::MarkAsMethods => 0,
MooseX::NonMoose => 0,
DBIx::Class::EncodedColumn => 0,
Expand Down
19 changes: 13 additions & 6 deletions config.yml
Expand Up @@ -18,11 +18,18 @@ charset: "UTF-8"
# simple: default and very basic template engine
# template_toolkit: TT

template: "simple"
# template: "simple"

# template: "template_toolkit"
# engines:
# template_toolkit:
# start_tag: '[%'
# end_tag: '%]'
template: "template_toolkit"
engines:
template_toolkit:
start_tag: '[%'
end_tag: '%]'

session: "simple"

plugins:
DBIC:
default:
dsn: dbi:mysql:dbname=lystyng
schema_class: Lystyng::Schema
1 change: 1 addition & 0 deletions db/lystyng.sql
@@ -1,6 +1,7 @@
create table `user` (
id integer primary key auto_increment,
username varchar(20) not null,
name varchar(100) not null,
email varchar(200) not null,
password char(64) not null
) ENGINE=INNODB CHARSET=utf8;
Expand Down
66 changes: 65 additions & 1 deletion lib/Lystyng.pm
Expand Up @@ -8,9 +8,73 @@ package Lystyng;

use Dancer ':syntax';
our $VERSION = '0.0.1';
use Dancer::Plugin::DBIC qw[schema resultset];

$ENV{LYSTYNG_DB_USER} && $ENV{LYSTYNG_DB_PASS}
or die 'Must set LYSTYNG_DB_USER and LYSTYNG_DB_PASS';

my $cfg = setting('plugins');
$cfg->{DBIC}{default}{user} = $ENV{LYSTYNG_DB_USER};
$cfg->{DBIC}{default}{pass} = $ENV{LYSTYNG_DB_PASS};


get '/' => sub {
template 'index';
template 'index';
};

get '/register' => sub {
template 'register';
};

post '/register' => sub {
my $user_rs = resultset('User');
my @errors;
if (param('password') ne param('password2')) {
push @errors, 'Your passwords do not match.';
}
my ($user) = $user_rs->find({ username => param('username') });
if ($user) {
push @errors, 'Username ' . $user->username . ' is already in use.';
};

if (@errors) {
template 'register', {
errors => \@errors,
};
} else {
$user = $user_rs->create({
username => param('username'),
name => param('name'),
email => param('email'),
password => param('password'),
});

session user => $user;

redirect '/';
}
};

get '/login' => sub {
template 'login';
};

post '/login' => sub {
my $user_rs = resultset('User');
my ($user) = $user_rs->find({ username => param('username') });
if ($user && $user->check_password(param('password'))) {
session user => $user;
redirect '/';
} else {
template 'login', {
error => 1,
};
}
};

get '/logout' => sub {
session user => undef;
redirect '/';
};

true;
Expand Down
12 changes: 10 additions & 2 deletions lib/Lystyng/Schema/Result/User.pm
Expand Up @@ -50,6 +50,12 @@ __PACKAGE__->table("user");
is_nullable: 0
size: 20
=head2 name
data_type: 'varchar'
is_nullable: 0
size: 100
=head2 email
data_type: 'varchar'
Expand All @@ -73,6 +79,8 @@ __PACKAGE__->add_columns(
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"username",
{ data_type => "varchar", is_nullable => 0, size => 20 },
"name",
{ data_type => "varchar", is_nullable => 0, size => 100 },
"email",
{ data_type => "varchar", is_nullable => 0, size => 200 },
"password",
Expand Down Expand Up @@ -117,8 +125,8 @@ __PACKAGE__->has_many(
);


# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:20:50
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dOskylIQblron+9s87lFpw
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-31 21:09:46
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hqwt5y89b3BUVwXtE8rwag


# You can replace this text with custom code or comments, and it will be preserved on regeneration
Expand Down
153 changes: 5 additions & 148 deletions views/index.tt
@@ -1,148 +1,5 @@

<!--
Credit goes to the Ruby on Rails team for this page
has been heavily based on the default Rails page that is
built with a scaffolded application.

Thanks a lot to them for their work.

See Ruby on Rails if you want a kickass framework in Ruby:
http://www.rubyonrails.org/
-->

<div id="page">
<div id="sidebar">
<ul id="sidebar-items">
<li>
<h3>Join the community</h3>
<ul class="links">

<li><a href="http://perldancer.org/">PerlDancer</a></li>
<li><a href="http://twitter.com/PerlDancer/">Official Twitter</a></li>
<li><a href="https://github.com/PerlDancer/Dancer/">GitHub Community</a></li>
</ul>
</li>

<li>
<h3>Browse the documentation</h3>

<ul class="links">
<li><a
href="http://search.cpan.org/dist/Dancer/lib/Dancer/Introduction.pod">Introduction</a></li>
<li><a href="http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod">Cookbook</a></li>
<li><a href="http://search.cpan.org/dist/Dancer/lib/Dancer/Deployment.pod">Deployment Guide</a></li>
<li><a
href="http://search.cpan.org/dist/Dancer/lib/Dancer/Tutorial.pod"
title="a tutorial to build a small blog engine with Dancer">Tutorial</a></li>
</ul>
</li>

<li>
<h3>Your application's environment</h3>

<ul>
<li>Location: <code>/home/dcross/dev/github/lystyng/Lystyng</code></li>
<li>Template engine: <code><% settings.template %></code></li>
<li>Logger: <code><% settings.logger %></code></li>
<li>Environment: <code><% settings.environment %></code></li>
</ul>

</li>
</ul>

</div>

<div id="content">
<div id="header">
<h1>Perl is dancing</h1>
<h2>You&rsquo;ve joined the dance floor!</h2>
</div>

<div id="getting-started">
<h1>Getting started</h1>
<h2>Here&rsquo;s how to get dancing:</h2>

<h3><a href="#" id="about_env_link">About your application's environment</a></h3>

<div id="about-content" style="display: none;">
<table>
<tbody>
<tr>
<td>Perl version</td>
<td><tt><% perl_version %></tt></td>
</tr>
<tr>
<td>Dancer version</td>
<td><tt><% dancer_version %></tt></td>
</tr>
<tr>
<td>Backend</td>
<td><tt><% settings.apphandler %></tt></td>
</tr>
<tr>
<td>Appdir</td>
<td><tt>/home/dcross/dev/github/lystyng/Lystyng</tt></td>
</tr>
<tr>
<td>Template engine</td>
<td><tt><% settings.template %></tt></td>
</tr>
<tr>
<td>Logger engine</td>
<td><tt><% settings.logger %></tt></td>
</tr>
<tr>
<td>Running environment</td>
<td><tt><% settings.environment %></tt></td>
</tr>
</tbody>
</table>
</div>

<script type="text/javascript">
$('#about_env_link').click(function() {
$('#about-content').slideToggle('fast', function() {
// ok
});
return( false );
});
</script>


<ol>
<li>
<h2>Tune your application</h2>

<p>
Your application is configured via a global configuration file,
<tt>config.yml</tt> and an "environment" configuration file,
<tt>environments/development.yml</tt>. Edit those files if you
want to change the settings of your application.
</p>
</li>

<li>
<h2>Add your own routes</h2>

<p>
The default route that displays this page can be removed,
it's just here to help you get started. The template used to
generate this content is located in
<code>views/index.tt</code>.
You can add some routes to <tt>lib/Lystyng.pm</tt>.
</p>
</li>

<li>
<h2>Enjoy web development again</h2>

<p>
Once you've made your changes, restart your standalone server
(bin/app.pl) and you're ready to test your web application.
</p>
</li>

</ol>
</div>
</div>
</div>
[% IF session.user -%]
<p>Welcome [% session.user.name %] [<a href="/logout">Log out</a>]</p>
[% ELSE %]
<p><a href="/login">Log in</a> or <a href="/register">Register</a></p>
[% END -%]
10 changes: 5 additions & 5 deletions views/layouts/main.tt
Expand Up @@ -2,19 +2,19 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=<% settings.charset %>" />
<meta http-equiv="Content-type" content="text/html; charset=[% settings.charset %]" />
<title>Lystyng</title>
<link rel="stylesheet" href="<% request.uri_base %>/css/style.css" />
<link rel="stylesheet" href="[% request.uri_base %]/css/style.css" />

<script type="text/javascript">/* <![CDATA[ */
!window.jQuery && document.write('<script type="text/javascript" src="<% request.uri_base %>/javascripts/jquery.js"><\/script>')
!window.jQuery && document.write('<script type="text/javascript" src="[% request.uri_base %]/javascripts/jquery.js"><\/script>')
/* ]]> */</script>

</head>
<body>
<% content %>
[% content %]
<div id="footer">
Powered by <a href="http://perldancer.org/">Dancer</a> <% dancer_version %>
Powered by <a href="http://perldancer.org/">Dancer</a> [% dancer_version %]
</div>
</body>
</html>
10 changes: 10 additions & 0 deletions views/login.tt
@@ -0,0 +1,10 @@
[% IF error -%]
<p>That username and/or password is incorrect.</p>
[% END -%]
<form method="post" action="/login">
<table>
<tr><th>Username:</th><td><input name="username" value="[% params.username %]"></td></tr>
<tr><th>Password:</th><td><input name="password" value="[% params.password %]"></td></tr>
<tr><td colspan="2"><input type="submit"></td></tr>
</table>
</form>
16 changes: 16 additions & 0 deletions views/register.tt
@@ -0,0 +1,16 @@
[% IF errors -%]
<ul>
[% FOREACH error IN errors -%]
<li>[% error %]</li>
[% END -%]
[% END -%]
<form method="post" action="/register">
<table>
<tr><th>Username:</th><td><input name="username" value="[% params.username %]"></td></tr>
<tr><th>Email:</th><td><input name="email" value="[% params.email %]"></td></tr>
<tr><th>Name:</th><td><input name="name" value="[% params.name %]"></td></tr>
<tr><th>Password:</th><td><input name="password" value="[% params.password %]"></td></tr>
<tr><th>Confirm Password:</th><td><input name="password2" value="[% params.password2 %]"></td></tr>
<tr><td colspan="2"><input type="submit"></td></tr>
</table>
</form>

0 comments on commit de0198b

Please sign in to comment.