|
| 1 | +#!/usr/bin/env perl |
| 2 | +use Mojolicious::Lite; |
| 3 | +use Mojo::Util qw/md5_sum/; |
| 4 | +use FindBin; |
| 5 | +use File::Spec::Functions qw/catdir/; |
| 6 | +use Browser::Open qw/open_browser/; |
| 7 | + |
| 8 | +# This is the server-side code. |
| 9 | + |
| 10 | +my $SERV_PORT = 3000; |
| 11 | + |
| 12 | +my ($SSLCERTS,$HOMEDIR); |
| 13 | +BEGIN { |
| 14 | + $HOMEDIR = $ENV{PAR_TEMP} ? catdir($ENV{PAR_TEMP},'inc') : $FindBin::Bin; |
| 15 | + chdir $HOMEDIR or die "chdir $HOMEDIR: $!"; |
| 16 | + # do_pp.pl pulls the default Mojo SSL certs into the archive for us |
| 17 | + $SSLCERTS = $ENV{PAR_TEMP} ? '?cert=./server.crt&key=./server.key' : ''; |
| 18 | +} |
| 19 | + |
| 20 | +app->static->paths([catdir($HOMEDIR,'public')]); |
| 21 | +app->renderer->paths([catdir($HOMEDIR,'templates')]); |
| 22 | +app->secrets(['Hello, Perl World!']); |
| 23 | +app->types->type(js => "application/javascript"); |
| 24 | +app->types->type(data => "application/octet-stream"); |
| 25 | +app->types->type(mem => "application/octet-stream"); |
| 26 | +app->types->type(wasm => "application/wasm"); |
| 27 | + |
| 28 | +# Authentication and browser-launching stuff (optional) |
| 29 | +my $TOKEN = md5_sum(rand(1e15).time); |
| 30 | +hook before_server_start => sub { |
| 31 | + my ($server, $app) = @_; |
| 32 | + my @urls = map {Mojo::URL->new($_)->query(token=>$TOKEN)} @{$server->listen}; |
| 33 | + my $url = shift @urls or die "No urls?"; |
| 34 | + if ($ENV{DOING_PAR_PACKER}) { |
| 35 | + # arrange to have the server shut down in a few moments |
| 36 | + my $procpid = $$; |
| 37 | + my $pid = fork(); |
| 38 | + if (!defined $pid) { die "fork failed" } |
| 39 | + elsif ($pid==0) { sleep 5; kill 'USR1', $procpid; exit; } # child |
| 40 | + print "====> Please wait a few seconds...\n"; |
| 41 | + $SIG{USR1} = sub { $server->stop; exit }; |
| 42 | + } |
| 43 | + else { |
| 44 | + print "Attempting to open in browser: $url\n"; |
| 45 | + open_browser($url); |
| 46 | + } |
| 47 | +}; |
| 48 | +under sub { |
| 49 | + my $c = shift; |
| 50 | + return 1 if ($c->param('token')//'') eq $TOKEN; |
| 51 | + $c->render(text => 'Bad token!', status => 403); |
| 52 | + return undef; |
| 53 | +}; |
| 54 | + |
| 55 | +get '/' => sub { shift->render } => 'index'; |
| 56 | + |
| 57 | +post '/example' => sub { |
| 58 | + my $c = shift; |
| 59 | + my $data = $c->req->json; |
| 60 | + # can do anything here, this is just an example |
| 61 | + $data->{string} = reverse $data->{string}; |
| 62 | + $c->render(json => $data); |
| 63 | +}; |
| 64 | + |
| 65 | +app->start('daemon', '-l', "https://localhost:$SERV_PORT$SSLCERTS"); |
| 66 | + |
| 67 | +__DATA__ |
| 68 | +
|
| 69 | +@@ index.html.ep |
| 70 | +% layout 'main', title => 'WebPerl GUI Demo'; |
| 71 | +<main role="main" class="container"> |
| 72 | + <div> |
| 73 | + <h1>WebPerl Advanced GUI Demo</h1> |
| 74 | + <p class="lead">Hello, Perl World!</p> |
| 75 | + <div id="buttons"></div> |
| 76 | + </div> |
| 77 | +</main> |
0 commit comments