diff --git a/lib/Mojolicious/Plugin/Yancy.pm b/lib/Mojolicious/Plugin/Yancy.pm index ccac6da6..8b8fc939 100644 --- a/lib/Mojolicious/Plugin/Yancy.pm +++ b/lib/Mojolicious/Plugin/Yancy.pm @@ -54,12 +54,27 @@ connections. helper pg => sub { state $pg = Mojo::Pg->new( 'postgres:///myapp' ) }; plugin Yancy => { backend => { Pg => app->pg } }; +=item editor + +Configuration for the Yancy web application. A hash, permitting these +keys: + +=over + =item route A base route to add Yancy to. This allows you to customize the URL -and add authentication or authorization. Defaults to allowing access -to the Yancy web application under C, and the REST API under -C. +and add authentication or authorization. If a string is supplied, it +will be turned into a L object with that +path, under the app. Defaults to C. + +=item api_route + +A route as above, for the editor's REST API, living under the editor's +route. A string may also be supplied, defaulting to C. This means +the default REST API path will be C. + +=back =item return_to @@ -103,6 +118,19 @@ authentication or authorization checks: } ); $auth_route->add_child( $_ ) for @need_auth; +=head2 yancy.api_route + +Get the route where the Yancy API will appear. Useful for adding +authentication or authorization checks: + + my $api_route = $c->yancy->api_route; + my @need_auth = @{ $api_route->children }; + my $auth_route = $api_route->under( sub { + # ... Check auth + return 1; + } ); + $auth_route->add_child( $_ ) for @need_auth; + =head2 yancy.plugin Add a Yancy plugin. Yancy plugins are Mojolicious plugins that require @@ -410,8 +438,13 @@ has _filters => sub { {} }; sub register { my ( $self, $app, $config ) = @_; - my $route = $config->{route} // $app->routes->any( '/yancy' ); - $route->to( return_to => $config->{return_to} // '/' ); + my $editor_route = $config->{editor}{route} // '/yancy'; + $editor_route = $app->routes->any( $editor_route ) + if !UNIVERSAL::isa( $editor_route, 'Mojolicious::Routes::Route' ); + $editor_route->to( return_to => $config->{return_to} // '/' ); + my $api_route = $config->{editor}{api_route} // '/api'; + $api_route = $editor_route->any( $api_route )->name( 'yancy.api' ) + if !UNIVERSAL::isa( $api_route, 'Mojolicious::Routes::Route' ); $config->{api_controller} //= 'Yancy::API'; $config->{openapi} = _ensure_json_data( $app, $config->{openapi} ); @@ -423,7 +456,7 @@ sub register { # Helpers $app->helper( 'yancy.config' => sub { return $config } ); - $app->helper( 'yancy.route' => sub { return $route } ); + $app->helper( 'yancy.route' => sub { return $editor_route } ); $app->helper( 'yancy.backend' => sub { state $backend = load_backend( $config->{backend}, $config->{collections} || $config->{openapi}{definitions} ); } ); @@ -444,7 +477,7 @@ sub register { $app->helper( 'yancy.filter.apply' => curry( \&_helper_filter_apply, $self ) ); # Routes - $route->get( '/' )->name( 'yancy.index' ) + $editor_route->get( '/' )->name( 'yancy.index' ) ->to( template => 'yancy/index', controller => $config->{api_controller}, @@ -497,10 +530,11 @@ sub register { $self->_openapi_spec_add_mojo( $spec, $config ); my $openapi = $app->plugin( OpenAPI => { - route => $route->any( '/api' )->name( 'yancy.api' ), + route => $api_route, spec => $spec, default_response_name => '_Error', } ); + $app->helper( 'yancy.api_route' => sub { $api_route } ); $app->helper( 'yancy.openapi' => sub { $openapi } ); # Add supported formats to silence warnings from JSON::Validator @@ -742,7 +776,7 @@ sub _openapi_spec_from_schema { info => $config->{info} || { title => 'Yancy', version => 1 }, swagger => '2.0', host => $config->{host} // hostname(), - basePath => '/api', + basePath => $config->{editor}{api_route} // '/api', schemes => [qw( http )], consumes => [qw( application/json )], produces => [qw( application/json )], diff --git a/lib/Yancy.pm b/lib/Yancy.pm index cd1966da..56eb4096 100644 --- a/lib/Yancy.pm +++ b/lib/Yancy.pm @@ -190,7 +190,7 @@ __DATA__

This is the default not found page.

Getting Started

-

To edit your data, go to /yancy.

+

To edit your data, go to <%== url_for 'yancy.index' %>.

Add your templates to <%= app->home->child( 'templates' ) %>. Each template becomes a URL in your site: