Search code examples
perlmojolicious

Embedding a Mojolicious::Server in a forked process


Working on a little netflow collector (like ntop) and I want to spawn a web server when my program starts (don't want to force folks to configure an external webserver). I'm having some trouble figuring out how to get my app started in the fork. Here's what I'm doing:

#This is basically what the child process is doing.
#running this outside of my fork does the same thing.
use myApp;
use Mojo::Server;
use Mojo::Server::Daemon;
use Mojolicious::Commands;
my $daemon = Mojo::Server::Daemon->new( listen => ['http://*:5656'] );

Mojolicious::Commands->start_app('myApp');

myApp.pm contains

sub startup
{
    my $self = shift();

    my $r = $self->routes;

    $r->get('/') => sub {
        my $self = shift;

        $self->render( text => "Howdy!!" );
    };

}

When I run this I get the Following. . .

usage: ./FlowTrack.pl COMMAND [OPTIONS]

Tip: CGI and PSGI environments can be automatically detected very often and
     work without commands.

These commands are currently available:
  cgi        Start application with CGI.
  cpanify    Upload distribution to CPAN.
  daemon     Start application with HTTP and WebSocket server.
  eval       Run code against application.
  generate   Generate files and directories from templates.
  get        Perform HTTP request.
.
.
etc
.

I haven't found docs/examples doing what I'm trying to do. I'm sure I'm just not looking in the right place.


Solution

  • Figured it out. Typing the question always seems to plant the seed of the fix, anyway, if someone else is trying to do this. (I still have an error in my application, that is stopping the test from working, but I have the server loop started)

    use MyApp;
    use Mojo::Server;
    use Mojo::Server::Daemon;
    use Mojolicious::Commands;
    
    my $daemon = Mojo::Server::Daemon->new( app => MyApp, listen => ['http://*:5656'] );
    
    $daemon->run();
    

    Finally found an example that put the app in the daemon new call. Then I realized that the new call probably didn't start the loop as well, so I dug a bit there. Considered deleting the question but I figured someone else may find it useful.