public
Description: A Perl 6 web framework
Homepage:
Clone URL: git://github.com/masak/web.git
web / bin / omgblog.pl
9e23d9a5 » tene 2009-05-10 Basic blog sketch... 1 use LolDispatch;
2 use HTTP::Daemon;
3 use Tags;
4
5 my $posts-file = '/tmp/blog/posts.perl';
6 our @posts;
7 sub index($request, $match) is handler</> {
d2dc1873 » tene 2009-05-10 * Fix some stupid decisions... 8 show {
9e23d9a5 » tene 2009-05-10 Basic blog sketch... 9 html {
10 head {
11 title { "blog index" }
12 }
13 body {
14 h1 'blog index';
15 ul {
16 for @posts.kv -> $k, $v {
17 li {
18 a :href("/post/$k"), $v<subject>;
19 }
20 }
21 }
22 a :href</post>, "new post";
23 }
24 }
25 };
26 }
27
28 sub format-post($q) {
29 return show {
30 h2 $q<subject>;
31 pre $q<body>;
32 };
33 }
34
35 sub item($request, $match) is handler(/^\/post\/(\d+)/) {
36 my $q = fetch-post($match[0]);
37 show {
38 html {
39 head {
40 title $q<subject>;
41 }
42 body {
43 h1 { a :href</>, "omgblog" }
44 outs format-post($q);
45 }
46 }
47 };
48 }
49
50 sub post($request, $match) is handler(/^\/post\/?$/) {
51 show {
52 html {
53 head {
54 title 'make a new post';
55 }
56 body {
57 h1 'omg new post dood';
58 form :method<POST>, :action</submit>, {
59 p {
60 label :for<subject>, 'Subject: ';
61 input :name<subject>, :id<subject>;
62 }
63 p {
64 label :for<body>, 'Body: ';
65 textarea :cols<80>, :rows<20>, :name<body>, :id<body>;
66 }
67 input :type<submit>, :name<submit>, :value('POST BLOG');
68 }
69 }
70 }
71 };
72 }
73
74 sub submit($request, $match) is handler(/^\/submit\/?$/) {
75 my $id = save-post($request.query);
76 show {
77 p { outs 'Post number '; a :href("/post/$id"), { $id } };
78 };
79 }
80
81 sub save-post($q) {
82 my $id = @posts.elems;
83 @posts[$id] = $q;
84 my $fh = open($posts-file, :w);
85 $fh.say( @posts.perl );
86 $fh.close();
87 return $id;
88 }
89
90 sub fetch-post($id) {
91 @posts[$id];
92 }
93
94 sub request($c) {
d2dc1873 » tene 2009-05-10 * Fix some stupid decisions... 95 my $response := dispatch($c.get_request);
96 $c.send_response: $response // "Error: no content";
9e23d9a5 » tene 2009-05-10 Basic blog sketch... 97 }
98
99 @posts = $posts-file ~~ :f ?? eval(slurp($posts-file)).list !! ();
100
101 my HTTP::Daemon $d .= new( :host('0.0.0.0'), :port(2080) );
102 say "Check out http://localhost:2080/";
103 $d.daemon();