/
CometDesktop.pm
312 lines (240 loc) · 8.29 KB
/
CometDesktop.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package CometDesktop;
use strict;
use warnings;
use CometDesktopX::Session;
use CometDesktop::Controller;
use DBI;
use DBIx::Simple;
use File::Spec;
use base 'Mojolicious';
our $VERSION = '2.01';
our $config = {};
# This method will run for each request
sub process {
my ( $self, $c ) = @_;
$c->config( $config );
# set the mojo version
$c->res->headers->header( 'X-Powered-By' => 'Mojo/'.$Mojo::VERSION );
if ( $self->mode eq 'development' ) {
# set the connection id, useful for debugging
$c->res->headers->header( 'X-Mojo-Connection' => $1 )
if ( $c->tx->connection =~ m/\(0x([^\)]+)\)$/ );
}
$self->dispatch( $c );
}
sub production_mode {
shift->log->level( 'error' );
}
sub development_mode {
shift->log->level( 'debug' );
}
# This method will run once at server start
sub startup {
my $self = shift;
$self->_load_config;
$self->_add_hooks;
$self->_set_routes;
$self->_configure_apps;
return;
}
sub _add_hooks {
my $self = shift;
$self->plugins->add_hook(
after_static_dispatch => sub {
my $c = $_[ 1 ];
# a file is about to be served, skip db and user obj setup
return if $c->res->code;
# database
my $dbh = DBI->connect( @{$config}{qw( db_interface db_user db_pass )} )
or die 'DBI connect failed';
# session
$c->session_store->store->dbh( $dbh );
# dbix
my $db = DBIx::Simple->connect( $dbh )
or die DBIx::Simple->error;
$c->db( $db );
# user class, holds weak ref to ctx
$c->user->init( $c );
}
)->add_hook(
after_dispatch => sub {
my $c = $_[ 1 ];
if ( $c->db ) {
warn "disconnecting from db\n";
$c->db->disconnect;
$c->db( undef );
$c->user( undef );
}
}
);
return;
}
sub _load_config {
my $self = shift;
# Use our own controller
$self->controller_class( 'CometDesktop::Controller' );
$config = $self->plugin( multi_config => {
files => [
'etc/comet-desktop.conf',
@Bootstrapper::configs
],
config => {
mojo_plugins => [],
mojo_types => {},
cometdesktop_plugins => [],
},
stash_key => 'config'
});
foreach( @{$config->{mojo_plugins}} ) {
s/-/_/g;
next if ( $_ eq 'static_fallback' );
warn "loading plugin:$_\n" if ( $self->mode eq 'development' );
$self->plugin( $_, $config );
}
if ( $config->{enable_stderr_log_redirect} ) {
my $log = $self->log;
# see EOF
tie *STDERR, 'Tie::Callback', sub {
my $line = "STDERR: ----------- $_[1]"; chomp( $line );
# TODO, use goto so the call stack is jumped a level
$log->debug( $line );
} unless tied *STDERR;
}
# use our json encoder
$self->renderer->add_handler(
json => sub {
my ( $r, $c, $output, $options ) = @_;
# uses the faster JSON/JSON::XS encoder if available
$$output = $c->json_encode( $options->{json} );
}
);
# template helper <%= ext_path %>
# TBD get this from a config file
$self->renderer->add_helper(
ext_version => sub { $config->{ext_version} }
);
if ( $config->{mojo_types} ) {
while( my ( $k, $v ) = each %{$config->{mojo_types}} ) {
$self->types->type( $k => $v );
}
}
# sessions
$self->secret( $config->{mojo_session_secret} )
if ( $config->{mojo_session_secret} );
$self->session->cookie_name( $config->{mojo_session_cookie_name} )
if ( $config->{mojo_session_cookie_name} );
$self->session->cookie_path( $config->{mojo_session_cookie_path} )
if ( $config->{mojo_session_cookie_path} );
$self->session->cookie_domain( $config->{mojo_session_cookie_domain} )
if ( $config->{mojo_session_cookie_domain} );
$self->session->default_expiration( $config->{mojo_session_default_expiration} )
if ( defined $config->{mojo_session_default_expiration} );
return;
}
sub _configure_apps {
my $self = shift;
my $db = DBIx::Simple->connect( @{$config}{qw( db_interface db_user db_pass )} )
or die DBIx::Simple->error;
$config->{cometdesktop_apps} = $db->query( 'SELECT * FROM apps' )->hashes;
$db->disconnect;
my $static = $self->plugin( static_fallback => $config );
foreach( @{$config->{cometdesktop_apps}} ) {
warn "configuring app $_->{app_id} : $_->{app_name} : $_->{app_desc}\n";
my $dir = $self->home->rel_dir( File::Spec->catfile( 'apps', $_->{app_name} ) );
my $public = File::Spec->catfile( $dir, 'public' );
my $routes = File::Spec->catfile( $dir, 'routes.conf' );
if ( -d $public ) {
warn "adding static fallback for apps/$_->{app_name}/ to $public\n";
$static->add( "/desktop/apps/$_->{app_name}/" => $public );
}
# if ( -e File::Spec->catfile( $dir, 'plugin.conf' ) ) {
# my $plug = $_->{app_name}; $plug =~ s/-/_/g;
# $self->plugin( $plug, $config );
# }
$self->_set_app_routes( $_, $routes ) if ( -e $routes );
}
if ( $self->mode eq 'development' ) {
require Data::Dumper;
warn Data::Dumper->Dump([$config],['config']);
}
return;
}
sub _set_app_routes {
my ( $self, $app_info, $file ) = @_;
$app_info->{app_path} = "/desktop/apps/$app_info->{app_name}/";
warn "route for $app_info->{app_path}\n";
# my $base = $self->routes->route( $app_info->{app_path} );
my $base = $self->routes;
my $cfg = $self->_json_config( $file, { %$app_info, routes => $base } );
$cfg = [$cfg] if defined $cfg && ref $cfg eq 'HASH';
$app_info->{routes} = $cfg;
if ( $self->mode eq 'development' ) {
warn Data::Dumper->Dump([$cfg],[$app_info->{app_name}.'_routes']);
}
return unless defined $cfg && ref $cfg eq 'ARRAY';
foreach ( @$cfg ) {
$_->{route} ||= '';
$_->{route} =~ s/^\///g if $_->{route} =~ m/^\//;
my $r = $base->route( $app_info->{app_path}.$_->{route} );
$r->parse( $_->{parse} ) if $_->{parse};
$r->pattern( $_->{pattern} ) if $_->{pattern};
$r->conditions( $_->{conditions} ) if $_->{conditions};
$r->over( $_->{over} ) if $_->{over};
$r->to( $_->{to} ) if $_->{to};
my $defaults ||= {};
$defaults->{controller} = $_->{controller} if $_->{controller};
$defaults->{action} = $_->{action} if $_->{action};
$r->to( $defaults ) if keys %$defaults;
$r->via( $_->{via} ) if $_->{via};
$r->name( $_->{name} ) if $_->{name};
$r->inline( 1 ) if $_->{inline};
$r->websocket( 1 ) if $_->{websocket};
}
return;
}
sub _set_routes {
my $self = shift;
$self->routes->route( '/' )->via( 'get' )->to({ callback => sub { shift->redirect_to( '/desktop/' ) } });
$self->routes->route( '/desktop' )->via( 'get' )->to( 'desktop#root' )->name( 'desktop' );
$self->routes->route( '/desktop/login' )->via( 'post' )->to( 'desktop#login' );
$self->routes->route( '/desktop/logout' )->via( 'post' )->to( 'desktop#logout' );
return;
}
sub _json_config {
my ( $self, $file, $template ) = @_;
# Slurp UTF-8 file
open FILE, "<:encoding(UTF-8)", $file
or die qq/Couldn't open config file "$file": $!/;
my $encoded = do { local $/; <FILE> };
close FILE;
# Instance
my $prepend = 'my $app = shift;';
# Be less strict
$prepend .= q/no strict 'refs'; no warnings 'redefine';/;
# Helper
$prepend .= "sub app; *app = sub { \$app };";
# Be strict again
$prepend .= q/use strict; use warnings;/;
# Render
my $mt = Mojo::Template->new($template || {});
$mt->prepend($prepend);
$encoded = $mt->render($encoded, $self);
# Parse
my $json = Mojo::JSON->new;
my $config = $json->decode($encoded);
my $error = $json->error;
die qq/Couldn't parse config file "$file": $error/ if !$config && $error;
return $config;
}
1;
package Tie::Callback;
use strict;
use warnings;
sub PRINT {
shift->( @_ );
}
sub TIEHANDLE {
bless pop, shift;
}
sub BINMODE {}
1;