diff options
| author | yo mama <pepper@scannerjammer.com> | 2015-02-02 20:29:31 -0800 |
|---|---|---|
| committer | yo mama <pepper@scannerjammer.com> | 2015-02-02 20:29:31 -0800 |
| commit | d7ebb35b53c2f3e0ae8a609e9c9cc4bb9aa9ccd6 (patch) | |
| tree | fe99a2302f90e071b524b1feb53b0af786d5c0b3 | |
first
| -rw-r--r-- | dancr.pl | 120 | ||||
| -rw-r--r-- | database/dancr.db | bin | 0 -> 3072 bytes | |||
| -rw-r--r-- | database/schema.sql | 6 | ||||
| -rw-r--r-- | logs/development.log | 0 | ||||
| -rw-r--r-- | public/css/simple-form.css | 57 | ||||
| -rw-r--r-- | views/layouts/main.tt | 23 | ||||
| -rw-r--r-- | views/login.tt | 11 | ||||
| -rw-r--r-- | views/show_entries.tt | 23 |
8 files changed, 240 insertions, 0 deletions
diff --git a/dancr.pl b/dancr.pl new file mode 100644 index 0000000..0d8d501 --- /dev/null +++ b/dancr.pl @@ -0,0 +1,120 @@ +use Dancer; +use DBI; +use File::Spec; +use File::Slurp; +use Template; + +print STDERR "firing up server...\n"; +set 'database' => './database/dancr.db'; +set 'session' => 'Simple'; +set 'template' => 'template_toolkit'; +set 'logger' => 'console'; +set 'log' => 'debug'; +set 'show_errors' => 1; +set 'access_log' => 1; +set 'warnings' => 1; +set 'username' => 'username'; +set 'password' => 'password'; +set 'layout' => 'main'; + +#CONSTANTS +my $SITE_NAME = 'Demo'; +my $SCHEMAFILE = './database/schema.sql'; +my $FORM_CSS = 'css/simple-form.css'; +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($SCHEMAFILE); + $db->do($schema) or die $db->errstr; +} + +before_template sub { + my $tokens = shift; + $tokens->{'site_name'} = $SITE_NAME; + $tokens->{'css_url'} = request->base . $FORM_CSS; + $tokens->{'login_url'} = uri_for('/login'); + $tokens->{'logout_url'} = uri_for('/logout'); +}; + +get '/' => sub { + my $db = connect_db(); + my $sql = 'select id, date, 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 (date, title, text) values (?, ?, ?)'; + my $sth = $db->prepare($sql) or die $db->errstr; + $sth->execute(time(), 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.'); + redirect '/'; + } + } + + # display login form + template 'login.tt', { + 'err' => $err, + }; + +}; + +get '/logout' => sub { + session->destroy; + set_flash('You are logged out.'); + redirect '/'; +}; + +init_db(); +start; + diff --git a/database/dancr.db b/database/dancr.db Binary files differnew file mode 100644 index 0000000..dc977eb --- /dev/null +++ b/database/dancr.db diff --git a/database/schema.sql b/database/schema.sql new file mode 100644 index 0000000..2bd7916 --- /dev/null +++ b/database/schema.sql @@ -0,0 +1,6 @@ +create table if not exists entries ( + id integer primary key autoincrement, + date date not null, + title string not null, + text string not null +); diff --git a/logs/development.log b/logs/development.log new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/logs/development.log diff --git a/public/css/simple-form.css b/public/css/simple-form.css new file mode 100644 index 0000000..0cc6218 --- /dev/null +++ b/public/css/simple-form.css @@ -0,0 +1,57 @@ +body { +font-family: sans-serif; +} +a, h1, h2 { +color: gray; +} +h1, h2 { +font-family: 'Georgia', serif; +margin: 0; +} +h1 { +border-bottom: 1px solid black; +} +h2 { +font-size: 1.2em; +} +.page { +margin: 2em auto; +width: 35em; +border: 1px solid black; +padding: 0.8em; +background: white; +} +.entries { +list-style: none; +margin: 0; +padding: 0; +border-bottom: 1px solid black; +} +.entries li { +margin: 0.8em 1.2em; +} +.entries li h2 { +margin-left: -1em; +} +.add-entry { +font-size: 0.9em; +border-bottom: 1px solid black; +} +.add-entry dl { +font-weight: bold; +} +.login_logout { +text-align: right; +font-size: 0.8em; +padding: 0.3em; +margin-bottom: 1em; +} +.flash { +background: whitesmoke; +padding: 0.5em; +border: 1px solid black; +} +.error { +background: indianred; +padding: 0.5em; +} diff --git a/views/layouts/main.tt b/views/layouts/main.tt new file mode 100644 index 0000000..2bc5ad8 --- /dev/null +++ b/views/layouts/main.tt @@ -0,0 +1,23 @@ +<!doctype html> +<html> +<head> +<title><% site_name %></title> +<link rel="stylesheet" type="text/css" href="<% css_url %>"> +</head> +<body> +<div class="login_logout"> +<% IF not session.logged_in %> + <a href="<% login_url %>">log in</a> +<% ELSE %> + <a href="<% logout_url %>">log out</a> +<% END %> +</div> +<div class="page"> + <h1><% site_name %></h1> + <% IF msg %> + <div class="flash"> <% msg %> </div> + <% END %> + <% content %> +</div> +</body> +</html> diff --git a/views/login.tt b/views/login.tt new file mode 100644 index 0000000..617687a --- /dev/null +++ b/views/login.tt @@ -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> diff --git a/views/show_entries.tt b/views/show_entries.tt new file mode 100644 index 0000000..9de4fae --- /dev/null +++ b/views/show_entries.tt @@ -0,0 +1,23 @@ + <% IF session.logged_in %> + <form action="<% add_entry_url %>" method="post" class="add-entry"> + <dl> + <dt>Title: + <dd><input type="text" name="title" id="title"> + <dt>Text: + <dd><textarea name="text" id="text"></textarea> + <dd><input type="submit" value="post"> + </dl> + </form> + <% END %> + <ul class=entries> + <% IF entries.size %> + <% FOREACH id IN entries.keys.nsort %> + <li> + <h2 class="title"><% entries.$id.title %></h2> + <span class="time"><% entries.$id.date %>: </span> + <span class="entry"><% entries.$id.text %></span> + <% END %> + <% ELSE %> + <li><em>Unbelievable. No entries here so far</em> + <% END %> + </ul> |
