1 |
package Webpacus; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
# |
7 |
# Set flags and add plugins for the application |
8 |
# |
9 |
# -Debug: activates the debug mode for very useful log messages |
10 |
# Static::Simple: will serve static files from the applications root directory |
11 |
# |
12 |
use Catalyst qw/-Debug |
13 |
Prototype |
14 |
Config::YAML |
15 |
FillInForm |
16 |
SubRequest |
17 |
Static::Simple |
18 |
StackTrace |
19 |
Dumper |
20 |
/; |
21 |
# FormValidator |
22 |
|
23 |
use Encode qw/is_utf8 decode/; |
24 |
|
25 |
our $VERSION = '0.37-dev'; |
26 |
|
27 |
# |
28 |
# Configure the application |
29 |
# |
30 |
__PACKAGE__->config( |
31 |
name => 'Webpacus', |
32 |
config_file => 'config.yml', |
33 |
); |
34 |
|
35 |
# |
36 |
# Start the application |
37 |
# |
38 |
__PACKAGE__->setup; |
39 |
|
40 |
=head1 NAME |
41 |
|
42 |
Webpacus - Catalyst based application |
43 |
|
44 |
=head1 SYNOPSIS |
45 |
|
46 |
script/webpacus_server.pl |
47 |
|
48 |
=head1 DESCRIPTION |
49 |
|
50 |
Catalyst based application. |
51 |
|
52 |
=head1 METHODS |
53 |
|
54 |
=over 4 |
55 |
|
56 |
=item default |
57 |
|
58 |
=cut |
59 |
|
60 |
sub default : Private { |
61 |
my ( $self, $c ) = @_; |
62 |
$c->stash->{template} = 'index.tt'; |
63 |
|
64 |
my $site_name = shift @{ $c->req->args } || ''; |
65 |
|
66 |
$c->log->debug("trying site name: $site_name"); |
67 |
|
68 |
if (my $sites_root = $c->config->{sites_root}) { |
69 |
my $site_path = $c->path_to('root', $sites_root, $site_name); |
70 |
|
71 |
if (-e $site_path) { |
72 |
$c->stash->{site} = $site_name; |
73 |
if (my $action = join('/', @{ $c->req->args })) { |
74 |
$c->log->debug( "site: $site_name, action: $action" ); |
75 |
# do a subrequest |
76 |
$c->res->body( $c->subreq( $action, $c->stash, @{ $c->req->args } ) ); |
77 |
} else { |
78 |
$c->log->debug( "site: $site_name" ); |
79 |
} |
80 |
} else { |
81 |
$c->log->debug( "can't find site $site_name in $site_path"); |
82 |
} |
83 |
} else { |
84 |
$c->log->warn("sites_root not defined in config"); |
85 |
} |
86 |
} |
87 |
|
88 |
=item end |
89 |
|
90 |
=cut |
91 |
|
92 |
sub end : Private { |
93 |
my ( $self, $c ) = @_; |
94 |
|
95 |
$c->stash->{webpacus_version} = $VERSION; |
96 |
|
97 |
if ($c->response->body) { |
98 |
$c->log->debug("at end body exists, we won't touch it!"); |
99 |
} else { |
100 |
if (my $site_name = $c->stash->{site}) { |
101 |
my $template_path = $c->path_to('root', $c->config->{sites_root}, $site_name, $c->stash->{template}); |
102 |
if (-e $template_path) { |
103 |
$c->stash->{template} = $c->config->{sites_root} . "/$site_name/" . $c->stash->{template}; |
104 |
} else { |
105 |
$c->log->warn("can't find $template_path, fallback to default"); |
106 |
} |
107 |
$c->log->debug("using template " . $c->stash->{template}); |
108 |
|
109 |
$c->stash->{site_url} = "/$site_name/"; |
110 |
|
111 |
#$c->log->dumper( $c->stash, 'stash' ); |
112 |
|
113 |
} |
114 |
|
115 |
# Forward to View unless response body is already defined |
116 |
$c->forward('View::TT'); |
117 |
|
118 |
# oh, turn utf8 flag for all parametars (why do I need to do this?) |
119 |
my $utf8_params; |
120 |
map { |
121 |
$utf8_params->{$_} = decode('UTF-8', $c->request->params->{$_}); |
122 |
} keys %{ $c->request->params }; |
123 |
|
124 |
# and fill-in form with new UTF-8 encoded params (to prevent double-escape of UTF-8) |
125 |
$c->fillform( $utf8_params ); |
126 |
|
127 |
$c->response->{body} =~ s#\Qnew Ajax.Autocompleter(\E#new WebPAC.Suggest(#gs; |
128 |
$c->response->{body} =~ s#\Qnew Ajax.Updater(\E#new WebPAC.Updater(#gs; |
129 |
|
130 |
# $c->log->debug( $c->response->body ); |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
=back |
136 |
|
137 |
=head1 AUTHOR |
138 |
|
139 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
140 |
|
141 |
=head1 LICENSE |
142 |
|
143 |
This library is free software, you can redistribute it and/or modify |
144 |
it under the same terms as Perl itself. |
145 |
|
146 |
=cut |
147 |
|
148 |
1; |