| Lemonldap::NG::Common::PSGI::Router(3pm) | User Contributed Perl Documentation | Lemonldap::NG::Common::PSGI::Router(3pm) |
Lemonldap::NG::Common::PSGI::Router - Base library for REST APIs of Lemonldap::NG.
package My::PSGI;
use base Lemonldap::NG::Common::PSGI::Router;
sub init {
my ($self,$args) = @_;
# Will be called 1 time during startup
# Declare REST routes (could be HTML templates or methods)
$self->addRoute ( 'index.html', undef, ['GET'] )
->addRoute ( books => { ':book' => 'booksMethod' }, ['GET', 'POST'] )
->addRoute ( properties => { '*' => 'propertiesMethod' }, ['GET', 'POST', 'PUT', 'DELETE']);
# Default route (ie: PATH_INFO == '/')
$self->defaultRoute('index.html');
# See Lemonldap::NG::Common::PSGI for other options
# Return a boolean. If false, then error message has to be stored in
# $self->error
return 1;
}
sub booksMethod {
my ( $self, $req, @otherPathInfo ) = @_;
my $book = $req->params('book');
my $method = $req->method;
...
$self->sendJSONresponse(...);
}
sub propertiesMethod {
my ( $self, $property, @otherPathInfo ) = @_;
my $method = $req->method;
...
$self->sendJSONresponse(...);
}
This package could then be called as a CGI, using FastCGI,...
#!/usr/bin/env perl use My::PSGI; use Plack::Handler::FCGI; # or Plack::Handler::CGI Plack::Handler::FCGI->new->run( My::PSGI->run() );
This package provides base class for Lemonldap::NG REST API but could be used regardless.
See Lemonldap::NG::Common::PSGI for logging methods, content sending,...
addRoute ( $word, $dest, $methods )
Declare a REST route. Arguments:
Route types
As seen in "SYNOPSIS", you can declare routes with variable component. $dest can be:
Some special $word:
Examples:
$self->addRoute( books => { ':bookId' => 'book' }, ['GET'] );
bookId parameter will be stored in $req->params('bookId');
$self->addRoute( books => { ':bookId' => { pages => { ':pageId' => 'page' } } }, ['GET'] );
$self->addRoute( books => { ':bookId' => { pages => { ':pageId' => 'page' } } }, ['GET'] )
->addRoute( books => { ':bookId' => { '*' => 'book' } }, ['GET'] );
Note that book() will be called for any path_info containing /books/<$bookid>/<$other> except if $other == 'pages'.
$self->addRoute( properties => [ 'p1', 'p2' ] );
defaultRoute($path)
This method defined which path_info to use if path_info is '/' or empty.
See Lemonldap::NG::Common::PSGI for inherited accessors (error, languages, logLevel, staticPrefix, templateDir, links, syslog).
<http://lemonldap-ng.org/>, Lemonldap::NG::Portal, Lemonldap::NG::Handler, Plack, PSGI, Lemonldap::NG::Common::PSGI, Lemonldap::NG::Common::PSGI::Request, HTML::Template,
Use OW2 system to report bug or ask for features: <https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
Lemonldap::NG is available at <https://lemonldap-ng.org/download>
See COPYING file for details.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
| 2023-09-29 | perl v5.36.0 |