Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a734592868
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 99 lines (91 sloc) 2.742 kb
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
use Web::Request;
use Web::Response;
use Faz::Application;
use Faz::Dispatcher;
use Faz::Action::Root;
use Faz::Action::Chained;
use Faz::Action::Public;
use Tags;

class Yarn is Faz::Application {
  my sub get-posts() {
    my @posts = 'data/posts' ~~ :f
      ?? @(eval(slurp('data/posts')))
        !! ();
    return @posts;
  }

  method setup {
    $.dispatcher = Faz::Dispatcher.new;

    my $root = Faz::Action::Chained.new\
      ( :parent(0),
        :private-name('(root)'),
        :regex(/ ^ /),
        :execute-closure({ 1; }),
        :finish-closure({ 1; }),
        :begin-closure({ %*stash<posts> = get-posts() }) );
    self.register-action($root);

    my $index = Faz::Action::Public.new\
      ( :private-name('(root)/'),
        :regex(/ \/ $ /),
        :parent($root),
        :begin-closure({ 1; }),
        :finish-closure({ 1; }),
        :execute-closure({
           $*response.write(show {
             html {
               head { title { 'Yarn' } }
                 body {
                   p {
                     a :href</create>, { 'Write a new post' }
                     }
                     for @(%*stash<posts>) -> $post {
                       div :class<post>, {
                         h1 { $post<title> };
                         div { $post<content> };
                       }
                     }
                   }
                 }
               })
        })
      );
    self.register-action($index);

    my $create = Faz::Action::Public.new\
      ( :private-name('(root)/create'),
        :regex(/ \/create \/? $/),
        :parent($root),
        :begin-closure({ 1; }),
        :finish-closure({ 1; }),
        :execute-closure({
           when $*request.GET<title> ne '' {
             my $p = $*request.GET;
             unless 'data' ~~ :d {
               run('mkdir data');
             }
             %*stash<posts>.unshift( { title => $p<title>,
                                       content => $p<content> } );
             my $fh = open('data/posts', :w) or die $!;
             $fh.print( %*stash<posts>.perl );
             $fh.close;
           }

           $*response.write(show {
             html { title { 'Writing a post' } }
               body {
                 form :action</create>, :method<get>, {
                   p { input :name<title>, { '' } }
                   p { textarea :name<content>, { '' } }
                   p { input :type<submit>, { '' } }
                 }
               }
           });
        })
      );
    self.register-action($create);
    $.dispatcher.compile;
  }

  method call($env) {
    my Web::Request $req .= new($env);
    my Web::Response $res .= new;
    self.handle($req, $res);
    $res.finish();
  }
}
Something went wrong with that request. Please try again.