Skip to content

Commit ba70198

Browse files
committedJan 26, 2012
Rewrite to a plack app
1 parent 46d5e27 commit ba70198

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed
 

‎wre/lib/WRE/WebguiDemo.pm

+47-49
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ package WRE::WebguiDemo;
77
# (docs/license.txt) that came with this distribution before using
88
# this software.
99
#-------------------------------------------------------------------
10-
# http://www.plainblack.com info@plainblack.com
10+
# http://www.plainblack.com info@plainblack.com
1111
#-------------------------------------------------------------------
1212

1313
use lib ('/data/wre/lib','/data/WebGUI/lib');
1414
use strict;
15-
use Apache2::Const;
16-
use Apache2::RequestRec;
17-
use Apache2::RequestUtil;
18-
use Apache2::RequestIO;
19-
use DBI;
2015
use JSON;
2116
use String::Random qw(random_string);
2217
use WebGUI;
@@ -25,62 +20,68 @@ use WRE::Config;
2520
use WRE::File;
2621
use WRE::Mysql;
2722

23+
extends 'Plack::Component';
24+
use Plack::Util::Accessor qw/wre_config/;
25+
2826
#-------------------------------------------------------------------
29-
sub handler {
30-
my $r = shift;
27+
sub call {
28+
my $env = shift;
3129
my $config = WRE::Config->new;
32-
$r->pnotes('wreConfig' => $config);
33-
my $id = $r->uri;
34-
$id =~ s/^\/(demo[0-9\_]+).*$/$1/;
35-
if (-e $config->getWebguiRoot("/etc/".$id.".conf")) {
36-
return WebGUI::handler($r,$id.".conf");
37-
}
38-
elsif ($r->uri =~ /^\/extras/) {
39-
# just pass it on thru
40-
}
41-
elsif ($r->uri eq "/create") {
42-
$r->set_handlers(PerlResponseHandler => \&createDemo);
43-
$r->set_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
44-
}
30+
$self->wre_config($config);
31+
my $r = Plack::Request->new($env);
32+
my ($id) = $r->uri->host =~ m/^\/(demo[0-9\_]+).*$/;
33+
my $webgui_config = $config->getWebguiRoot("/etc/".$id.".conf");
34+
$self->response($response);
35+
if (-e $webgui_config) {
36+
## Code to use the WebGUI App with a particular config file.
37+
local $ENV{WEBGUI_CONFIG} = $webgui_config;
38+
my $psgi = WebGUI::Paths->defaultPSGI;
39+
my $app = Plack::Util::load_psgi($psgi);
40+
$app->call($env);
41+
}
42+
# Extras can be served from nginx
43+
elsif ($r->uri->path eq "create") {
44+
$response = $self->create_demo();
45+
}
4546
else {
46-
$r->set_handlers(PerlResponseHandler => \&promptDemo);
47-
$r->set_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
48-
}
49-
return Apache2::Const::DECLINED;
47+
$response = $self->prompt_demo();
48+
}
5049
}
5150

5251

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

6969
#-------------------------------------------------------------------
70-
sub createDemo {
71-
my $r = shift;
72-
my $config = $r->pnotes('wreConfig');
73-
my $now = time();
70+
sub create_demo {
71+
my $self = shift;
72+
my $response = $self->response();
73+
my $config = $self->wre_config;
74+
my $now = time();
7475
my $demo = $config->get("demo");
75-
srand;
76-
my $demoId = "demo".$now."_".int(rand(999));
76+
srand;
77+
my $demoId = "demo".$now."_".int(rand(999));
7778
my $template = Template->new;
7879
my $params = {};
7980
my $file = WRE::File->new(wreConfig=>$config);
8081

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

118119
# create webroot
119-
$file->makePath($config->getDomainRoot('/demo/'.$demoId.'/uploads/'));
120+
$file->makePath($config->getDomainRoot('/demo/'.$demoId.'/uploads/'));
120121
$file->copy($demo->{creation}{uploads}.'/',
121122
$config->getDomainRoot('/demo/'.$demoId.'/uploads/'),
122-
{ force=>1, recursive=>1});
123+
{ force=>1, recursive=>1});
123124

124125
# send redirect
125-
$r->headers_out->set(Location => "/".$demoId."/");
126-
$r->status(301);
127-
return Apache2::Const::OK;
126+
$response->location("/".$demoId."/");
127+
$response->status(301);
128+
return $response;
128129
}
129130

130-
131-
132-
133131
1;

0 commit comments

Comments
 (0)