Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm using Strawberry Perl, and in most cases I'm not using anything too complex or too UNIX-ish on Windows, at maximum I'm using fork. As for database, it's often SQLite DB (using Mojo::SQLite), there was once a project with database in DBF files, and now I'm writing a project with Firebird SQL server as a database (using DBD::ODBC and Firebird ODBC connector). Frontend is always some flavour of Vue.js.

To make things more like it's done on Windows, I'm using two files, START.CMD for starting whole project, which is:

@echo off set PATH=perl;%PATH% perl -Iperl\lib "script/project" pause

Here's project structure:

[lib] [perl] [public] [script] [templates] [uploads] db.sqlite db.sqlite-shm db.sqlite-wal init.sql START.CMD project.conf

Here you see that I rewrite %PATH% variable. It's my Electron-like application harness, in perl dir there's a stripped Strawberry Perl with modules to run on other Windows machines.

Then there is starter perl script,

#!perl use Mojo::Base -strict; use Mojo::Server::Morbo; use Mojo::Util qw(extract_usage getopt); my $morbo = Mojo::Server::Morbo->new; my $pid = fork(); if ($pid) { # PARENT $morbo->daemon->listen(["http://*:3000"]); $morbo->run(shift); } else { # CHILD use Socket; my $host = 'localhost'; my $port = '3000'; my $timeout = 30; my $started = 0; while (not $started) { sleep 1; $started = portAlive(); } system("start http://localhost:3000"); exit; sub portAlive { my $proto = getprotobyname('tcp'); my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || warn "socket: +$!"; eval { local $SIG{ALRM} = sub { die "timeout" }; alarm($timeout); connect(SOCKET, $paddr) || error(); alarm(0); }; if ($@) { close SOCKET || warn "close: $!"; print "$host is NOT listening on tcp port $port.\n"; return 0; } else { close SOCKET || warn "close: $!"; print "$host is listening on tcp port $port.\n"; return 1; } } }

When Morbo server started, script will start browser with URL of project, and voila. Also, there could be no black CMD.EXE windows at all with wperl.exe + WebSocket + Mojolicious trick:

$r->websocket('/bus' => sub { my $self = shift; $self->inactivity_timeout(1000); #$log->debug(sprintf 'Client connected: %s', $self->tx); my $id = sprintf "%s", $self->tx; $clients->{$id} = $self->tx; $self->on(message => sub { my ($self, $msg) = @_; for (keys %$clients) { $clients->{$_}->send({json => {message => $msg}}); } }); $self->on(finish => sub { #$log->debug('Client disconnected'); delete $clients->{$id}; Mojo::IOLoop->timer(10 => sub { my $loop = shift; if (not scalar keys %{ $clients }) { #$log->debug('No clients! exiting!'); exit; } else { #$log->debug('Have clients! not exiting!'); } }); }); } );

When last client disconnects from WebSocket (browser window/tab with project closes) - server exits.

In reply to Re^3: Constructive thoughts on Dancer2 v Mojolicious by alexander_lunev
in thread Constructive thoughts on Dancer2 v Mojolicious by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2021-01-19 06:06 GMT
    Find Nodes?
      Voting Booth?