Re: [Mojolicious] Minion: getting data out.

2019-10-22 Thread Justin Hawkins


> On 22 Oct 2019, at 12:31 am, BobbyBrown  wrote:
> 
> I still have not figured out how to reliably get data out of a minion task, 
> I'll have to work on events and read up on non-blocking.
> If I understand the code correctly the linkcheck example simply fails if the 
> minion job has not finished when it looks up the result?

The minion job is completely independent of your web request - which is the 
entire point.

If you are doing something which you expect to be completed in the time of the 
request, then you may as well just execute it immediately and block waiting for 
it (or look at Mojo::IOLoop::Subprocess).

Otherwise, you would check back on the minion job later, perhaps via a REST 
request or similar.

A typical flow:

* request causes minion job to be enqueued
* job id is returned to the user's browser
* user's browser starts polling a REST endpoint looking for job completion (via 
javascript)
* once the job is complete, the browser redirects to a new URL with the job data

This is far from the only way to do it, and javascript is not a requirement.

Cheers,

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/FC37D777-C462-49D8-B64E-F6A4504674B3%40hawkins.id.au.


Re: [Mojolicious] Difference between %= and %== on undefined data

2019-09-11 Thread Justin Hawkins


> On 9 Sep 2019, at 8:43 pm, Lars Madsen  wrote:
> 
> So my question is: Is %= and %== suppose to have different handling of undef 
> data?
> 

Hi Lars,

Not specifically different, but %= will escape the data and %== passes it 
straight through. Presumably the escaping has handling for undef data which 
prevents “use of uninitialised” warnings. 

In general, making sure "uninitialised value" warnings don’t get emitted is 
your problem, not the frameworks. An undef being printed or otherwise emitted 
to the end user is generally a code smell.

Cheers,

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/81A738E2-9214-4A64-A94D-9BF7FAA78CDE%40hawkins.id.au.


Re: [Mojolicious] 'Under' controller method called multiple times

2019-06-19 Thread Justin Hawkins


> On 15 Jun 2019, at 01:02, Dan Book  wrote:
> 
> To just "establish a URL path", under is not needed, you can just use any 
> which does not create a discrete action.

Thanks Dan, 

This worked perfectly.

The double call to the previous “under” is still a mystery but no longer a 
problem. 

Cheers,

  Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/4661E8A0-435D-498D-A11B-FD3FE66E245C%40hawkins.id.au.


[Mojolicious] 'Under' controller method called multiple times

2019-06-12 Thread Justin Hawkins
Hello all,

I think there is a good chance this is a case of “holding it wrong”, so I will 
first explain what I’m trying to achieve.

Standard web app, multiple ways of establishing rights to privileged routes.

The first is standard username/password, establishing a session.

The second is via an API key (supplied with a header).

I use two separate controller methods, one to check the session, one to check 
the header. Each one has the opportunity to set a stash variable indicating 
that the user has a valid session (via either cookie session or API key).

So I use two “under” routes to call each method. So far so good, things work ok.

The problem I have is that I later have another under route, to establish a new 
URL path for my REST interface.

Any requests that go through routes derived from that third “under" have a 
strange behaviour - they call the second ‘under’ controller twice!

This is probably easiest to see with an example:

package TestUnder;
use Mojo::Base 'Mojolicious';

# This method will run once at server start
sub startup {
  my $self = shift;

  # Load configuration from hash returned by config file
  my $config = $self->plugin('Config');

  # Configure the application
  $self->secrets($config->{secrets});

  # Router
  my $r = $self->routes;

  # All requests should go through these two
  $r = $r->under()->to('example#under_one');
  $r = $r->under()->to('example#under_two');
  $r->get('/')->to('example#welcome');

  # REST interface
  my $rest_v1 = $r->under('/rest/v1');
  $rest_v1->get('/')->to('example#welcome');

}

package TestUnder::Controller::Example;

use Mojo::Base 'Mojolicious::Controller';

sub under_one {
  my $self = shift;
  $self->app->log->info("under_one called");
  1;
}

sub under_two {
  my $self = shift;
  $self->app->log->info("under_two called");
  1;
}

sub welcome {
  my $self = shift;
  $self->render(text => 'hi');
}

1;

