Skip to content

Commit

Permalink
[bin/web-druid] early web app
Browse files Browse the repository at this point in the history
You can't make moves in it yet. But it works.
  • Loading branch information
Carl Masak committed Apr 12, 2009
1 parent dd6cca0 commit 2f0bcca
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions bin/web-druid
@@ -0,0 +1,63 @@
#!/usr/local/bin/perl6
use v6;
use HTTP::Daemon;
use Tags;
use Druid::Game;
use Druid::View::Text;

defined @*ARGS[0] && @*ARGS[0] eq '--request' ?? request() !! daemon();

# Serve one page
sub request {
# Currently executed in a child process of socat - inefficient
my HTTP::Daemon $d .= new;
while my HTTP::Daemon::ClientConn $c = $d.accept {
while my HTTP::Request $r = $c.get_request {
if $r.req_method eq 'GET' {
# log request info to the standard error stream
warn "{hhmm} GET {$r.url.path} {$r.header('User-Agent')}";
my $qs = $r.url.path ~~ / '?' (.*) $/
?? $0
!! '';
my $board-size = 8;
my Druid::Game $game .= new(:size($board-size));
my Druid::View $view = Druid::View::Text.new(:$game);

$c.send_response(
show {
html {
head { title { 'Druid' } }
body {
pre { $view }
ul {
for $game.possible-moves() -> $move {
li {
a :href("?moves=$move"), { $move }
}}}}}}
);
}
else {
warn "{hhmm} rejected {$r.req_method} {$r.url.path}";
$c.send_error('RC_FORBIDDEN');
}
warn ' '; # blank line
}
}
}

# Executed as main parent process with an endless loop that re-starts
# netcat after every page request.
sub daemon {
my HTTP::Daemon $d .= new( host=> '127.0.0.1', port=> 8888 );
say "Browse this Perl 6 (Rakudo) web server at {$d.url}";
$d.daemon();
}

# give the current time in hh:mm format
sub hhmm {
my $t = int(time);
my $m = int( $t / 60 ) % 60;
my $h = int( $t / 3600 ) % 24;
my $hhmm = "{$h.fmt('%02d')}:{$m.fmt('%02d')}";
return $hhmm;
}
1 change: 1 addition & 0 deletions deps.proto
@@ -0,0 +1 @@
web

0 comments on commit 2f0bcca

Please sign in to comment.