Permalink
Browse files

First version "user-aware"

  * scaffold the application with Dancer 0.9906 helper so we have pretty errors
    and configuration.
  * add session: yaml in config.yml
  * /login et /logout actions
  * add a dumb TinyWiki::User model that should in the future
    authenticate a user for real.
  * Split app.pl into app.pl et TinyWiki.pm
  • Loading branch information...
1 parent 8a2c20f commit 7f2f62f08a686abd671a6218dc27494d1e7d17fd Alexis Sukrieh committed with Oct 16, 2009
Showing with 274 additions and 54 deletions.
  1. +2 −0 .gitignore
  2. +84 −0 TinyWiki.pm
  3. +3 −54 app.pl
  4. +14 −0 app.psgi
  5. +1 −0 config.yml
  6. +5 −0 environments/development.yml
  7. +5 −0 environments/production.yml
  8. +10 −0 lib/TinyWiki/User.pm
  9. +13 −0 public/404.html
  10. +13 −0 public/500.html
  11. +70 −0 public/css/error.css
  12. +34 −0 public/css/style.css
  13. +9 −0 views/layouts/main.tt
  14. +11 −0 views/login.tt
View
@@ -0,0 +1,2 @@
+logs/
+sessions/
View
@@ -0,0 +1,84 @@
+package TinyWiki;
+
+use Dancer;
+use KiokuDB;
+use Template;
+use Text::Markdown 'markdown';
+
+use lib 'lib';
+use TinyWiki::User;
+
+layout 'main';
+
+my $k = KiokuDB->connect(
+ 'bdb:dir=/tmp/kioku-tiny-wiki',
+ create => 1
+);
+
+# XXX Not sure about this: KiokuDB needs a scope object
+# so this does create at least one per request... but
+# not sure about its scope. :p
+before sub { var scope => $k->new_scope; };
+
+# Read-only actions
+
+get '/' => sub {
+ my $document = $k->lookup('home');
+
+ $document ||= { content => 'This is the default homepage' };
+ template 'page' => {
+ document => markdown( $document->{content} ),
+ name => 'home',
+ session => session,
+ };
+};
+
+# Dumb login action
+get '/login' => sub { template 'login' };
+post '/login' => sub {
+ my $user = TinyWiki::User::authenticate_user(params);
+ if (defined $user) {
+ session user => $user->{login};
+ "you have signed in as ".$user->{login};
+ }
+ else {
+ template 'login', {error => "bad credentials" };
+ }
+};
+
+get '/logout' => sub {
+ session user => undef;
+ "you have signed out"
+};
+
+# display a page
+get '/:name' => sub {
+ my $document = $k->lookup( params->{name} );
+
+ $document ||= { content => 'Page does not exist yet... Create it?' };
+ template 'page' => { document => markdown($document->{content}), name => params->{name} };
+};
+
+# Write actions (user is mandatory)
+
+
+get '/edit/:name' => sub {
+ my $document = $k->lookup( params->{name} );
+
+ template 'edit' => { name => params->{name}, document => $document };
+};
+
+post '/update' => sub {
+ my $document = $k->lookup( params->{name} );
+
+ if ( $document ) {
+ $document->{content} = params->{content};
+ $k->update($document);
+ } else {
+ $document = { content => params->{content} };
+ $k->store( params->{name} => $document );
+ }
+ template 'page' => { document => markdown($document->{content}), name => params->{name} };
+};
+
+1;
View
57 app.pl 100644 → 100755
@@ -1,55 +1,4 @@
-#!/usr/bin/env perl
-
-use KiokuDB;
+#!/usr/bin/perl
use Dancer;
-use Template;
-use Text::Markdown 'markdown';
-
-use strict;
-
-layout 'main';
-
-my $k = KiokuDB->connect(
- 'bdb:dir=/tmp/kioku-tiny-wiki',
- create => 1
-);
-
-# XXX Not sure about this: KiokuDB needs a scope object
-# so this does create at least one per request... but
-# not sure about its scope. :p
-before sub { var scope => $k->new_scope; };
-
-get '/' => sub {
- my $document = $k->lookup('home');
-
- $document ||= { content => 'This is the default homepage' };
- template 'page' => { document => markdown($document->{content}), name => 'home' };
-};
-
-get '/:name' => sub {
- my $document = $k->lookup( params->{name} );
-
- $document ||= { content => 'Page does not exist yet... Create it?' };
- template 'page' => { document => markdown($document->{content}), name => params->{name} };
-};
-
-get '/edit/:name' => sub {
- my $document = $k->lookup( params->{name} );
-
- template 'edit' => { name => params->{name}, document => $document };
-};
-
-post '/update' => sub {
- my $document = $k->lookup( params->{name} );
-
- if ( $document ) {
- $document->{content} = params->{content};
- $k->update($document);
- } else {
- $document = { content => params->{content} };
- $k->store( params->{name} => $document );
- }
- template 'page' => { document => markdown($document->{content}), name => params->{name} };
-};
-
-Dancer->dance;
+use TinyWiki;
+dance;
View
@@ -0,0 +1,14 @@
+# This is a PSGI application file for Apache+Plack support
+use CGI::PSGI;
+use lib '/home/sukria/Bureau/Development/tiny_wiki';
+use tiny_wiki;
+
+use Dancer::Config 'setting';
+setting apphandler => 'PSGI';
+setting environment => 'production';
+Dancer::Config->load;
+
+my $handler = sub {
+ my $env = shift;
+ Dancer->dance(CGI::PSGI->new($env));
+};
View
@@ -0,0 +1 @@
+session: yaml
@@ -0,0 +1,5 @@
+log: "debug"
+warnings: 1
+show_errors: 1
+auto_reload: 1
+
@@ -0,0 +1,5 @@
+log: "warning"
+warnings: 0
+show_errors: 0
+auto_reload: 0
+
View
@@ -0,0 +1,10 @@
+package TinyWiki::User;
+
+sub authenticate_user {
+ my ($params) = @_;
+
+ # here should come the real authentication
+ return { login => $params->{'login'} };
+}
+
+1;
View
@@ -0,0 +1,13 @@
+<!DOCTYPE html
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
+<head>
+<title>Error 404</title>
+<link rel="stylesheet" type="text/css" href="/css/error.css" />
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
+</head>
+<body>
+<h1>Error 404</h1><div id="content"><p><h2>Page Not Found</h2><p>Sorry, this is the void.</p></p></div><div id="footer">Powered by <a href="http://dancer.sukria.net">Dancer</a></div>
+</body>
+</html>
View
@@ -0,0 +1,13 @@
+<!DOCTYPE html
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
+<head>
+<title>Error 500</title>
+<link rel="stylesheet" type="text/css" href="/css/error.css" />
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
+</head>
+<body>
+<h1>Error 500</h1><div id="content"><p><h2>Internal Server Error</h2><p>Wooops, something went wrong</p></p></div><div id="footer">Powered by <a href="http://dancer.sukria.net">Dancer</a></div>
+</body>
+</html>
View
@@ -0,0 +1,70 @@
+body {
+ font-family: Lucida,sans-serif;
+}
+
+h1 {
+ color: #AA0000;
+ border-bottom: 1px solid #444;
+}
+
+h2 { color: #444; }
+
+pre {
+ font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace;
+ font-size: 12px;
+ border-left: 2px solid #777;
+ padding-left: 1em;
+}
+
+#footer {
+ font-size: 10px;
+}
+
+span.key {
+ color: #449;
+ font-weight: bold;
+ width: 120px;
+ display: inline;
+}
+
+span.value {
+ color: #494;
+}
+
+/* these are for the message boxes */
+
+pre.content {
+ background-color: #eee;
+ color: #000;
+ padding: 1em;
+ margin: 0;
+ border: 1px solid #aaa;
+ border-top: 0;
+ margin-bottom: 1em;
+}
+
+div.title {
+ font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace;
+ font-size: 12px;
+ background-color: #aaa;
+ color: #444;
+ font-weight: bold;
+ padding: 3px;
+ padding-left: 10px;
+}
+
+pre.content span.nu {
+ color: #889;
+ margin-right: 10px;
+}
+
+pre.error {
+ background: #334;
+ color: #ccd;
+ padding: 1em;
+ border-top: 1px solid #000;
+ border-left: 1px solid #000;
+ border-right: 1px solid #eee;
+ border-bottom: 1px solid #eee;
+}
+
View
@@ -0,0 +1,34 @@
+body {
+ font-family: Lucida,sans-serif;
+ color: #eee;
+ background-color: #1f1b1a;
+}
+
+#content {
+ color: #000;
+ background-color: #eee;
+ padding: 1em;
+ margin: 1em;
+ padding-top: 0.5em;
+}
+
+a {
+ color: #a5ec02;
+}
+
+h1 {
+ color: #a5ec02;
+}
+
+#footer {
+ border-top: 1px solid #aba29c;
+ margin-top: 2em;
+ padding-top: 1em;
+ font-size: 10px;
+ color: #ddd;
+}
+
+pre {
+ font-family: \"lucida console\",\"monaco\",\"andale mono\",\"bitstream vera sans mono\",\"consolas\",monospace;
+}
+
View
@@ -8,6 +8,15 @@
<p>
<strong>Tiny Wiki</strong> :
<a href="/">Home</a>
+
+ <span style="margin-left: 2em; ">
+ <% if (session.user) %>
+ <% session.user %> [<a href="/logout">Sign out</a>]
+ <% else %>
+ [<a href="/login">Sign in</a>]
+ <% end %>
+ </span>
+
</p>
</div>
<div id="content">
View
@@ -0,0 +1,11 @@
+<form action="/login" method="post">
+
+ <div>
+ <label for="login">Login</label>
+ <input id="login" type="text" name="login" value="" />
+ </div>
+
+ <div>
+ <input type="submit" value="Login" />
+ </div>
+</form>

0 comments on commit 7f2f62f

Please sign in to comment.