Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created September 1, 2025 19:28
Show Gist options
  • Save briandfoy/cd9f6aea5be013c208c382a681952ad1 to your computer and use it in GitHub Desktop.
Save briandfoy/cd9f6aea5be013c208c382a681952ad1 to your computer and use it in GitHub Desktop.
A mojo server inside a program
#!perl
use v5.40;
use Mojolicious::Lite;
use Mojo::Server::Daemon;
use Mojo::UserAgent;
my $port = 3000;
my $pid = fork;
if( $pid == 0 ) {
local $SIG{INT} = sub {exit};
local $SIG{TERM} = sub {exit};
local $SIG{__WARN__} = sub {1};
get '/' => sub ($c) {
sleep 10;
$c->render(text => 'Hello from inside the program!');
};
local *STDOUT;
open STDOUT, '>>', '/dev/null';
my $app = app->log( Mojo::Log->new(path => '/dev/null') );
my $daemon = Mojo::Server::Daemon->new(app => $app, listen => ["http://127.0.0.1:$port"]);
$daemon->start;
Mojo::IOLoop->start;
}
else {
sleep 2;
my $ua = Mojo::UserAgent->new->inactivity_timeout(3);
my $tx = $ua->get( "http://127.0.0.1:$port/" );
if( eval {$tx->result} ) {
say "BODY: " . $tx->res->body;
}
else {
say "ERROR: $@";
}
kill 9, $pid;
waitpid $pid, 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment