Skip to content
Permalink
Browse files

Rewrite to a plack app

  • Loading branch information...
perlDreamer committed Jan 26, 2012
1 parent 46d5e27 commit ba701981308f74bb8da2e9d072774e1114a3bc6e
Showing with 47 additions and 49 deletions.
  1. +47 −49 wre/lib/WRE/WebguiDemo.pm
@@ -7,16 +7,11 @@ package WRE::WebguiDemo;
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------

use lib ('/data/wre/lib','/data/WebGUI/lib');
use strict;
use Apache2::Const;
use Apache2::RequestRec;
use Apache2::RequestUtil;
use Apache2::RequestIO;
use DBI;
use JSON;
use String::Random qw(random_string);
use WebGUI;
@@ -25,62 +20,68 @@ use WRE::Config;
use WRE::File;
use WRE::Mysql;

extends 'Plack::Component';
use Plack::Util::Accessor qw/wre_config/;

#-------------------------------------------------------------------
sub handler {
my $r = shift;
sub call {
my $env = shift;
my $config = WRE::Config->new;
$r->pnotes('wreConfig' => $config);
my $id = $r->uri;
$id =~ s/^\/(demo[0-9\_]+).*$/$1/;
if (-e $config->getWebguiRoot("/etc/".$id.".conf")) {
return WebGUI::handler($r,$id.".conf");
}
elsif ($r->uri =~ /^\/extras/) {
# just pass it on thru
}
elsif ($r->uri eq "/create") {
$r->set_handlers(PerlResponseHandler => \&createDemo);
$r->set_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
}
$self->wre_config($config);
my $r = Plack::Request->new($env);
my ($id) = $r->uri->host =~ m/^\/(demo[0-9\_]+).*$/;
my $webgui_config = $config->getWebguiRoot("/etc/".$id.".conf");
$self->response($response);
if (-e $webgui_config) {
## Code to use the WebGUI App with a particular config file.
local $ENV{WEBGUI_CONFIG} = $webgui_config;
my $psgi = WebGUI::Paths->defaultPSGI;
my $app = Plack::Util::load_psgi($psgi);
$app->call($env);
}
# Extras can be served from nginx
elsif ($r->uri->path eq "create") {
$response = $self->create_demo();
}
else {
$r->set_handlers(PerlResponseHandler => \&promptDemo);
$r->set_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
}
return Apache2::Const::DECLINED;
$response = $self->prompt_demo();
}
}


#-------------------------------------------------------------------
sub promptDemo {
my $r = shift;
my $config = $r->pnotes('wreConfig');
$r->content_type("text/html");
$r->print(q|<html><head><title>WebGUI Demo</title></head><body>
sub prompt_demo {
my $self = shift;
my $response = Plack::Response->new();
$response->content_type("text/html");
$response->status(200);
$response->body(q|<html><head><title>WebGUI Demo</title></head><body>
<div style="width: 300px; margin-top: 20%; text-align: left; margin-left: 35%; margin-bottom: 10px;
background-color: #cccccc; border: 1px solid #800000; padding: 10px; color: #000080;">If you'd like your own
personal demo of the <a style="color: #ff2200;" href="http://www.webgui.org/">WebGUI Content Engine&reg;</a>
click the button below. Your demo will last for |.$config->get("demo")->{duration}.q| day(s), then will be deleted.</div>
personal demo of the <a style="color: #ff2200;" href="http://www.webgui.org/">WebGUI Content Engine&reg;</a>
click the button below. Your demo will last for |.$self->wre_config->get("demo")->{duration}.q| day(s), then will be deleted.</div>
<div style="text-align: center; width: 300px; margin-left: 35%;"><form action="/create" method="post">
<input onclick="this.value='Please wait while we create your demo!'" type="submit"
<input onclick="this.value='Please wait while we create your demo!'" type="submit"
value="Create My Personal WebGUI Demo" /> </form></div> </body></html>|);
return Apache2::Const::OK;
return $response;
}

#-------------------------------------------------------------------
sub createDemo {
my $r = shift;
my $config = $r->pnotes('wreConfig');
my $now = time();
sub create_demo {
my $self = shift;
my $response = $self->response();
my $config = $self->wre_config;
my $now = time();
my $demo = $config->get("demo");
srand;
my $demoId = "demo".$now."_".int(rand(999));
srand;
my $demoId = "demo".$now."_".int(rand(999));
my $template = Template->new;
my $params = {};
my $file = WRE::File->new(wreConfig=>$config);

# manufacture stuff
$params->{databaseName} = $demoId;
$params->{databaseUser} = random_string("ccccccccccccccc");
$params->{databaseUser} = random_string("ccccccccccccccc");
$params->{databasePassword} = random_string("cCncCnCCncCncCnnnCcccnnCnc");
$params->{databaseHost} = $config->get("mysql")->{hostname};
$params->{databasePort} = $config->get("mysql")->{port};
@@ -116,18 +117,15 @@ sub createDemo {
);

# create webroot
$file->makePath($config->getDomainRoot('/demo/'.$demoId.'/uploads/'));
$file->makePath($config->getDomainRoot('/demo/'.$demoId.'/uploads/'));
$file->copy($demo->{creation}{uploads}.'/',
$config->getDomainRoot('/demo/'.$demoId.'/uploads/'),
{ force=>1, recursive=>1});
{ force=>1, recursive=>1});

# send redirect
$r->headers_out->set(Location => "/".$demoId."/");
$r->status(301);
return Apache2::Const::OK;
$response->location("/".$demoId."/");
$response->status(301);
return $response;
}




1;

0 comments on commit ba70198

Please sign in to comment.
You can’t perform that action at this time.