Example below - you can easily see that for the “REST” route, ‘under_two’ is 
called twice:



   
$ script/test_under get / >/dev/null
[2019-06-13 15:28:30.55547] [44306] [debug] GET "/" (9ca39c7c)
[2019-06-13 15:28:30.55627] [44306] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "under_one"
[2019-06-13 15:28:30.55637] [44306] [info] under_one called
[2019-06-13 15:28:30.55651] [44306] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "under_two"
[2019-06-13 15:28:30.55661] [44306] [info] under_two called
[2019-06-13 15:28:30.55675] [44306] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "welcome"
[2019-06-13 15:28:30.55704] [44306] [debug] 200 OK (0.001571s, 636.537/s)

$ script/test_under get /rest/v1 >/dev/null
[2019-06-13 15:28:34.95883] [44315] [debug] GET "/rest/v1" (6aad1c75)
[2019-06-13 15:28:34.95975] [44315] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "under_one"
[2019-06-13 15:28:34.95984] [44315] [info] under_one called
[2019-06-13 15:28:34.95997] [44315] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "under_two"
[2019-06-13 15:28:34.96005] [44315] [info] under_two called
[2019-06-13 15:28:34.96020] [44315] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "under_two"
[2019-06-13 15:28:34.96031] [44315] [info] under_two called
[2019-06-13 15:28:34.96044] [44315] [debug] Routing to controller 
"TestUnder::Controller::Example" and action "welcome"
[2019-06-13 15:28:34.96083] [44315] [debug] 200 OK (0.001985s, 503.778/s)

$ mojo version
CORE
  Perl(v5.26.1, darwin)
  Mojolicious (8.17, Supervillain)

OPTIONAL
  Cpanel::JSON::XS 4.04+  (n/a)
  EV 4.0+ (n/a)
  IO::Socket::Socks 0.64+ (n/a)
  IO::Socket::SSL 2.009+  (2.051)
  Net::DNS::Native 0.15+  (n/a)
  Role::Tiny 2.01+(2.05)

This version is up to date, have fun!

Regards,

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/8EB007C9-0631-448B-B654-56C6D1EE0A5A%40hawkins.id.au.


Re: [Mojolicious] Mojo::Test to follow redirect?

2017-05-30 Thread Justin Hawkins

> On 29 May 2017, at 8:55 am, iaw4  wrote:
> 
> thanks, heiko.  my site relies heavily on proper handling many host 
> subdomains.for example, my auth subdomain handles OAuth intereactions 
> with google, which are subdomain specific.  and then each of my users has 
> their own vanity subdomain, but they nicely share session information.  once 
> logged into one subdomain, they can go to another.  so, for my case, the 
> usual testing just within sites (get_ok('/')) is insufficient.  I really do 
> need to work with full http:// and URLs to obtain the subdomain and make 
> different decisions based on it.  I guess my app is unusual :-(.

To do this, take the decision logic (which parses the hostname), and make it 
testable. For instance, make it check an environment variable first:

my $domain;
if ($ENV{OVERRIDE_DOMAIN}) {
  $domain = $ENV{OVERRIDE_DOMAIN};
}
else {
  $domain = $self->req.., # normal logic
}

Then you can go back to using the built in testing idioms, and simply set the 
environment variable where appropriate.

Cheers,

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] Hypnotoad and gone SQL

2017-05-04 Thread Justin Hawkins

> On 4 May 2017, at 5:30 pm, Alexander Lunev  wrote:
> 
> DBD::Pg::db selectall_hashref failed: server closed the connection 
> unexpectedly. 

You tried to use a database handle that had been initialised in a previous 
process and then forked. As you’ve realised, this doesn’t work :-)

> 
> And if I try to use recipe with "has => dbh", somehow i need to get access to 
> $self->app in lib/PortMgr/Storage/PgSQL.pm, but it is not a Mojolicious 
> controller class, so i need to drag $dbh from lib/PortMgr.pm to 
> lib/PortMgr/Controller/Hw.pm to lib/PortMgr/Model/Hw.pm to 
> lib/PortMgr/Storage/PgSQL.pm? 
> 
> Is there any other solution? 


In general, if you find it difficult to test any of your classes in isolation, 
you’ve got some sort of code smell. In this case, the fact that the model 
instantiates it’s own database handle makes it hard to test, and causes this 
issue.

I’d suggest instantiating your database connection in the main application, and 
then passing it as a parameter to your model class. No singletons. Singletons 
aren’t always bad, but this one is :-) The model class should store it as an 
attribute:

package PortMgr::Model::Hw;

use Mojo::Base qw/-base/;

has ‘storage’;

…

As an added benefit, now you can test more easily:

