Ado

 view release on metacpan or  search on metacpan

lib/Ado.pm  view on Meta::CPAN

    Carp::croak("$CLASS installation directory not found!");
};

sub CODENAME { return $CODENAME }
has sessions => sub { Ado::Sessions::get_instance(shift->config) };

has home => sub {
    return Mojo::Home->new->detect();                 #fallback to Mojo::Home
};


sub _initialise {
    my ($app)    = @_;
    my $home     = $app->home;
    my $mode     = $app->mode;
    my $ado_home = $app->ado_home;

    # add paths to bundled files if needed.
    my $templates_dir  = $ado_home->rel_file('templates');
    my $site_templates = $home->rel_file('site_templates');
    my $renderer_paths = $app->renderer->paths;
    my $public_dir     = $ado_home->rel_file('public');
    my $static_paths   = $app->static->paths;
    push @$renderer_paths, $templates_dir
      unless (first { $_ eq $templates_dir } @$renderer_paths);
    push @$static_paths, $public_dir
      unless (first { $_ eq $public_dir } @$static_paths);

    $app->secrets([Mojo::Util::sha1_sum($app->moniker . $mode . $home),]);
    unshift @$renderer_paths, $site_templates if -d $site_templates;
    $app->controller_class("${CLASS}::Control");
    $app->routes->namespaces(["${CLASS}::Control"]);
    $app->plugins->namespaces(['Mojolicious::Plugin', "${CLASS}::Plugin",]);
    unshift @{$app->commands->namespaces}, "${CLASS}::Command";
    return $app;
}

# This method will run once at server start
sub startup {
    shift->_initialise()->load_config()->load_plugins()->load_routes()->define_mime_types();
    return;
}

#load ado.conf
sub load_config {
    my ($app) = @_;
    my $app_config = $app->home->rel_file('etc/' . $app->moniker . '.conf');
    $ENV{MOJO_CONFIG} //=
      -s $app_config
      ? $app_config
      : $app->ado_home->rel_file('etc/' . lc($CLASS) . '.conf');
    $app->plugin('Config');
    return $app;
}

sub load_plugins {
    my ($app) = @_;

    my $plugins = $app->config('plugins') || [];
    foreach my $plugin (@$plugins) {
        $app->log->debug('Loading Plugin ' . (ref $plugin ? $plugin->{name} : $plugin));
        if (ref $plugin eq 'HASH') {
            $app->plugin($plugin->{name} => $plugin->{config});
        }
        elsif ($plugin) {
            $app->plugin($plugin);
        }
    }

    return $app;
}

#load routes defined in ado.conf
sub load_routes {
    my ($app, $config_routes) = @_;
    $config_routes ||= $app->config('routes') || [];
    my $routes = $app->routes;

    # Hide Ado::Control methods and attributes from router.
    $routes->hide(
        qw(
          debug config require_format list_for_json
          validate_input
          )
    );

    foreach my $route (@$config_routes) {
        my ($pattern, $over, $to, $via, $params) =
          ($route->{route}, $route->{over}, $route->{to}, $route->{via}, $route->{params});

        next unless $to;
        my $r = $params ? $routes->route($pattern, %$params) : $routes->route($pattern);

        if ($over) {
            if   (ref $over eq 'HASH') { $r->over(%$over); }
            else                       { $r->over($over); }
        }
        if ($via) {
            $r->via(@$via);
        }
        $r->to(ref $to eq 'HASH' ? %$to : $to);
    }

    # Default "/perldoc" page is Ado/Manual
    my $perldoc = $routes->lookup('perldocmodule');
    if ($perldoc) { $perldoc->to->{module} = 'Ado/Manual'; }

    return $app;
}

sub define_mime_types {
    my ($app) = @_;
    my $mimes = $app->config('types') || {};    #HASHREF
    foreach my $mime (keys %$mimes) {

        # Add new MIME type or redefine any existing
        $app->types->type($mime => $mimes->{$mime});
    }
    return $app;
}



( run in 1.804 second using v1.01-cache-2.11-cpan-f56aa216473 )