Last active
December 14, 2015 22:08
-
-
Save Revlin/5155856 to your computer and use it in GitHub Desktop.
The following is some really basic explorations of the capabilities of Mojolicious::Lite -- I will update as I find out more
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -wT | |
use Mojolicious::Lite; | |
# The following is some really basic explorations of the capabilities of | |
# Mojolicious::Lite -- I will update as I find out more and a living (or | |
# more alive) version of this script is available on gist @ | |
# https://gist.github.com/Revlin/5155856 | |
my $app = app; | |
my $version = Mojolicious->VERSION; | |
get '/' => sub { | |
my $self = shift; | |
#1) How to get info about the host environment(something like CGI's @ENV) | |
# Some host environment info is modeled by Mojo::Message::Request | |
# which I guess is intended to emulate the HTTP request process | |
my $req = $self->req; | |
# $req can now be used to access other objects like header, url, | |
# and env, this last being a hash which is equivalent to @ENV, if | |
# running Mojolicious within a CGI environment [ with app->start('cgi') ] | |
my $URL = $req->url; # now we should be able to display parts of the URL... | |
say $URL; # ...but running on a local server, this returns "/" | |
say $URL->host; # and this is actually uninitialized | |
#2) How to use inline text to "render" HTML | |
$self->render( inline => <<HTML ); | |
<!DOCTYPE html> | |
<html><head> | |
<title>Mojolicious Title App</title> | |
</head> | |
<body> | |
Mojolicious: $version <br /> | |
Reading: $URL | |
</body></html> | |
HTML | |
# Not the simplest way to render this content, since the output of the | |
# following is equivallent to the inline heredoc used above... | |
#$self->render( text => "Mojolicious: $version\n Reading: $URL" ); | |
# K, this is the end of the / route | |
}; | |
#3) A more consistent method of getting info about request headers | |
get '/mojolicious' => sub { | |
my $self = shift; | |
my $request = join("&", ( | |
'accept: '.$self->req->headers->accept, | |
'accept_encoding: '.$self->req->headers->accept_encoding, | |
'accept_charset: '.$self->req->headers->accept_charset, | |
'accept_language: '.$self->req->headers->accept_language, | |
'accept_ranges: '.$self->req->headers->accept_ranges, | |
'authorization: '.$self->req->headers->authorization, | |
'cache_control: '.$self->req->headers->cache_control, | |
'connection: '.$self->req->headers->connection, | |
'content_disposition: '.$self->req->headers->content_disposition, | |
'content_encoding: '.$self->req->headers->content_encoding, | |
'content_length: '.$self->req->headers->content_length, | |
'content_range: '.$self->req->headers->content_range, | |
'content_type: '.$self->req->headers->content_type, | |
'cookie: '.$self->req->headers->cookie, | |
'date: '.$self->req->headers->date, | |
'dnt: '.$self->req->headers->dnt, | |
'etag: '.$self->req->headers->etag, | |
'expect: '.$self->req->headers->expect, | |
'expires: '.$self->req->headers->expires, | |
'host: '.$self->req->headers->host, | |
'if_modified_since: '.$self->req->headers->if_modified_since, | |
'is_finished: '.$self->req->headers->is_finished, | |
'is_limit_exceded: '.$self->req->headers->is_limit_exceeded, | |
'last_modified: '.$self->req->headers->last_modified, | |
'leftovers: '.$self->req->headers->leftovers, | |
'location: '.$self->req->headers->location, | |
'names: '.(join " ", sort @{$self->req->headers->names} ), | |
'origin: '.$self->req->headers->origin, | |
'proxy_authenticate: '.$self->req->headers->proxy_authenticate, | |
'proxy_authorization: '.$self->req->headers->proxy_authorization, | |
'range: '.$self->req->headers->range, | |
'referrer: '.$self->req->headers->referrer, | |
'sec_websocket_accept: '.$self->req->headers->sec_websocket_accept, | |
'sec_websocket_extensions: '.$self->req->headers->sec_websocket_extensions, | |
'sec_websocket_key: '.$self->req->headers->sec_websocket_key, | |
'sec_websocket_protocol: '.$self->req->headers->sec_websocket_protocol, | |
'sec_websocket_version: '.$self->req->headers->sec_websocket_version, | |
'server: '.$self->req->headers->server, | |
'set_cookie: '.$self->req->headers->set_cookie, | |
'status: '.$self->req->headers->status, | |
'te: '.$self->req->headers->te, | |
'trailer: '.$self->req->headers->trailer, | |
'transfer_encoding: '.$self->req->headers->transfer_encoding, | |
'upgrade: '.$self->req->headers->upgrade, | |
'user_agent: '.$self->req->headers->user_agent, | |
'www_authenticate: '.$self->req->headers->www_authenticate, | |
) ); | |
#4) How to use EP templates to "render" HTML | |
$self->stash(message=>"la la la la"); | |
$self->render('mojo', request=>$request); | |
# Go to http://localhost:3000/mojolicious to see these header values | |
}; | |
#5) How to make Mojolicious search the project's root directory for static files | |
my $static = $app->static; | |
push @{$static->paths}, ($ENV{PWD}); | |
# i.e. try navigating to http://localhost:3000/mojo-lessons.pl to see this source | |
# Now we'll start 'er up | |
$app->secrets('p@$$w0rd'); | |
$app->start(); | |
#$app->start('cgi'); | |
# this other version of start() is for running as CGI script | |
__DATA__ | |
@@ mojo.html.ep | |
<!DOCTYPE html> | |
<html><head> | |
<title>Mojolicious Title App</title> | |
</head> | |
<body> | |
<br /><br /><br /> | |
Welcome to the Mojolicious real-time web framework!<br /> | |
This page was generated from the template "mojo.html.ep"<br /> | |
<a href="<%== url_for %>">Click here</a> to reload the page. | |
<br /> | |
<br /> | |
<br /> | |
<% foreach my $parm (split '&', $request ) { %> | |
<%= $parm %><br /> | |
<% } %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment