NAME

Whelk::Manual::Kelp - Setting up Whelk inside Kelp

SYNOPSIS

# File: conf/config.pl
########################
{
	modules => [qw(JSON YAML Whelk)],
	modules_init => {
		Routes => {
			base => 'MyApp/Controller',
			rebless => 1,
		},
		Whelk => {
			resources => {
				Res => '/',
			},
			openapi => '/openapi.json',
		},
	},
}

# File: lib/MyApp.pm
######################
package MyApp;

use Kelp::Base 'Kelp';

sub build {
	my ($self) = @_;

	$self->whelk->init;
}

# File: lib/MyApp/Controller.pm
#################################
package MyApp::Controller;

use Kelp::Base 'MyApp';

# File: lib/MyApp/Controller/Res.pm
#####################################
package MyApp::Controller::Res;

use Kelp::Base 'MyApp::Controller';
use Role::Tiny::With;

with 'Whelk::Role::Resource';

sub api
{
	# implement the API as usual
	...
}

DESCRIPTION

Whelk is easily set up to work as a regular Kelp module. This document explains the differences between standalone Whelk and nested Whelk. Most information from Whelk::Manual is still valid, but there are a couple of differences.

Configuration: Whelk module and reblessing router

Kelp::Module::Whelk is a module which must be loaded in app's configuration. It can be loaded at any place, since it does not fully init itself during initial module load.

Whelk works exclusively in the controller mode, so Router module must have correct setting of base and rebless. Whelk is not guaranteed to work with custom Kelp routers.

Unlike standalone Whelk, all configuration listed in "Configuration" in Whelk::Manual is not taken from root hash but instead from modules_init.Whelk hash.

Calling finalize inside application's build

To correctly set up Whelk, you have to manually call $app->whelk->finalize(); in your application's build method. You can use it to your advantage by defining some global schemas before calling finalize, so that all resources will have access to them. After calling finalize the api is finalized and can no longer be modified.

If you forget to call finalize, the api will not be built and installed in your app.

Consuming Whelk::Role::Resource role

Each controller which is supposed to be a Whelk resource must consume Whelk::Role::Resource (Role::Tiny role) instead of extending Whelk::Resource. It can be done with with from Role::Tiny::With or Moo.

Don't consume this role in your base controller, unless you plan to have all controllers as Whelk resources.

SEE ALSO

Whelk::Manual

Kelp::Module::Whelk