karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
a while ago i wrote Mojolicious::Lite: Should i use the build-in web server?.
In the last months I didn't have the time to dive a bit deeper into this stuff.
But anyway, one must start somehow, regardless how poor the first approach might be.
Here is what did:
package Nose; use XML::LibXML; use Moo::Role; my $xml = sub { my ( $self, $params ) = @_; # you really need $self my $doc = XML::LibXML::Document->new( '1.0', 'utf-8' ); my $root = $doc->createElement("myapp"); my $tag = $doc->createElement("nose"); my $value = $params->{"nose"}; $tag->appendTextNode($value); $root->appendChild($tag); $doc->setDocumentElement($root); $doc->toString(); }; sub nose { my ( $self, $params ) = @_; $self->$xml($params); } 1; __END__
package Karl; use Moo; with qw(Nose); 1; __END__
#!/usr/bin/env perl use Mojolicious::Lite; use Karl; my $moo = Karl->new(); get '/foo' => sub { my $c = shift; my $params->{nose} = $c->param('nose'); $c->render( text => $moo->nose($params) ); }; app->start; __END__
#!/usr/bin/env perl use strict; use warnings; use Mojo::Server::Hypnotoad; my $app = q(/Users/karl/Documents/workspace/monks/MyMoo/MyMojo/myapp.p +sgi); my $hypnotoad = Mojo::Server::Hypnotoad->new; $hypnotoad->run($app) __END__
For what this stuff is (or should be) good for please see ibidem. I didn't want to quote it because it is a bit extensive. Sorry for the inconvenience.
Basically it does why i want. I tried to simplify it as much i could (skipping the database part a.s.o.).
But this approach seems to be so simple that i'm in doubt if i miss something.
Thank you very much for any hint.
Update: The database part that i skipped for simplification:
package Connector; use DBIx::Connector; use Types::Standard qw(InstanceOf); use Moo::Role; has 'connector' => ( is => 'ro', builder => '_build_connector', handles => [qw(dbh txn)], isa => InstanceOf( ['DBIx::Connector'] ), ); sub _build_connector { my $dsn = qq(dbi:SQLite:dbname=db/myDB.sqlite); my $conn = DBIx::Connector->new( $dsn, { RaiseError => 1, AutoCommit => 1, } ); $conn->mode('fixup'); $conn; } 1; __END__
package MyApp; use Moo; with qw(Connector); 1; __END__
use Mojolicious::Lite; use MyApp; my $moo = MyApp->new(); get '/foo' => sub { my $c = shift; my $query = qq(select nose from myTable); my $dbh = $moo->dbh; my $sth = $moo->txn( sub { my $sth = $dbh->prepare($query); $sth->execute; $sth; } ); my $row = $sth->fetch; $c->render( text => "<h1>$row->[0]</h1>" ); }; get '/bar' => sub { my $c = shift; my $query = qq(select monk from myTable); my $dbh = $moo->dbh; my $sth = $moo->txn( sub { my $sth = $dbh->prepare($query); $sth->execute; $sth; } ); my $row = $sth->fetch; $c->render( text => "<h1>$row->[0]</h1>" ); }; app->start; __END__
I wasn't aware of this prefork issue but it seems like my example works as designed with hypnotoad ;-)
Edit: Added comment about $self.
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: Mojolicious::Lite and Moo: A Very Basic Application Layout
by jeffa (Bishop) on Aug 05, 2015 at 21:19 UTC | |
by karlgoethebier (Abbot) on Aug 05, 2015 at 21:52 UTC | |
|
Re: RFC: Mojolicious::Lite and Moo: A Very Basic Application Layout
by Anonymous Monk on Aug 05, 2015 at 21:11 UTC | |
by karlgoethebier (Abbot) on Aug 05, 2015 at 21:35 UTC | |
by Anonymous Monk on Aug 05, 2015 at 22:55 UTC | |
by karlgoethebier (Abbot) on Aug 05, 2015 at 23:49 UTC | |
by Anonymous Monk on Aug 06, 2015 at 00:29 UTC |