Permalink
Browse files

[web-druid] changed to use the new simple regime

For some reason, though, it doesn't work. I will have to investigate
further. Getting this error when hitting the page:

$ perl6 bin/web-druid
Druid now runs on http://127.0.0.1:8888/
No applicable candidates found to dispatch to for 'trait_mod:of'
in method HTTP::Daemon::accept (lib/HTTP/Daemon.pm:218)
called from method Web::Handler::HTTPDaemon::run (/Users/masak/gwork/web/lib/Web/Handler/HTTPDaemon.pm:8)
called from Main (bin/web-druid:9)
  • Loading branch information...
1 parent 79bc10a commit 16afaed5a864b5047ba8bb87d26fb96df6a6dc33 @masak committed Sep 6, 2009
Showing with 17 additions and 78 deletions.
  1. +4 −59 bin/web-druid
  2. +13 −19 lib/Druid/Webapp.pm
View
63 bin/web-druid
@@ -1,64 +1,9 @@
#!/usr/local/bin/perl6
use v6;
-use HTTP::Daemon;
use Druid::Webapp;
-defined @*ARGS[0] && @*ARGS[0] eq '--request' ?? request() !! daemon();
+use Web::Handler::HTTPDaemon;
-# Serve one page
-sub request($c) {
- my $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')}";
- if $r.url.path eq '/board.svg' {
- $c.send_response( svg-board() );
- }
- else {
- my $qs = $r.url.path ~~ / '?' (.*) $/
- ?? $0
- !! '';
- $c.send_response( Druid::Webapp.page($qs) );
- }
- }
- 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;
-}
-
-sub svg-board() {
- return '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
- "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
-<svg xmlns="http://www.w3.org/2000/svg"
- version="1.0"
- width="400"
- height="400">
- <rect
- width="400"
- height="400"
- x="0"
- y="0"
- style="opacity:1;fill:none;stroke:#000000;stroke-width:2;"
- />
-</svg>';
-}
+my $port = 8888;
+say "Druid now runs on http://127.0.0.1:$port/";
+Web::Handler::HTTPDaemon.run( Druid::Webapp.new );
View
32 lib/Druid/Webapp.pm
@@ -1,29 +1,23 @@
use v6;
-use Tags;
+
+use Web::Request;
+use Web::Response;
+
use Druid::Game;
use Druid::View::Text;
-class Druid::Webapp {
-
- method page(Str $query-string) {
+class Druid::Webapp does Callable {
+ method postcircumfix:<( )>($env) {
my $board-size = 8;
my Druid::Game $game .= new(:size($board-size));
my Druid::View $view = Druid::View::Text.new(:$game);
- return
- show {
- html {
- head { title { 'Druid' } }
- body {
- pre { $view }
- object :type<image/svg+xml>, :data</board.svg>, {
- 'alternate text'
- }
- ul {
- for $game.possible-moves() -> $move {
- li {
- a :href("?moves=$move"), { $move }
- }}}}}};
+ my Web::Response $res .= new;
+ $res.write($_) for
+ '<title>Druid</title>',
+ '<pre>',
+ $view,
+ '</pre>';
+ $res.finish();
}
-
}

0 comments on commit 16afaed

Please sign in to comment.