-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.pl
executable file
·113 lines (92 loc) · 2.91 KB
/
app.pl
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env perl
use lib qw<lib>;
use Mojolicious::Lite;
use Mojo::UserAgent;
use Mojo::File qw/path/;
use Mojo::Util qw/trim/;
use Time::Moment;
use Perl5::Party::Posts;
use constant GLOT_KEY => '02fb41ba-78cd-44d8-9f30-2a28350000a8';
my $posts = Perl5::Party::Posts->new;
my $ua = Mojo::UserAgent->new;
app->config({ hypnotoad => { listen => ['http://*:3000'], proxy => 1 } });
plugin 'AssetPack' => { pipes => [qw/Sass JavaScript Combine/] };
app->asset->process( 'app.css' => 'sass/main.scss' );
app->asset->process( 'app.js' => qw{
js/ie10-viewport-bug-workaround.js
js/codemirror/codemirror.min.js
js/codemirror/perl6-mode.js
js/main.js
}
);
### Routes
get '/about';
get '/' => sub {
my $c = shift;
$c->stash( posts => $posts->all );
} => 'index';
get '/post/#post' => sub {
my $c = shift;
my ($meta, $markdown, $html) = $posts->load(
$c->param('post'),
$c->param('md'),
);
$html or return $c->reply->not_found;
return $c->render(
text => prep_for_blogs_perl_org($markdown),
format => 'txt',
) if length $c->param('md');
$c->stash( %$meta, post => $html, title => $meta->{title} );
} => 'post';
post '/run' => sub {
my $c = shift;
my $code = $c->param('code')
or return $c->reply->not_found;
$code =~ s/\N{ZERO WIDTH SPACE}//g;
$ua->post(
'https://run.glot.io/languages/perl6/latest' => {
'Content-type' => 'application/json',
'Authorization' => 'Token ' . GLOT_KEY,
} => json => { files => [{ name => 'main.p6', content => $code }] }
=> sub {
my ($ua, $tx) = @_;
my $out = $tx->res->json;
$out = trim join "\n", $out->{stdout} // (),
$out->{stderr} ? "STDERR:\n$out->{stderr}" : ();
$c->render( text => $out );
},
);
$c->render_later;
};
any $_ => sub {
my $c = shift;
my $posts = [ map +{ %$_ }, @{ $posts->all } ];
$_->{date} = blog_date_to_feed_date($_->{date}) for @$posts;
my $blog_last_updated_date = $posts->[0]{date};
$c->stash(
posts => $posts,
last_update => $blog_last_updated_date,
template => 'feed',
format => 'xml',
);
} for '/feed', '/feed/', '/feed/index', '/atom', '/atom/', '/atom/index';
get '/pull/*password' => sub {
my $c = shift;
return $c->reply->not_found
unless $c->param('password') eq trim path('pull-password')->slurp;
$c->render(
text => "Pulled!\n" . `git pull`,
format => 'txt',
);
};
app->start;
sub prep_for_blogs_perl_org {
my $markdown = shift;
$markdown
=~ s{src="/assets/pics/}{ style="display: block; margin: 5px auto;" src="https://rakudo.party/assets/pics/}gr;
}
sub blog_date_to_feed_date {
my $date = shift;
return Time::Moment->from_string("${date}T00:00:00Z")
->strftime("%a, %d %b %Y %H:%M:%S %z");
}