Created
December 11, 2016 20:25
-
-
Save ufobat/8fa1042351d821cd7de7e85ae75a344c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Dancr; | |
Dancr.start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6.c; | |
unit class Dancr; | |
use Bailador; | |
use DBIish; | |
use LWP::Simple; | |
my %settings; | |
multi sub setting(Str $name) { | |
%settings{$name} | |
} | |
multi sub setting(Pair $pair) { | |
%settings{$pair.key} = $pair.value | |
} | |
setting 'database' => $*TMPDIR.child('dancr.db'); | |
setting 'session' => 'Simple'; | |
setting 'template' => 'template_toolkit'; | |
setting 'logger' => 'console'; | |
setting 'log' => 'debug'; | |
setting 'show_errors' => 1; | |
setting 'startup_info' => 1; | |
setting 'warnings' => 1; | |
# webscale authentication method | |
setting 'username' => 'admin'; | |
setting 'password' => 'password'; | |
setting 'layout' => 'main'; | |
my $flash; | |
method to-app() { | |
app.get-psgi-app | |
} | |
method start_requests { | |
my $promise = Promise.start( | |
{ | |
my $count = 0; | |
while $count < 10 { | |
try { | |
say "start proc async"; | |
my $proc = Proc::Async.new("/usr/bin/curl", '-m', '5', 'http://localhost:3000/'); | |
$proc.stdout.tap(-> $v { print "$v" }); | |
$proc.stderr.tap(-> $v { print "$v" }); | |
say await $proc.start; | |
say "done"; | |
CATCH { | |
sleep 1; | |
say .gist; | |
} | |
} | |
} | |
} | |
); | |
} | |
method start() { | |
self.start_requests(); | |
baile(); | |
} | |
sub set_flash($message) { | |
$flash = $message; | |
} | |
sub get_flash() { | |
my $msg = $flash; | |
$flash = ""; | |
return $msg; | |
} | |
sub connect_db() { | |
my $dbh = DBIish.connect('SQLite', :database(setting('database').Str)); | |
return $dbh; | |
} | |
sub init_db() { | |
my $db = connect_db(); | |
my $schema = slurp 'schema.sql'; | |
$db.do($schema); | |
LEAVE { $db.dispose }; | |
} | |
sub std_tokens () { | |
return | |
} | |
sub layout($content) { | |
template 'layouts/' ~ setting('layout') ~ '.tt', { | |
css_url => '/css/style.css', | |
login_url => uri_for('/login'), | |
logout_url => uri_for('/logout'), | |
session => session, | |
content => $content, | |
}; | |
} | |
get '/' => { | |
#my $db = connect_db(); | |
#LEAVE { $db.dispose }; | |
#my $db will leave { .disconnect } = connect_db(); | |
#my $sql = 'select id, title, text from entries order by id desc'; | |
#my $sth = $db.prepare($sql); | |
#$sth.execute; | |
#my $entries = $sth.allrows(:array-of-hash).map({$_<id> => $_}).hash, | |
my $entries = {}; | |
layout template 'show_entries.tt', { | |
msg => get_flash(), | |
add_entry_url => uri_for('/add'), | |
entries => $entries, | |
session => session, | |
}; | |
} | |
post '/add' => { | |
unless session<logged_in> { | |
send_error("Not logged in", 401); | |
} | |
my $db = connect_db(); | |
LEAVE { $db.dispose }; | |
my $sql = 'insert into entries (title, text) values (?, ?)'; | |
my $sth = $db.prepare($sql); | |
$sth.execute(request.params<title>, request.params<text>); | |
set_flash('New entry posted!'); | |
redirect '/'; | |
} | |
get '/login' => sub (*@a) { | |
# display login form | |
layout template 'login.tt', {err => ''}; | |
} | |
post '/login' => sub (*@a) { | |
my $err; | |
# process form input | |
if request.params<username> ne setting('username') { | |
$err = "Invalid username"; | |
} | |
elsif request.params<password> ne setting('password') { | |
$err = "Invalid password"; | |
} | |
else { | |
session<logged_in> = True; | |
set_flash('You are logged in.'); | |
return redirect '/'; | |
} | |
# display login form | |
layout template 'login.tt', {err => $err}; | |
} | |
get '/logout' => { | |
app.session-delete; | |
set_flash('You are logged out.'); | |
redirect '/'; | |
} | |
get '/exit' => { | |
exit 0; | |
} | |
init_db(); | |
sub send_error($message, $code) { | |
} | |
sub uri_for($path) { | |
return $path; | |
} | |
# vim: ft=perl6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
┌─[martin@psyche] - [~/.workspace/p6/Bailador-Benchmark/Dancr] - [So Dez 11, 21:25] | |
└─[$] <> perl6 -v | |
This is Rakudo version 2016.11-188-g8d4dbb2 built on MoarVM version 2016.11-41-gd2139b5 | |
implementing Perl 6.c. | |
┌─[martin@psyche] - [~/.workspace/p6/Bailador-Benchmark/Dancr] - [So Dez 11, 21:25] | |
└─[$] <> perl6 --profile -I . dancr.pl6 | |
Entering the development dance floor: http://0.0.0.0:3000 | |
start proc async | |
[2016-12-11T20:25:14Z] Started HTTP server. | |
% Total % Received % Xferd Average Speed Time Time Time Current | |
Dload Upload Total Spent Left Speed | |
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- [2016-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment