From ee93a98d357c5e4ca470b70e31c0e02d15e2c8b2 Mon Sep 17 00:00:00 2001 From: Maximilian Gass Date: Mon, 27 Jun 2011 14:25:19 +0200 Subject: [PATCH] Port to Dancer --- MANIFEST | 19 +++++++++ MANIFEST.SKIP | 13 ++++++ Makefile.PL | 21 ++++++++++ app.psgi | 67 ------------------------------ bin/app.pl | 5 +++ config.yml | 9 ++++ environments/development.yml | 27 ++++++++++++ environments/production.yml | 17 ++++++++ genres.yaml => genres.yml | 0 index.tt | 28 ------------- lib/App/Genregenerator.pm | 28 +++++++++++++ public/404.html | 18 ++++++++ public/500.html | 18 ++++++++ public/css/error.css | 70 ++++++++++++++++++++++++++++++++ {static => public/css}/style.css | 0 public/dispatch.cgi | 15 +++++++ public/dispatch.fcgi | 17 ++++++++ public/javascripts/jquery.js | 1 + t/001_base.t | 5 +++ t/002_index_route.t | 10 +++++ views/index.tt | 28 +++++++++++++ 21 files changed, 321 insertions(+), 95 deletions(-) create mode 100644 MANIFEST create mode 100644 MANIFEST.SKIP create mode 100644 Makefile.PL delete mode 100644 app.psgi create mode 100755 bin/app.pl create mode 100644 config.yml create mode 100644 environments/development.yml create mode 100644 environments/production.yml rename genres.yaml => genres.yml (100%) delete mode 100644 index.tt create mode 100644 lib/App/Genregenerator.pm create mode 100644 public/404.html create mode 100644 public/500.html create mode 100644 public/css/error.css rename {static => public/css}/style.css (100%) create mode 100755 public/dispatch.cgi create mode 100755 public/dispatch.fcgi create mode 120000 public/javascripts/jquery.js create mode 100644 t/001_base.t create mode 100644 t/002_index_route.t create mode 100644 views/index.tt diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..025265d --- /dev/null +++ b/MANIFEST @@ -0,0 +1,19 @@ +MANIFEST +bin/app.pl +config.yml +environments/development.yml +environments/production.yml +views/index.tt +views/layouts/main.tt +MANIFEST.SKIP +lib/genregenerator.pm +public/css/style.css +public/css/error.css +public/404.html +public/dispatch.fcgi +public/javascripts/jquery.js +public/dispatch.cgi +public/500.html +t/002_index_route.t +t/001_base.t +Makefile.PL diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..8fd3d29 --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1,13 @@ +^\.git\/ +maint +^tags$ +.last_cover_stats +Makefile$ +^blib +^pm_to_blib +^.*.bak +^.*.old +^t.*sessions +^cover_db +^.*\.log +^.*\.swp$ diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..446081e --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,21 @@ +use strict; +use warnings; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'genregenerator', + AUTHOR => q{YOUR NAME }, + VERSION_FROM => 'lib/genregenerator.pm', + ABSTRACT => 'YOUR APPLICATION ABSTRACT', + ($ExtUtils::MakeMaker::VERSION >= 6.3002 + ? ('LICENSE'=> 'perl') + : ()), + PL_FILES => {}, + PREREQ_PM => { + 'Test::More' => 0, + 'YAML' => 0, + 'Dancer' => 1.3060, + }, + dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, + clean => { FILES => 'genregenerator-*' }, +); diff --git a/app.psgi b/app.psgi deleted file mode 100644 index 7459555..0000000 --- a/app.psgi +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env perl -# Copyright 2011 Maximilian Gaß -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -use strict; -use warnings; - -use Data::Random qw(rand_enum); -use Plack::App::File; -use Plack::Builder; -use YAML::XS qw(LoadFile); -use Template; - -chdir ('/srv/www/de.genregenerator'); -my $data = LoadFile('genres.yaml'); - -my $template = Template->new({ - INCLUDE_PATH => '.', -}); - -sub chance { - my ($percent) = @_; - return int(rand(100 / $percent)) == 0; -} - -sub generate_genre { - my @genre; - - if (chance(10)) { - push @genre, rand_enum(set => $data->{cities}); - } - - if (chance(70)) { - push @genre, rand_enum(set => $data->{subgroup}); - } - - - push @genre, rand_enum(set => $data->{subgroup}); - push @genre, rand_enum(set => $data->{group}); - - return join ' ', @genre; -} - -builder { - mount '/static' => Plack::App::File->new(root => 'static')->to_app(), - mount '/' => sub { - my $genre = generate_genre(); - my $out = ''; - $template->process('index.tt', { genre => $genre }, \$out); - return [ - 200, - [ 'Content-Type' => 'text/html' ], - [ $out ] - ]; - }, -} diff --git a/bin/app.pl b/bin/app.pl new file mode 100755 index 0000000..4cfac2f --- /dev/null +++ b/bin/app.pl @@ -0,0 +1,5 @@ +#!/usr/bin/env perl +use Dancer; +use App::Genregenerator; + +dance; diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..b8e808c --- /dev/null +++ b/config.yml @@ -0,0 +1,9 @@ +# Your application's name +appname: "genregenerator" + +# when the charset is set to UTF-8 Dancer will handle for you +# all the magic of encoding and decoding. You should not care +# about unicode within your app when this setting is set (recommended). +charset: "UTF-8" + +template: "simple" diff --git a/environments/development.yml b/environments/development.yml new file mode 100644 index 0000000..bf9f228 --- /dev/null +++ b/environments/development.yml @@ -0,0 +1,27 @@ +# 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 environement +# core is the lowest, it shows Dancer's core log messages as well as yours +# (debug, warning and error) +log: "core" + +# should Dancer consider warnings as critical errors? +warnings: 1 + +# should Dancer show a stacktrace when an error is caught? +show_errors: 1 + +# auto_reload is a development and experimental feature +# you should enable it by yourself if you want it +# Module::Refresh is needed +# +# Be aware it's unstable and may cause a memory leak. +# DO NOT EVER USE THAT FEATURE IN PRODUCTION +# OR TINY KITTENS SHALL DIE WITH LOTS OF SUFFERING +auto_reload: 0 diff --git a/environments/production.yml b/environments/production.yml new file mode 100644 index 0000000..629973f --- /dev/null +++ b/environments/production.yml @@ -0,0 +1,17 @@ +# configuration file for production environment + +# only log warning and error messsages +log: "warning" + +# log message to a file in logs/ +logger: "null" + +# don't consider warnings critical +warnings: 0 + +# hide errors +show_errors: 0 + +# cache route resolution for maximum performance +route_cache: 1 + diff --git a/genres.yaml b/genres.yml similarity index 100% rename from genres.yaml rename to genres.yml diff --git a/index.tt b/index.tt deleted file mode 100644 index 696d98c..0000000 --- a/index.tt +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - -
- Listen to -
- - - -
-
-
- -
- Created by xeno and mxey | git -
- - - diff --git a/lib/App/Genregenerator.pm b/lib/App/Genregenerator.pm new file mode 100644 index 0000000..8590cd0 --- /dev/null +++ b/lib/App/Genregenerator.pm @@ -0,0 +1,28 @@ +package App::Genregenerator; +use Dancer ':syntax'; +use Data::Random qw(rand_enum); +use YAML::XS qw(LoadFile); + +my $data = LoadFile('genres.yml'); + +sub chance { + my ($percent) = @_; + return int(rand(100 / $percent)) == 0; +} + +sub generate_genre { + my @genre; + + push @genre, rand_enum(set => $data->{cities}) if chance 10; + push @genre, rand_enum(set => $data->{subgroup}) if chance 70; + push @genre, rand_enum(set => $data->{subgroup}); + push @genre, rand_enum(set => $data->{group}); + + return join ' ', @genre; +} + +get '/' => sub { + template 'index', { genre => generate_genre() }; +}; + +true; diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..c314550 --- /dev/null +++ b/public/404.html @@ -0,0 +1,18 @@ + + + +Error 404 + + + + +

