Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route to static file in Mojo

I have small app based on mojolicious. And I have index.html in public dir. I want to have route to this file when user asks for '/'.

I wrote two solution, but I don't like them.

First solution - add simple controller.

sub stratup {
  //...
  $r->get('/')->to('general#index_html');
  //...
}

package MyPackage::General;

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

use strict;
use warnings;

sub index_html {
    my $self = shift;
    $self->render_static('index.html');
    return;
}

1;

Second solution - add hook

sub startup {
    my $self = shift;

    $self->hook(before_dispatch => sub {
            my $self = shift;
            if ($self->req->url eq '/') {
                $self->req->url( Mojo::URL->new('/index.html') );
            }
        });

What I want:

$r->get('/')->to('/index.html');

or something like that.

P.S. I know, than usualy nginx/apache do it, but I use morbo to run code.

like image 861
KoVadim Avatar asked Mar 05 '15 14:03

KoVadim


1 Answers

You want:

$r->get('...')->to(cb => sub {  
     my $c = shift;                                   
     $c->reply->static('index.html')                
}); 

(As long as you're after Mojolicous 5.45 2014-09-26)

like image 193
Nyanstep Avatar answered Nov 13 '22 16:11

Nyanstep