I was reflecting on what would have made my initial learning curve less 
steep.  I think something like the following live cribsheet 
mojolicious:lite example would have made it a lot easier.  Thus, I am 
posting it here.  (And to add it to documentation and M has discussion 
suggested anyway.  I would put this at the start or end of the tutorial.)

I would also suggest that mojo add something like "mojo generate 
medium_app" that creates something akin to what I have below.

The specific code is not definitive.  It is just a suggestion.  I hope it 
helps others.


#!/usr/bin/env perl
use Mojolicious::Lite;


## Documentation browser under "/perldoc"
## plugin 'PODRenderer';


my $indexpage= sub {
  my $c = shift;  ## $c here is the same as $self in template
  $c->stash( stasha => 'pass via stasha' );


  $c->render(template => 'index', stashb => 'pass via stashb' );
};


get '/' => $indexpage;
post '/' => $indexpage;


## extend to allow $self->domain(), which should return last two host parts 
plus port
*Mojo::URL::domain = sub {
  my $self= shift;
  my $fulldomain= $self->to_abs->host;
  (defined($fulldomain)) or return;
  ($fulldomain =~ /localhost/) and return "localhost:3000";
  $fulldomain =~ s{^http.*[^/]*/}{};  ## search forward until we see the 
first / (from http.s:.as//)


  my @f= split(/\./, $fulldomain);
  (@f) or return;  ## should never happen, so this is an error return
  return $f[$#f-1].'.'.$f[$#f];  ## the last two: TLD and Domain
};




# Not found (404)
get '/missing' => sub { shift->render(template => 'does_not_exist') };


# Exception (500)
get '/dies' => sub { die "Intentional error with $@"; };


get '/bad' => sub { die "I am diying!"; };


## app->sessions->cookie_domain(...);  ## app parameters can go here

app->start;




__DATA__


@@ index.html.ep


% layout 'default';
% title 'basic mojolicious cribsheet';


<p style="font-size:small">Ivo Welch, May 2017</p>


  <%
  use warnings FATAL => qw{ uninitialized };
  use strict;
  %>


<h1>TITLE: <%= title %></h1>




  <h2> Generation and Running </h2>


  <p> <span>mojo generate lite_app myfunnyname</span> </p>


  <p> <span>morbo -v myfunnyname</span> or <p> <span>morbo -v myfunnyname -l 
mydomain.tld:80 </p>


  <p> CLI: <span>./myfunnyname get -v -M POST -c 'test' /echo</span> </p>




  <h2> Redirection </h2>


  <p>To redirect in perl code, use <span> $self->flash( message => "you are 
redirected" )->$c->redirect_to($url); </span>




  <h2> Using Templates </h2>


  <p><span>&lt;% perl_code(); &gt;</span>


  <p><span>&lt;%= $escaped_perl_variable &gt;</span>


  <p><span>&lt;%== $unescaped_perl_variable &gt;</span>


  <p>stasha: <span><%= $stasha %></span> <span><%= $stashb %></span>




  <h2>The Current URLs</h2>


  <% my $cururl= $self->req->url->to_abs; %>


  <table>
  <tr> <td>Full</td> <td> <%= $cururl %> </td> </tr>
  <tr> <td>Host</td> <td> <%= $cururl->host %> </td> </tr>
  <tr> <td>Port</td> <td> <%= $cururl->port %> </td> </tr>
  <tr> <td>Fragment</td> <td> <%= $cururl->fragment %> </td> </tr>
  <tr> <td>Scheme</td> <td> <%= $cururl->scheme %> </td> </tr>
  <tr> <td>Path_Query</td> <td> <%= $cururl->path_query %> </td> </tr>
  </table>


  <p>Referer: <span><%== dumper $self->req->headers %></span></p> <!-- not 
ideal? maybe give referrer -->




  <h2> FORM GET </h2>


  <p>Getting something:</p>
  <form action='/' method='GET'>
    <input name="sometext" value="this was get" />
    <input type="submit">
  </form>


  <p>Get Result: <span><%= ($self->req->query_params->param("sometext")) || 
"no text yet" %></span></p>




  <h2> FORM POST </h2>


  <form action='/?ab=12' method='POST'>
    <input name="somepost" value="this was post" />
    <input type="submit">
  </form>


  <p>Post Result: <span><%= ($self->req->params->param("somepost")) || "no 
text yet" %></span> .
 ab: <span><%= ($self->req->query_params->param("ab")) || "ab not seen" 
%></span></p>


  <p>In hash form: <span><%= dumper $c->req->params->to_hash %></span>




  <h2> FILE UPLOAD </h2>


   <form action="/" id="uploadform" method="post" 
enctype="multipart/form-data" style="display:block">
     <label for="idupload">Upload A New File: </label>
     <input type="file" name="uploadfile" id="idupload" 
style="display:inline"  ><br />
     <input type="submit" >
   </form>




  <%
  sub tellmeuploadinfo {
    my $uploadfile= shift->param('uploadfile');
    my @rv= ("no file", 0, "nothing yet uploaded");
    (defined($uploadfile)) and @rv= ( $uploadfile->size, 
$uploadfile->filename, substr($uploadfile->asset->{content}, 0, 80));
    return \@rv;
  }
  sub toprint {
    $_ = $_[0];
    return "<table> <tr> <th>Field</th> <th>Content<th> </tr>
                    <tr> <td>Filename</td> <td>".$_->[0]."</td> </tr>
                    <tr> <td>Filesize</td> <td>".$_->[1]."</td> </tr>
                    <tr> <td>First 80 characters </td> <td>".$_->[2]."</td> 
</tr>
            </table>";
  }
  %>


  <p><%== toprint(tellmeuploadinfo($self)) %></p>




  <h2> Cookie </h2>


  <p>Cookie Counter: <span> <%= $self->session->{count} || "no cookie yet" 
%> </span> </p>


  <% $self->session->{count}= ($self->session->{count} || 0)+1; %>




  <h2> User Agent </h2>


  <p><span> <%= $c->req->headers->user_agent %> </p>






  <h2> Other Template Files and Defaults </h2>


  <p> For static files and exception handling, look at the end of the code. 
</p>


    <pre> morbo -v -m production </pre>


    then point to <a 
href="http://localhost:3000/bad";>http://localhost:3000/bad</a></p>




  <h2> ENV </h2>


  <pre>
    <% use Data::Dumper; %>
    <div style="background-color:white">
      <%== dumper \%ENV %>
    </div>
  </pre>








@@ does_not_exist.html.ep


% layout 'default';
% title 'redirect a 404';


ok, how do I get more info here?




@@ test.txt


this is basic test.txt, which can be viewed at 
http://localhost:3000/test.txt while in localhost development on port 3000


@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <style> table { border: 1px solid black; } span { 
background-color:white; padding:0.5ex; } </style>
  </head>


  <body style="margin:3em; background-color:beige">
    <%= content %>
  </body>
</html>




@@ exception.production.html.ep


<!DOCTYPE html>
<html>
  <head><title>Server error</title></head>
  <body style="background-color:orange">
    <h1>Exception</h1>
      <p><%= $exception->message %></p>
    <h1>Stash</h1>
      <pre><%= dumper $snapshot %></pre>
  </body>
</html>



-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to