Error 404

+
+

Page Not Found

Sorry, this is the void.

+
+ + + \ No newline at end of file diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000..6563a85 --- /dev/null +++ b/public/500.html @@ -0,0 +1,18 @@ + + + +Error 500 + + + + +

Error 500

+
+

Internal Server Error

Wooops, something went wrong

+
+ + + \ No newline at end of file diff --git a/public/css/error.css b/public/css/error.css new file mode 100644 index 0000000..003ee2a --- /dev/null +++ b/public/css/error.css @@ -0,0 +1,70 @@ +body { + font-family: Lucida,sans-serif; +} + +h1 { + color: #AA0000; + border-bottom: 1px solid #444; +} + +h2 { color: #444; } + +pre { + font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace; + font-size: 12px; + border-left: 2px solid #777; + padding-left: 1em; +} + +footer { + font-size: 10px; +} + +span.key { + color: #449; + font-weight: bold; + width: 120px; + display: inline; +} + +span.value { + color: #494; +} + +/* these are for the message boxes */ + +pre.content { + background-color: #eee; + color: #000; + padding: 1em; + margin: 0; + border: 1px solid #aaa; + border-top: 0; + margin-bottom: 1em; +} + +div.title { + font-family: "lucida console","monaco","andale mono","bitstream vera sans mono","consolas",monospace; + font-size: 12px; + background-color: #aaa; + color: #444; + font-weight: bold; + padding: 3px; + padding-left: 10px; +} + +pre.content span.nu { + color: #889; + margin-right: 10px; +} + +pre.error { + background: #334; + color: #ccd; + padding: 1em; + border-top: 1px solid #000; + border-left: 1px solid #000; + border-right: 1px solid #eee; + border-bottom: 1px solid #eee; +} + diff --git a/static/style.css b/public/css/style.css similarity index 100% rename from static/style.css rename to public/css/style.css diff --git a/public/dispatch.cgi b/public/dispatch.cgi new file mode 100755 index 0000000..3bb7f2a --- /dev/null +++ b/public/dispatch.cgi @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use Dancer ':syntax'; +use FindBin '$RealBin'; +use Plack::Runner; + +# For some reason Apache SetEnv directives dont propagate +# correctly to the dispatchers, so forcing PSGI and env here +# is safer. +set apphandler => 'PSGI'; +set environment => 'production'; + +my $psgi = path($RealBin, '..', 'bin', 'app.pl'); +die "Unable to read startup script: $psgi" unless -r $psgi; + +Plack::Runner->run($psgi); diff --git a/public/dispatch.fcgi b/public/dispatch.fcgi new file mode 100755 index 0000000..8c42e3a --- /dev/null +++ b/public/dispatch.fcgi @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use Dancer ':syntax'; +use FindBin '$RealBin'; +use Plack::Handler::FCGI; + +# For some reason Apache SetEnv directives dont propagate +# correctly to the dispatchers, so forcing PSGI and env here +# is safer. +set apphandler => 'PSGI'; +set environment => 'production'; + +my $psgi = path($RealBin, '..', 'bin', 'app.pl'); +my $app = do($psgi); +die "Unable to read startup script: $@" if $@; +my $server = Plack::Handler::FCGI->new(nproc => 5, detach => 1); + +$server->run($app); diff --git a/public/javascripts/jquery.js b/public/javascripts/jquery.js new file mode 120000 index 0000000..b77fd86 --- /dev/null +++ b/public/javascripts/jquery.js @@ -0,0 +1 @@ +/usr/share/javascript/jquery/jquery.js \ No newline at end of file diff --git a/t/001_base.t b/t/001_base.t new file mode 100644 index 0000000..06a2749 --- /dev/null +++ b/t/001_base.t @@ -0,0 +1,5 @@ +use Test::More tests => 1; +use strict; +use warnings; + +use_ok 'App::Genregenerator'; diff --git a/t/002_index_route.t b/t/002_index_route.t new file mode 100644 index 0000000..98f6abf --- /dev/null +++ b/t/002_index_route.t @@ -0,0 +1,10 @@ +use Test::More tests => 2; +use strict; +use warnings; + +# the order is important +use App::Genregenerator; +use Dancer::Test; + +route_exists [GET => '/'], 'a route handler is defined for /'; +response_status_is ['GET' => '/'], 200, 'response status is 200 for /'; diff --git a/views/index.tt b/views/index.tt new file mode 100644 index 0000000..3770a01 --- /dev/null +++ b/views/index.tt @@ -0,0 +1,28 @@ + + + + + + + + + +
+ Listen to +
+ + + +
+
+
+ +
+ Created by xeno and mxey | git +
+ + +