my $db = Test::PortMgr::Storage::PgSQL->new(); # db handle to test database
my $model = PortMgr::Model::Hw->new(storage = >$db);

ok($model->get_hw(), ‘can get hw’);

Cheers,

Justin


-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


[Mojolicious] Including templates with common "extends" base

2016-06-23 Thread Justin Hawkins
Hi All,

I'm using the template ‘extends’ feature to build some forms with common 
elements. I’m trying to include multiple forms on a single page, thus including 
several templates that inherit from a common base.

Hopefully this code example demonstrates what I mean: 
https://gist.github.com/anonymous/2ddc0fa21ed69470401bf978bf575998

The problem is that the “second.html.ep” template seems to retrieve the cached 
content of the ‘base.html.ep’ template after it was modified from 
‘first.html.ep’, rather than re-evaluating it.

Am I “holding it wrong” or is this a bug? Is there any way around it?

Thanks!

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] Getting error page in a test

2015-09-01 Thread Justin Hawkins

> On 2 Sep 2015, at 9:34 am, Neil Watson  wrote:
> 
> 
> If I have something like this in a *.t test file.
> 
> lives_and {
>  $t->get_ok( '/initialize_database' )
>  ->status_is( 200, 'Initialize database' );
> } '/initialze_database';
> 
> If the test fails how I get the test to output details of the failure?
> All I see now is that 500 was returned, but not the page.
> 

Hey Neil,

There’s a ‘tx’ method on the test object, so:

   say $t->tx->res->body

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] is_file

2015-05-26 Thread Justin Hawkins

> On 27 May 2015, at 8:47 am, Andrew  wrote:
> 
> So I'm left scratching my head - what does is_file actually do, if it
> doesn't check if a file exists?
> 

A Mojo::Asset::File is always a file, as the name implies, thus it always 
returns true.

If you have a Mojo::Asset, you can use is_file to find out if it is a file (as 
opposed to it being perhaps a Mojo::Asset::Memory object).

if (! $asset->is_file) {
  $asset->move_to(‘/some/other/file.txt’);
}

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] Get at name of current template?

2015-05-13 Thread Justin Hawkins

> On 14 May 2015, at 2:48 am, Juergen Nickelsen  
> wrote:
> 
> for a while I have been trying to get at the name of the template that
> is just being rendered, to use it in the layout.

I can’t answer your question directly, but this requirement sounds like a bit 
of a code smell. What are you trying to accomplish?

You should probably be setting something in the stash to drive logic required 
for presentation in the layout, not depending on what template is being 
rendered.

For example (untested):

sub some_controller_method {
  my $self = shift;
  $self->stash( has_login_box => 1 );
  $self->render(template => ‘foo’);
}

then in the layout:

% if (stash(‘has_login_box’)) {

...

% }

or better:

% if (stash(‘has_login_box’)) {
%   include ‘page_bits/login’;
% }

If you literally just want the name of the template, well, I don’t know how to 
do that, and I don’t know why you’d want to do that :-)

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] Swagger2: "The World's Most Popular Framework for APIs"

2014-12-10 Thread Justin Hawkins

> On 10 Dec 2014, at 5:26 pm, Jan Henning Thorsen  
> wrote:
> 
> Looks interesting Justin!
> 
> How would you feel if i changed your code into a Swagger2 command and merged 
> it into my project? You could also be co-auth, if you like…

If you can find something of use in my code, please do so :-)

> I've been thinking about a swagger (or swagger2 command), which could convert 
> the API description into POD. Something like this:
> 
>   $ mojo swagger pod path/to/spec.json
> 
> I guess if I could merge your module, there would also be a --version switch:
> 
>   $ mojo swagger json --version 1.2 lib/YourApp.pm

I wasn’t wedded to supporting 1.2 at the time - it was only that 2.0 wasn’t 
mature. I’d probably target that now.

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


smime.p7s
Description: S/MIME cryptographic signature


Re: [Mojolicious] Swagger2: "The World's Most Popular Framework for APIs"

2014-12-09 Thread Justin Hawkins

On 10 Dec 2014, at 1:32 am, Jan Henning Thorsen  wrote:

> I'm working on a new module that takes advantage of the Swagger2 API 
> documentation format.
> 
> https://metacpan.org/release/Swagger2
> https://metacpan.org/release/JHTHORSEN/Swagger2-0.03 (in case 0.03 isn't 
> visible from the line above)
> 
> Features:
> * Automatic route generation
> * Input/output validation in your Mojoliicous applicaiton
> * Swagger documentation to Perl documentation (POD)
> * JSON Schema validation
> 
> Plans:
> * Add Swagger::Client which wraps around Mojo::UserAgent and enables 
> input/output validation
> 
> Any feedback/ideas are very much appreciated.
> 
> 

