Setting base URL to work either via Apache/CGI or morbo/hypnotoad?

185 views
Skip to first unread message

VirtualWolf

unread,
Jul 28, 2012, 8:34:10 AM7/28/12
to mojol...@googlegroups.com
Hi all,

Currently I have this set to configure the base URL (I have my code running on a couple of different servers):

    $self->hook(
        before_dispatch => sub {
            my $self        = shift;

            my $base_url    = "http://$ENV{SERVER_NAME}/";
            $self->req->url->base( Mojo::URL->new($base_url) );
        }
    );

And it works beautifully. However, I realised that it doesn't work on anything but Apache. I'm starting to muck around with morbo and get a bunch of these errors:

    Use of uninitialized value $ENV{"SERVER_NAME"} in concatenation (.) or string at script/../lib/Site.pm line 38.

I use the server name as in the title of each page, too. Is there a "native Mojolicious" way to do this that will work across Apache/CGI and Morbo etc?

Thanks!

Tekki

unread,
Jul 29, 2012, 3:07:26 PM7/29/12
to mojol...@googlegroups.com
A "native Perl" way:

  use Net::Domain;

 
  ...

  my $hostname = Net::Domain::hostname;
  my $base_url = "http://$hostname/";

Regards
Rolf

VirtualWolf

unread,
Jul 29, 2012, 9:23:44 PM7/29/12
to mojol...@googlegroups.com
...I have no idea why that didn't occur to me. Thanks very much. :D
Reply all
Reply to author
Forward
0 new messages