/
app.pl6
89 lines (71 loc) · 1.84 KB
/
app.pl6
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
#!/usr/bin/env perl6
use v6.c;
use lib 'lib';
use Bailador;
use Bailador::Route::StaticFile;
app.config.mode = 'development';
# simple cases
get '/' => sub {
q{
<h2>Welcome to Bailador!</h2>
<ul>
<li><a href="/red">Redirect to not existing page</a></li>
<li><a href="/die">Throw an exception</a></li>
<li><a href="/about">Simple text</a></li>
<li><a href="/hello/Foo Bar">Say hello to Foo Bar</a></li>
<li><a href="/hello/Foo.html">Say hello to Foo.html</a></li>
<li><a href="/hello/Foo/Bar">This should be a 404</a></li>
<li><a href="/def/Foo/Bar">This should work</a></li>
<li><a href="/fooBarMoo">Routing</a></li>
<li><a href="/one-two-three">Routing</a></li>
<li><a href="/template/abc">Use a template</a></li>
</ul>
};
}
get '/red' => sub {
redirect('/index.html');
}
get '/redir/:code' => sub ($code) {
redirect('/index.html', $code.Int);
}
get '/die' => sub {
die 'This is an exception so you can see how it is handled';
"hello world"
}
head '/about' => sub { "" }
get '/about' => sub {
"about me"
}
get '/hello/:name' => sub ($name) {
"Hello $name!"
};
get '/def/:one/:two' => sub ($first, $second) {
"Hello '$first' and '$second'!"
};
get '/info' => sub {
say %*ENV<P6SGI_CONTAINER>;
say %*ENV<P6SGI_CONTAINER>.WHAT;
say %*ENV<PERL6_PROGRAM_NAME>;
say %*ENV<PERL6_PROGRAM_NAME>.WHAT;
}
# regexes, as usual
get /foo(.+)/ => sub ($x) {
"regexes! I got $x"
}
get rx{ '/' (.+) '-' (.+) } => sub ($x, $y) {
"$x and $y"
}
# templates!
get / ^ '/template/' (.+) $ / => sub ($x) {
template 'tmpl.tt', { name => $x }
}
get '/env' => sub {
my Str $result;
for request.env.sort(*.key)>>.kv -> ($k, $v) {
$result ~= "$k\t" ~ $v.perl ~ "\n";
}
app.render: content => $result, type => 'text/plain';
$result;
}
static-dir / (.*) / => '/public';
baile();