Hi Jan,

Interesting idea!

I hacked for a while on essentially the reverse - generate the swagger JSON 
data from the Mojolicious routes + POD documentation. Thus the actual code and 
inline documentation becomes the source of truth for your API specification.

Maybe I need to pick it up again :-)

https://github.com/tardisx/mojolicious-command-swagger

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] What databases are you using with Mojolicious? (Poll)

2014-09-26 Thread Justin Hawkins

On 27 Sep 2014, at 2:25 am, sri  wrote:

> Wow, this thread gives a great overview of what the community currently looks 
> like, personally i'm (positively) surprised how strong SQL databases are 
> represented. So here's a bonus question, how do you manage schema upgrades?

Sqitch.

I’d urge everyone still doing schema upgrades by hand to try it (or something 
similar).

- Justin


signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [Mojolicious] Cutesy dates (going full stack or not)

2014-08-27 Thread Justin Hawkins

On 28 Aug 2014, at 1:25 pm, sri  wrote:

> A few words about the motivation. Like many Perl hackers, since the closure 
> of Google Groups i'm a regular visitor of the Planet Perl Iron Man site 
> (http://ironman.enlightenedperl.org/), which is a very good example for 
> something quickly hacked together and never cleaned up. You don't even have 
> to look at the code, which still sports the stub documentation generated by 
> Catalyst. One of the first thing that jumped into my eye were those horrible 
> timestamps, even for a quick hack you should never have to choose those. 
> Mojolicious needs to be better than that, making aesthetically pleasing 
> applications has to be easy by default.

I share this pet peeve. I've been using Time::Duration since ... forever.

Mojo::Date has a nicer interface, but I still don't know how I feel about it 
being in core.

I can see the arguments for - date and time handling comes up in almost any 
non-trivial web app, and you might as well make it look good and make sense.

Overall I think I'm in favour. Not sure about the 'cutesy dates' name :-)

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] multiple authentication methods?

2014-08-23 Thread Justin Hawkins

On 23 Aug 2014, at 3:44 am, Richard Sugg  wrote:

> Anyone have any patterns they have followed in this situation? I realize that 
> some may have objections to my implementation, but I am developing an 
> internally-used corporate website, so I don't have leeway in the matter. I 
> know how I *could* do this, but would be happy to hear others' thoughts on 
> implementation first.

If you want automated tools to have access, providing a per-user API key (which 
can be generated/changed/revoked when appropriate) is a common way of providing 
access to non-interactive “users”.

- Justin


signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [Mojolicious] getting arguments with mojolicious::command

2014-05-16 Thread Justin Hawkins

On 16/05/2014, at 11:12 AM, Neil Watson  wrote:

> Greetings,
> 
> When using mojo::command how do you parse arguments? I tried
> getopt::long but nothing came through.
> 
> package DeltaR::Command::query;
> use Mojo::Base 'Mojolicious::Command';
> use Getopt::Long;
> use Data::Dumper;
> 
> sub run
> {
>  my $self = shift;
>  my %query_params;

use Getopt::Long qw/GetOptionsFromArray/;

sub run {

  my ($self, @args) = @_;
  my $verbose = 0;

  GetOptionsFromArray(\@args, "verbose" => \$verbose );

  ...
}

- Justin

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] How to update mojolicious?

2014-03-20 Thread Justin Hawkins

On 18 Mar 2014, at 11:39 pm, Renato Forti  wrote:

> Hi All, 
> 
> I want update my mojo! I am using 2.98 on ubuntu, see: 
> 
> CORE
>   Perl(v5.14.2, linux)
>   Mojolicious (2.98, Leaf Fluttering In Wind)
> 
> OPTIONAL
>   EV   (not installed)
>   IO::Socket::INET6(2.69)
>   IO::Socket::SSL  (1.76)
> 
> You might want to update your Mojolicious to 4.90.
> 
> How I can update?, I did tried:  curl get.mojolicio.us | sh, but dont work!
> 
> Thanks a lot!

If you want to stick with packages, I've created Mojolicious .deb files with 
cpan2deb with no problems at all.

- Justin

-- 
Justin Hawkins
jus...@hawkins.id.au





-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.