Skip to content
This repository
Browse code

Simple Dancer app that just allows users to register, log in and log …

…out.
  • Loading branch information...
commit de0198bcf5eb3f7cd55e7934c566da6e084d3cd3 1 parent f750bc6
Dave Cross authored January 31, 2014
1  Build.PL
@@ -9,6 +9,7 @@ my $build = Module::Build->new(
9 9
         Moose       => 0,
10 10
         YAML        => 0,
11 11
         Dancer      => 1,
  12
+        Dancer::Plugin::DBIC       => 0,
12 13
         MooseX::MarkAsMethods      => 0,
13 14
         MooseX::NonMoose           => 0,
14 15
         DBIx::Class::EncodedColumn => 0,
19  config.yml
@@ -18,11 +18,18 @@ charset: "UTF-8"
18 18
 # simple: default and very basic template engine
19 19
 # template_toolkit: TT
20 20
 
21  
-template: "simple"
  21
+# template: "simple"
22 22
 
23  
-# template: "template_toolkit"
24  
-# engines:
25  
-#   template_toolkit:
26  
-#     start_tag: '[%'
27  
-#     end_tag:   '%]'
  23
+template: "template_toolkit"
  24
+engines:
  25
+  template_toolkit:
  26
+    start_tag: '[%'
  27
+    end_tag:   '%]'
28 28
 
  29
+session: "simple"
  30
+
  31
+plugins:
  32
+  DBIC:
  33
+    default:
  34
+      dsn: dbi:mysql:dbname=lystyng
  35
+      schema_class: Lystyng::Schema
1  db/lystyng.sql
... ...
@@ -1,6 +1,7 @@
1 1
 create table `user` (
2 2
   id integer primary key auto_increment,
3 3
   username varchar(20) not null,
  4
+  name varchar(100) not null,
4 5
   email varchar(200) not null,
5 6
   password char(64) not null
6 7
 ) ENGINE=INNODB CHARSET=utf8;
66  lib/Lystyng.pm
@@ -8,9 +8,73 @@ package Lystyng;
8 8
 
9 9
 use Dancer ':syntax';
10 10
 our $VERSION = '0.0.1';
  11
+use Dancer::Plugin::DBIC qw[schema resultset];
  12
+
  13
+$ENV{LYSTYNG_DB_USER} && $ENV{LYSTYNG_DB_PASS}
  14
+  or die 'Must set LYSTYNG_DB_USER and LYSTYNG_DB_PASS';
  15
+
  16
+my $cfg = setting('plugins');
  17
+$cfg->{DBIC}{default}{user} = $ENV{LYSTYNG_DB_USER};
  18
+$cfg->{DBIC}{default}{pass} = $ENV{LYSTYNG_DB_PASS};
  19
+
11 20
 
12 21
 get '/' => sub {
13  
-    template 'index';
  22
+  template 'index';
  23
+};
  24
+
  25
+get '/register' => sub {
  26
+  template 'register';
  27
+};
  28
+
  29
+post '/register' => sub {
  30
+  my $user_rs = resultset('User');
  31
+  my @errors;
  32
+  if (param('password') ne param('password2')) {
  33
+    push @errors, 'Your passwords do not match.';
  34
+  }
  35
+  my ($user) = $user_rs->find({ username => param('username') });
  36
+  if ($user) {
  37
+    push @errors, 'Username ' . $user->username . ' is already in use.';
  38
+  };
  39
+
  40
+  if (@errors) {
  41
+    template 'register', {
  42
+      errors => \@errors,
  43
+    };
  44
+  } else {
  45
+    $user = $user_rs->create({
  46
+      username => param('username'),
  47
+      name     => param('name'),
  48
+      email    => param('email'),
  49
+      password => param('password'),
  50
+    });
  51
+
  52
+    session user => $user;
  53
+
  54
+    redirect '/';
  55
+  }
  56
+};
  57
+
  58
+get '/login' => sub {
  59
+  template 'login';
  60
+};
  61
+
  62
+post '/login' => sub {
  63
+  my $user_rs = resultset('User');
  64
+  my ($user) = $user_rs->find({ username => param('username') });
  65
+  if ($user && $user->check_password(param('password'))) {
  66
+    session user => $user;
  67
+    redirect '/';
  68
+  } else {
  69
+    template 'login', {
  70
+      error    => 1,
  71
+    };
  72
+  }
  73
+};
  74
+
  75
+get '/logout' => sub {
  76
+  session user => undef;
  77
+  redirect '/';
14 78
 };
15 79
 
16 80
 true;
12  lib/Lystyng/Schema/Result/User.pm
@@ -50,6 +50,12 @@ __PACKAGE__->table("user");
50 50
   is_nullable: 0
51 51
   size: 20
52 52
 
  53
+=head2 name
  54
+
  55
+  data_type: 'varchar'
  56
+  is_nullable: 0
  57
+  size: 100
  58
+
53 59
 =head2 email
54 60
 
55 61
   data_type: 'varchar'
@@ -73,6 +79,8 @@ __PACKAGE__->add_columns(
73 79
   { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
74 80
   "username",
75 81
   { data_type => "varchar", is_nullable => 0, size => 20 },
  82
+  "name",
  83
+  { data_type => "varchar", is_nullable => 0, size => 100 },
76 84
   "email",
77 85
   { data_type => "varchar", is_nullable => 0, size => 200 },
78 86
   "password",
@@ -117,8 +125,8 @@ __PACKAGE__->has_many(
117 125
 );
118 126
 
119 127
 
120  
-# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:20:50
121  
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dOskylIQblron+9s87lFpw
  128
+# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-31 21:09:46
  129
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hqwt5y89b3BUVwXtE8rwag
122 130
 
123 131
 
124 132
 # You can replace this text with custom code or comments, and it will be preserved on regeneration
153  views/index.tt
... ...
@@ -1,148 +1,5 @@
1  
-  
2  
-<!-- 
3  
-    Credit goes to the Ruby on Rails team for this page 
4  
-    has been heavily based on the default Rails page that is 
5  
-    built with a scaffolded application.
6  
-
7  
-    Thanks a lot to them for their work.
8  
-
9  
-    See Ruby on Rails if you want a kickass framework in Ruby:
10  
-    http://www.rubyonrails.org/
11  
--->
12  
-
13  
-<div id="page">
14  
-      <div id="sidebar">
15  
-        <ul id="sidebar-items">
16  
-          <li>
17  
-            <h3>Join the community</h3>
18  
-            <ul class="links">
19  
-
20  
-              <li><a href="http://perldancer.org/">PerlDancer</a></li>
21  
-              <li><a href="http://twitter.com/PerlDancer/">Official Twitter</a></li>
22  
-              <li><a href="https://github.com/PerlDancer/Dancer/">GitHub Community</a></li>
23  
-            </ul>
24  
-          </li>
25  
-          
26  
-          <li>
27  
-            <h3>Browse the documentation</h3>
28  
-
29  
-            <ul class="links">
30  
-              <li><a
31  
-              href="http://search.cpan.org/dist/Dancer/lib/Dancer/Introduction.pod">Introduction</a></li>
32  
-              <li><a href="http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod">Cookbook</a></li>
33  
-              <li><a href="http://search.cpan.org/dist/Dancer/lib/Dancer/Deployment.pod">Deployment Guide</a></li>
34  
-              <li><a
35  
-              href="http://search.cpan.org/dist/Dancer/lib/Dancer/Tutorial.pod"
36  
-              title="a tutorial to build a small blog engine with Dancer">Tutorial</a></li>
37  
-            </ul>
38  
-          </li>
39  
-
40  
-          <li>
41  
-            <h3>Your application's environment</h3>
42  
-
43  
-            <ul>
44  
-                <li>Location: <code>/home/dcross/dev/github/lystyng/Lystyng</code></li>
45  
-                <li>Template engine: <code><% settings.template %></code></li>
46  
-                <li>Logger: <code><% settings.logger %></code></li>
47  
-                <li>Environment: <code><% settings.environment %></code></li>
48  
-            </ul>
49  
-
50  
-          </li>
51  
-        </ul>
52  
-
53  
-      </div>
54  
-
55  
-      <div id="content">
56  
-        <div id="header">
57  
-          <h1>Perl is dancing</h1>
58  
-          <h2>You&rsquo;ve joined the dance floor!</h2>
59  
-        </div>
60  
-
61  
-        <div id="getting-started">
62  
-          <h1>Getting started</h1>
63  
-          <h2>Here&rsquo;s how to get dancing:</h2>
64  
-                    
65  
-          <h3><a href="#" id="about_env_link">About your application's environment</a></h3>
66  
-
67  
-          <div id="about-content" style="display: none;">
68  
-            <table>
69  
-                <tbody>
70  
-                <tr>
71  
-                    <td>Perl version</td>
72  
-                    <td><tt><% perl_version %></tt></td>
73  
-                </tr>
74  
-                <tr>
75  
-                    <td>Dancer version</td>
76  
-                    <td><tt><% dancer_version %></tt></td>
77  
-                </tr>
78  
-                <tr>
79  
-                    <td>Backend</td>
80  
-                    <td><tt><% settings.apphandler %></tt></td>
81  
-                </tr>
82  
-                <tr>
83  
-                    <td>Appdir</td>
84  
-                    <td><tt>/home/dcross/dev/github/lystyng/Lystyng</tt></td>
85  
-                </tr>
86  
-                <tr>
87  
-                    <td>Template engine</td>
88  
-                    <td><tt><% settings.template %></tt></td>
89  
-                </tr>
90  
-                <tr>
91  
-                    <td>Logger engine</td>
92  
-                    <td><tt><% settings.logger %></tt></td>
93  
-                </tr>
94  
-                <tr>
95  
-                    <td>Running environment</td>
96  
-                    <td><tt><% settings.environment %></tt></td>
97  
-                </tr>
98  
-                </tbody>
99  
-            </table>
100  
-          </div>
101  
-
102  
-    <script type="text/javascript">
103  
-    $('#about_env_link').click(function() {
104  
-        $('#about-content').slideToggle('fast', function() {
105  
-            // ok
106  
-        });
107  
-        return( false );
108  
-    });
109  
-    </script>
110  
-
111  
-
112  
-          <ol>          
113  
-            <li>
114  
-              <h2>Tune your application</h2>
115  
-
116  
-              <p>
117  
-              Your application is configured via a global configuration file,
118  
-              <tt>config.yml</tt> and an "environment" configuration file,
119  
-              <tt>environments/development.yml</tt>. Edit those files if you
120  
-              want to change the settings of your application.
121  
-              </p>
122  
-            </li>
123  
-
124  
-            <li>
125  
-              <h2>Add your own routes</h2>
126  
-
127  
-              <p>
128  
-              The default route that displays this page can be removed,
129  
-              it's just here to help you get started. The template used to
130  
-              generate this content is located in 
131  
-              <code>views/index.tt</code>.
132  
-              You can add some routes to <tt>lib/Lystyng.pm</tt>. 
133  
-              </p>
134  
-            </li>
135  
-
136  
-            <li>
137  
-                <h2>Enjoy web development again</h2>
138  
-
139  
-                <p>
140  
-                Once you've made your changes, restart your standalone server
141  
-                (bin/app.pl) and you're ready to test your web application.
142  
-                </p>
143  
-            </li>
144  
-
145  
-          </ol>
146  
-        </div>
147  
-      </div>
148  
-    </div>
  1
+[% IF session.user -%]
  2
+  <p>Welcome [% session.user.name %] [<a href="/logout">Log out</a>]</p>
  3
+[% ELSE %]
  4
+  <p><a href="/login">Log in</a> or <a href="/register">Register</a></p>
  5
+[% END -%]
10  views/layouts/main.tt
@@ -2,19 +2,19 @@
2 2
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 3
 <html xmlns="http://www.w3.org/1999/xhtml">
4 4
 <head>
5  
-<meta http-equiv="Content-type" content="text/html; charset=<% settings.charset %>" />
  5
+<meta http-equiv="Content-type" content="text/html; charset=[% settings.charset %]" />
6 6
 <title>Lystyng</title>
7  
-<link rel="stylesheet" href="<% request.uri_base %>/css/style.css" />
  7
+<link rel="stylesheet" href="[% request.uri_base %]/css/style.css" />
8 8
 
9 9
 <script type="text/javascript">/* <![CDATA[ */
10  
-    !window.jQuery && document.write('<script type="text/javascript" src="<% request.uri_base %>/javascripts/jquery.js"><\/script>')
  10
+    !window.jQuery && document.write('<script type="text/javascript" src="[% request.uri_base %]/javascripts/jquery.js"><\/script>')
11 11
 /* ]]> */</script>
12 12
 
13 13
 </head>
14 14
 <body>
15  
-<% content %>
  15
+[% content %]
16 16
 <div id="footer">
17  
-Powered by <a href="http://perldancer.org/">Dancer</a> <% dancer_version %>
  17
+Powered by <a href="http://perldancer.org/">Dancer</a> [% dancer_version %]
18 18
 </div>
19 19
 </body>
20 20
 </html>
10  views/login.tt
... ...
@@ -0,0 +1,10 @@
  1
+[% IF error -%]
  2
+<p>That username and/or password is incorrect.</p>
  3
+[% END -%]
  4
+<form method="post" action="/login">
  5
+<table>
  6
+  <tr><th>Username:</th><td><input name="username" value="[% params.username %]"></td></tr>
  7
+  <tr><th>Password:</th><td><input name="password" value="[% params.password %]"></td></tr>
  8
+  <tr><td colspan="2"><input type="submit"></td></tr>
  9
+</table>
  10
+</form>
16  views/register.tt
... ...
@@ -0,0 +1,16 @@
  1
+[% IF errors -%]
  2
+<ul>
  3
+[%   FOREACH error IN errors -%]
  4
+<li>[% error %]</li>
  5
+[%   END -%]
  6
+[% END -%]
  7
+<form method="post" action="/register">
  8
+<table>
  9
+  <tr><th>Username:</th><td><input name="username" value="[% params.username %]"></td></tr>
  10
+  <tr><th>Email:</th><td><input name="email" value="[% params.email %]"></td></tr>
  11
+  <tr><th>Name:</th><td><input name="name" value="[% params.name %]"></td></tr>
  12
+  <tr><th>Password:</th><td><input name="password" value="[% params.password %]"></td></tr>
  13
+  <tr><th>Confirm Password:</th><td><input name="password2" value="[% params.password2 %]"></td></tr>
  14
+  <tr><td colspan="2"><input type="submit"></td></tr>
  15
+</table>
  16
+</form>

0 notes on commit de0198b

Please sign in to comment.
Something went wrong with that request. Please try again.