Skip to content

Instantly share code, notes, and snippets.

@genneko
Last active June 28, 2019 04:36
Show Gist options
  • Save genneko/7c49e5c6bad9d1b6d8856417c7e00ed5 to your computer and use it in GitHub Desktop.
Save genneko/7c49e5c6bad9d1b6d8856417c7e00ed5 to your computer and use it in GitHub Desktop.
A bare minimum template for a Mojolicious Lite app.
log
hypnotoad.pid
#!/usr/bin/env perl
#
# app.pl --- Mojolicious::Lite app template
#
use strict;
use utf8;
use open qw(:std :utf8);
use Encode;
use Mojolicious::Lite;
#
# When using a reverse proxy to deploy the app,
# Set $BASEPATH_CANDIDATE to the path through which
# users access the app.
#
# Use url_for() to get the actual path.
#
my $BASEPATH = "";
my $BASEPATH_CANDIDATE = "/mojo-lite-template";
my $LISTEN_ADDRPORT = "0.0.0.0:3003";
get '/' => sub {
my $c = shift;
$c->render('single', page => 'top');
};
get '/:page' => sub {
my $c = shift;
my $page = $c->param('page');
$c->render('single', page => $page);
};
$ENV{'MOJO_REVERSE_PROXY'} = 1;
app->hook(before_dispatch => sub {
my $self = shift;
my $_prefix = $self->req->headers->header('X-ProxyPassReverse-UsePrefix');
if(defined $_prefix && lc $_prefix eq 'on') {
$BASEPATH = $BASEPATH_CANDIDATE;
my $prefix = shift @{$self->req->url->path->parts};
$self->req->url->base->path->parse("$BASEPATH/$prefix");
}
});
app->config(hypnotoad => {listen => ["http://$LISTEN_ADDRPORT"]});
app->start;
__DATA__
@@ single.html.ep
% layout 'common', pagetype => 'single';
<%
my $page = stash('page');
%>
<h1><%= $page %></h1>
<pre>Hi, <%= $page %>!</pre>
<ul>
% if($page eq "top"){
<li><a href="<%= url_for('/history') %>">history</a>
% }else{
<li><a href="<%= url_for('/') %>">top</a>
% }
</ul>
@@ layouts/common.html.ep
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><%= stash('title') %></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style>
pre { white-space: pre-wrap; }
</style>
</head>
<body>
<div class="container">
%= content;
</div><!-- container -->
</body>
</html>
#!/usr/bin/env perl
#
# app.pl --- Mojolicious::Lite app template
#
use strict;
use utf8;
binmode(STDOUT, ":utf8");
use Encode;
use Mojolicious::Lite;
#
# When using a reverse proxy to deploy the app,
# Set $BASEPATH_CANDIDATE to the path through which
# users access the app.
#
# Use url_for() to get the actual path.
#
my $BASEPATH = "";
my $BASEPATH_CANDIDATE = "/mojo-lite-template";
my $LISTEN_ADDRPORT = "0.0.0.0:3003";
get '/' => sub {
my $c = shift;
$c->render('single', page => 'top');
};
post '/' => sub {
my $c = shift;
my $title = $c->param('title');
my $url = $c->param('url');
$c->render('single', page => 'top', title => $title, url => $url);
};
$ENV{'MOJO_REVERSE_PROXY'} = 1;
app->hook(before_dispatch => sub {
my $self = shift;
my $_prefix = $self->req->headers->header('X-ProxyPassReverse-UsePrefix');
if(defined $_prefix && lc $_prefix eq 'on') {
$BASEPATH = $BASEPATH_CANDIDATE;
my $prefix = shift @{$self->req->url->path->parts};
$self->req->url->base->path->parse("$BASEPATH/$prefix");
}
});
app->config(hypnotoad => {listen => ["http://$LISTEN_ADDRPORT"]});
app->start;
__DATA__
@@ single.html.ep
% layout 'common', pagetype => 'single';
<h1>URL Clipper</h1>
%= form_for '/', method => 'post', begin
%= text_field 'title'
<br>
%= text_field 'url'
<br>
%= submit_button 'Submit'
% end
<pre><%= stash('title') . ': ' . stash('url') %></pre>
@@ layouts/common.html.ep
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><%= stash('title') %></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style>
pre { white-space: pre-wrap; }
</style>
</head>
<body>
<div class="container">
%= content;
</div><!-- container -->
</body>
</html>
#!/bin/sh
prog=$(basename $0)
bindir=$(dirname $(readlink -f $0))
if [ ! -e "$bindir/log" ]; then
echo "Creating log/ directory..." >&2
mkdir $bindir/log
fi
hypnotoad $bindir/app.pl
#!/bin/sh
prog=$(basename $0)
bindir=$(dirname $(readlink -f $0))
hypnotoad -s $bindir/app.pl
#!/bin/sh
prog=$(basename $0)
bindir=$(dirname $(readlink -f $0))
morbo -l http://0.0.0.0:3000 $bindir/app.pl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment