-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathDancer2_ex.pl
125 lines (98 loc) · 2.71 KB
/
Dancer2_ex.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use Dancer2;
use DBI;
use File::Spec;
use File::Slurper qw/ read_text /;
use Template;
set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
set 'session' => 'Simple';
set 'template' => 'template_toolkit';
set 'logger' => 'console';
set 'log' => 'debug';
set 'show_stacktrace' => 1;
set 'startup_info' => 1;
set 'username' => 'admin';
set 'password' => 'password';
set 'layout' => 'main';
sub set_flash {
my $message = shift;
session flash => $message;
}
sub get_flash {
my $msg = session('flash');
session->delete('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_text('./schema.sql');
$db->do($schema)
or die $db->errstr;
}
hook before_template_render => sub {
my $tokens = shift;
$tokens->{'css_url'} = request->base . 'css/style.css';
$tokens->{'login_url'} = uri_for('/login');
$tokens->{'logout_url'} = uri_for('/logout');
};
get '/' => sub {
my $db = connect_db();
my $sql = 'select id, 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 (title, text) values (?, ?)';
my $sth = $db->prepare($sql)
or die $db->errstr;
$sth->execute(
body_parameters->get('title'),
body_parameters->get('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 ( body_parameters->get('username') ne setting('username') ) {
$err = "Invalid username";
}
elsif ( body_parameters->get('password') ne setting('password') ) {
$err = "Invalid password";
}
else {
session 'logged_in' => true;
set_flash('You are logged in.');
return redirect '/';
}
}
# display login form
template 'login.tt', {
err => $err,
};
};
get '/logout' => sub {
app->destroy_session;
set_flash('You are logged out.');
redirect '/';
};
any qr{.*} => sub {
}
1;