perl - how to pass arguments from an plack app to an mojolicious app enabled in builder? -


given example plack app using lots of middleware components , mojolicious app enabled in builder (see below), how can pass parameters app.psgi mojolicious without using ugly %env hack shown? of cause passing config example, scalar/object.

app.psgi

use plack::builder;  $env{config} = {...};  builder {     ...     mojolicious::commands->start_app('myapp'); }; 

myapp.pm

package myapp;  use mojo::base 'mojolicious';  sub startup {      $self = shift;     $r = $self->routes;      $self->config( $env{config} );      $r->route('/')->to('home#');         } 

this interesting question , tackled looking @ source. in example rightly use

mojolicious::commands->start_app('myapp'); 

looking @ source shows start_app rather simple wrapper:

sub start_app {   $self = shift;   return mojo::server->new->build_app(shift)->start(@_); } 

it turns out build_app as well:

sub build_app {   ($self, $app) = @_;   local $env{mojo_exe};   return $app->new unless $e = mojo::loader->new->load($app);   die ref $e ? $e : qq{couldn't find application class "$app".\n}; } 

returning new instance of app's class. mojolicious class's new function more involved, in end, it calls familiar startup method , returns instance.

this means cannot pass arguments startup method middleware wrapper, used in standard way. can think of 2 mechanisms accomplish want do: 1) write own build_app function replace server's method passes arguments $app->new (which passed startup in turn) or 2) write own start_app function call startup-like function.

# in myapp.pm  sub startup {   ... # before }  sub after_startup {   ... # new code here,       # or of in `startup` before } 

and

# app.psgi  builder {   ...   $app = mojo::server->new->build_app(shift);   $app->after_startup(@your_args_here);   $app->start(@_); } 

Comments