Skip to content

Commit

Permalink
Added (very simple) login.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Feb 24, 2016
1 parent a3daca3 commit 09b6d11
Show file tree
Hide file tree
Showing 50 changed files with 11,213 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -42,3 +42,7 @@ Mark items as completed
## Step 9

Add new tasks

## Step 10

Support log in
1 change: 1 addition & 0 deletions step10/README
@@ -0,0 +1 @@
Step 10 - Support login
Empty file added step10/Todo/.dancer
Empty file.
24 changes: 24 additions & 0 deletions step10/Todo/MANIFEST
@@ -0,0 +1,24 @@
MANIFEST
cpanfile
Makefile.PL
MANIFEST.SKIP
config.yml
.dancer
public/dispatch.fcgi
public/500.html
public/dispatch.cgi
public/favicon.ico
public/404.html
public/images/perldancer-bg.jpg
public/images/perldancer.jpg
public/css/style.css
public/css/error.css
public/javascripts/jquery.js
t/001_base.t
t/002_index_route.t
lib/Todo.pm
views/index.tt
views/layouts/main.tt
environments/production.yml
environments/development.yml
bin/app.psgi
17 changes: 17 additions & 0 deletions step10/Todo/MANIFEST.SKIP
@@ -0,0 +1,17 @@
^\.git\/
maint
^tags$
.last_cover_stats
Makefile$
^blib
^pm_to_blib
^.*.bak
^.*.old
^t.*sessions
^cover_db
^.*\.log
^.*\.swp$
MYMETA.*
^.gitignore
^.svn\/
^Todo-
26 changes: 26 additions & 0 deletions step10/Todo/Makefile.PL
@@ -0,0 +1,26 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;

# Normalize version strings like 6.30_02 to 6.3002,
# so that we can do numerical comparisons on it.
my $eumm_version = $ExtUtils::MakeMaker::VERSION;
$eumm_version =~ s/_//;

WriteMakefile(
NAME => 'Todo',
AUTHOR => q{YOUR NAME <youremail@example.com>},
VERSION_FROM => 'lib/Todo.pm',
ABSTRACT => 'YOUR APPLICATION ABSTRACT',
($eumm_version >= 6.3001
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer2' => 0.166001,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Todo-*' },
);
17 changes: 17 additions & 0 deletions step10/Todo/bin/app.psgi
@@ -0,0 +1,17 @@
#!/usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";

use Plack::Builder;

use Todo;

builder {
enable 'Plack::Middleware::Static',
path => qr{^/(javascripts|css)/} , root => './public/';
Todo->to_app;
};

61 changes: 61 additions & 0 deletions step10/Todo/config.yml
@@ -0,0 +1,61 @@
# This is the main configuration file of your Dancer2 app
# env-related settings should go to environments/$env.yml
# all the settings in this file will be loaded at Dancer's startup.

# Your application's name
appname: "Todo"

# The default layout to use for your application (located in
# views/layouts/main.tt)
layout: "main"

# when the charset is set to UTF-8 Dancer2 will handle for you
# all the magic of encoding and decoding. You should not care
# about unicode within your app when this setting is set (recommended).
charset: "UTF-8"

# template engine
# simple: default and very basic template engine
# template_toolkit: TT

# template: "simple"

template: "template_toolkit"
engines:
template:
template_toolkit:
start_tag: '<%'
end_tag: '%>'

# session engine
#
# Simple: in-memory session store - Dancer2::Session::Simple
# YAML: session stored in YAML files - Dancer2::Session::YAML
#
# Check out metacpan for other session storage options:
# https://metacpan.org/search?q=Dancer2%3A%3ASession&search_type=modules
#
# Default value for 'cookie_name' is 'dancer.session'. If you run multiple
# Dancer apps on the same host then you will need to make sure 'cookie_name'
# is different for each app.
#
# engines:
# session:
# Simple:
# cookie_name: todoapp.session

#engines:
# session:
# YAML:
# cookie_name: eshop.session
# is_secure: 1
# is_http_only: 1

plugins:
DBIC:
default:
schema_class: Todo::Schema
dsn: dbi:mysql:dbname=todo
user: todouser
password: sekr1t

11 changes: 11 additions & 0 deletions step10/Todo/cpanfile
@@ -0,0 +1,11 @@
requires "Dancer2" => "0.166001";

recommends "YAML" => "0";
recommends "URL::Encode::XS" => "0";
recommends "CGI::Deurl::XS" => "0";
recommends "HTTP::Parser::XS" => "0";

on "test" => sub {
requires "Test::More" => "0";
requires "HTTP::Request::Common" => "0";
};
23 changes: 23 additions & 0 deletions step10/Todo/environments/development.yml
@@ -0,0 +1,23 @@
# configuration file for development environment

# the logger engine to use
# console: log messages to STDOUT (your console where you started the
# application server)
# file: log message to a file in log/
logger: "console"

# the log level for this environment
# core is the lowest, it shows Dancer2's core log messages as well as yours
# (debug, info, warning and error)
log: "core"

# should Dancer2 consider warnings as critical errors?
warnings: 1

# should Dancer2 show a stacktrace when an 5xx error is caught?
# if set to yes, public/500.html will be ignored and either
# views/500.tt, 'error_template' template, or a default error template will be used.
show_errors: 1

# print the banner
startup_info: 1
16 changes: 16 additions & 0 deletions step10/Todo/environments/production.yml
@@ -0,0 +1,16 @@
# configuration file for production environment

# only log warning and error messsages
log: "warning"

# log message to a file in logs/
logger: "file"

# don't consider warnings critical
warnings: 0

# hide errors
show_errors: 0

# disable server tokens in production environments
no_server_tokens: 1
79 changes: 79 additions & 0 deletions step10/Todo/lib/Todo.pm
@@ -0,0 +1,79 @@
package Todo;
use Dancer2;

our $VERSION = '0.1';

use Dancer2::Plugin::DBIC;
use DateTime::Format::Strptime;
use DateTime;

get '/' => sub {
my $dt_parser = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d',
);

my @items = schema->resultset('Item')->search(
undef, { order_by => 'due' }
)->all;

my $now = DateTime->now;

template 'index', { items => \@items };
};

post '/done/:id' => sub {
my $id = route_parameters->get('id');
my $item = schema->resultset('Item')->find($id);

unless ($item) {
status 404;
return "Item $id not found";
}

$item->update({ done => 1 });
redirect('/');
};

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

post '/add' => sub {
my $item;
my @errors;
my %cols = (
title => 'Title',
description => 'Description',
due => 'Due Date',
);
foreach (qw[title description due]) {
unless ($item->{$_} = body_parameters->get($_)) {
push @errors, $cols{$_};
}
}

if (@errors) {
return template 'add', { errors => \@errors, item => $item };
}

resultset('Item')->create($item);
redirect('/');
};

post '/login' => sub {
my $user = body_parameters->get('user');
my $pass = body_parameters->get('password');

# TODO: Make this better!
if ($pass eq 'letmein') {
session user => $user;
}
redirect '/';
};

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

true;
20 changes: 20 additions & 0 deletions step10/Todo/lib/Todo/Schema.pm
@@ -0,0 +1,20 @@
use utf8;
package Todo::Schema;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Schema';

__PACKAGE__->load_namespaces;


# Created by DBIx::Class::Schema::Loader v0.07045 @ 2016-02-20 15:34:03
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vVE+p7R6DZlode+faUm3PA


# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
1;

0 comments on commit 09b6d11

Please sign in to comment.