Browse Source

We're a Dancer2 app now

master
Lucas 6 months ago
parent
commit
2158e08908
9 changed files with 184 additions and 99 deletions
  1. +0
    -99
      Pastry.pl
  2. +22
    -0
      bin/app.psgi
  3. +17
    -0
      config.yml
  4. +36
    -0
      environments/development.yml
  5. +29
    -0
      environments/production.yml
  6. +35
    -0
      lib/Pastry.pm
  7. +5
    -0
      views/404.tt
  8. +36
    -0
      views/layouts/main.tt
  9. +4
    -0
      views/upload_form.tt

+ 0
- 99
Pastry.pl View File

@ -1,99 +0,0 @@
#!/usr/bin/env perl
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
use v5.12;
use strict;
use warnings;
use IO::Handle;
use File::Temp qw(tempfile);
use Scalar::Util qw(openhandle);
use FCGI;
use FCGI::ProcManager;
my $UPLOAD_DIR = "./u";
my $DOMAIN = "example.com";
sub err_400 (*@)
{
my ($outh, @msg) = @_;
print $outh "Content-Type: text/plain\n";
print $outh "Status: 400 Bad Request\n";
print $outh "\n";
print $outh (@msg ? @msg : "Bad Request"), "\n";
}
sub err_405 (*)
{
my $outh = shift;
print $outh "Content-Type: text/plain\n";
print $outh "Status: 405 Method Not Allowed\n";
print $outh "\n";
print $outh "Only GET and POST are supported\n";
}
sub err_500 (*@)
{
my ($outh, @msg) = @_;
print $outh "Content-Type: text/plain\n";
print $outh "Status: 500 Internal Server Error\n";
print $outh "\n";
print $outh (@msg ? @msg : "Internal Server Error"), "\n";
}
sub handle_request ($$)
{
my ($req, $cgi) = @_;
my %env = %{$req->GetEnvironment()};
my ($inh, $outh, $errh) = $req->GetHandles();
if (($env{"REQUEST_METHOD"} || "") !~ m/^(get|post)$/i) {
err_405 $outh;
return;
}
print $outh "Content-Type: text/plain\n";
print $outh "\n";
print $outh "Hellow world\n";
}
sub main ()
{
my $sock = FCGI::OpenSocket(":9090", 5);
my $pm = FCGI::ProcManager->new({
n_processes => 2,
pm_title => "pastry-fcgi-pm",
});
$pm->pm_manage();
my %env;
my $req = FCGI::Request(IO::Handle->new(), IO::Handle->new(),
IO::Handle->new(), \%env, $sock, FCGI::FAIL_ACCEPT_ON_INTR);
my $cgi = undef;
while ($req->Accept() >= 0) {
$pm->pm_pre_dispatch();
handle_request($req, $cgi);
$req->Finish();
$pm->pm_post_dispatch();
}
FCGI::CloseSocket($sock);
}
main();

+ 22
- 0
bin/app.psgi View File

@ -0,0 +1,22 @@
#!/usr/bin/env perl
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Pastry;
Pastry->to_app;

+ 17
- 0
config.yml View File

@ -0,0 +1,17 @@
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
appname: "Pastry"
layout: "main"
charset: "UTF-8"
template: "simple"

+ 36
- 0
environments/development.yml View File

@ -0,0 +1,36 @@
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
# configuration file for development environment
# the logger engine to use
# console: log messages to STDOUT (your console where you started the
# application server)
# file: log message to a file in log/
logger: "console"
# the log level for this environment
# core is the lowest, it shows Dancer2's core log messages as well as yours
# (debug, info, warning and error)
log: "core"
# should Dancer2 consider warnings as critical errors?
warnings: 1
# should Dancer2 show a stacktrace when an 5xx error is caught?
# if set to yes, public/500.html will be ignored and either
# views/500.tt, 'error_template' template, or a default error template will be used.
show_errors: 1
# print the banner
startup_info: 1

+ 29
- 0
environments/production.yml View File

@ -0,0 +1,29 @@
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
# configuration file for production environment
# only log warning and error messsages
log: "warning"
# log message to a file in logs/
logger: "file"
# don't consider warnings critical
warnings: 0
# hide errors
show_errors: 0
# disable server tokens in production environments
no_server_tokens: 1

+ 35
- 0
lib/Pastry.pm View File

@ -0,0 +1,35 @@
# Pastry - paste service
#
# Written in 2020 by Lucas
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
package Pastry;
use Dancer2;
our $VERSION = v0.1;
hook before_template_render => sub {
my $tokens = shift;
my $env_s = "";
foreach my $k (sort keys %{$tokens->{"request"}->env}) {
$env_s .= $k . "=" . %{$tokens->{"request"}->env}{$k} . "\n";
}
$tokens->{"is_development"} = 1;
$tokens->{"env_s"} = $env_s;
};
get "/" => sub {
return template "upload_form" => {};
};
1;

+ 5
- 0
views/404.tt View File

@ -0,0 +1,5 @@
<p>Your princess is in another route! Try the <a href="/">root</a> instead.</p>
<pre><code>404 Not Found
<% request.to_string %>
</code></pre>

+ 36
- 0
views/layouts/main.tt View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<!--
Pastry - paste service
Written in 2020 by Lucas
To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any
warranty.
You should have received a copy of the CC0 Public Domain Dedication
along with this software. If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
-->
<html lang="en">
<head>
<meta charset="<% settings.charset %>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Pastry</title>
<link rel="stylesheet" href="<% request.uri_base %>/css/style.css">
</head>
<body>
<h1>Pastry</h1>
<% content %>
<% if is_development %><pre><code><% env_s %></code></pre><% end %>
<footer>
<p>Powered by <a href="http://perldancer.org/">Dancer2</a> <% dancer_version %></p>
</footer>
</body>
</html>

+ 4
- 0
views/upload_form.tt View File

@ -0,0 +1,4 @@
<form enctype="multipart/form-data" method="POST">
<label>File: <input type="file" name="upload" /></label>
<input type="submit" />
</form>