.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Lemonldap::NG::Common::PSGI 3pm" .TH Lemonldap::NG::Common::PSGI 3pm 2024-02-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Lemonldap::NG::Common::PSGI \- Base library for PSGI modules of Lemonldap::NG. Use Lemonldap::NG::Common::PSGI::Router for REST API. .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& package My::PSGI; \& \& use base Lemonldap::NG::Common::PSGI; \& \& sub init { \& my ($self,$args) = @_; \& # Will be called 1 time during startup \& \& # Store debug level \& $self\->logLevel(\*(Aqinfo\*(Aq); \& # It is possible to use syslog for user actions \& $self\->syslog(\*(Aqdaemon\*(Aq); \& \& # Return a boolean. If false, then error message has to be stored in \& # $self\->error \& return 1; \& } \& \& sub handler { \& my ( $self, $req ) = @_; \& # Do something and return a PSGI response \& # NB: $req is a Lemonldap::NG::Common::PSGI::Request object \& \& return [ 200, [ \*(AqContent\-Type\*(Aq => \*(Aqtext/plain\*(Aq ], [ \*(AqBody lines\*(Aq ] ]; \& } .Ve .PP This package could then be called as a CGI, using FastCGI,... .PP .Vb 1 \& #!/usr/bin/env perl \& \& use My::PSGI; \& use Plack::Handler::FCGI; # or Plack::Handler::CGI \& \& Plack::Handler::FCGI\->new\->run( My::PSGI\->run() ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This package provides base class for Lemonldap::NG web interfaces but could be used regardless. .SH METHODS .IX Header "METHODS" .SS "Running methods" .IX Subsection "Running methods" \fIrun ( \fR\f(CI$args\fR\fI )\fR .IX Subsection "run ( $args )" .PP Main method that will manage requests. It can be called using class or already created object: .IP Class\->run($args): 4 .IX Item "Class->run($args):" launch new($args), \fBinit()\fR, then manage requests (using private \fB_run()\fR method .ie n .IP $object\->\fBrun()\fR: 4 .el .IP \f(CW$object\fR\->\fBrun()\fR: 4 .IX Item "$object->run():" manage directly requests. Initialization must have be done earlier. .SS Logging .IX Subsection "Logging" \fIlmLog ( \fR\f(CI$msg\fR\fI, \fR\f(CI$level\fR\fI)\fR .IX Subsection "lmLog ( $msg, $level)" .PP Print on STDERR messages if level > \f(CW$self\fR\->{logLevel}. Defined log levels are: debug, info, notice, warn, error. .PP \fIuserLog ($msg, \fR\f(CI$level\fR\fI)\fR .IX Subsection "userLog ($msg, $level)" .PP Alias for \f(CW$self\fR\->userLogger\->$level($msg). Prefer to use this form (required for Auth/Combination) .PP \fR\f(BIuserError()\fR\fI \fR\f(BIuserNotice()\fR\fI \fR\f(BIuserInfo()\fR\fI\fR .IX Subsection "userError() userNotice() userInfo()" .PP Alias for userLog(level). Note that you must use \f(CW$self\fR\->userLogger\->$level instead .SS "Content sending" .IX Subsection "Content sending" Note that \f(CW$req\fR, the first argument of these functions, is a Lemonldap::NG::Common::PSGI::Request. See the corresponding documentation. .PP \fIsendHtml ( \fR\f(CI$req\fR\fI, \fR\f(CI$template\fR\fI )\fR .IX Subsection "sendHtml ( $req, $template )" .PP This method build HTML response using HTML::Template and the template \f(CW$template\fR. \&\f(CW$template\fR file must be in \f(CW$self\fR\->templateDir directory. HTML template will receive 5 variables: .IP "SCRIPT_NAME: the path to the (F)CGI" 4 .IX Item "SCRIPT_NAME: the path to the (F)CGI" .PD 0 .ie n .IP "STATIC_PREFIX: content of $self\->staticPrefix" 4 .el .IP "STATIC_PREFIX: content of \f(CW$self\fR\->staticPrefix" 4 .IX Item "STATIC_PREFIX: content of $self->staticPrefix" .ie n .IP "AVAILABLE_LANGUAGES: content of $self\->languages" 4 .el .IP "AVAILABLE_LANGUAGES: content of \f(CW$self\fR\->languages" 4 .IX Item "AVAILABLE_LANGUAGES: content of $self->languages" .ie n .IP "LINKS: JSON stringification of $self\->links" 4 .el .IP "LINKS: JSON stringification of \f(CW$self\fR\->links" 4 .IX Item "LINKS: JSON stringification of $self->links" .IP "VERSION: Lemonldap::NG version" 4 .IX Item "VERSION: Lemonldap::NG version" .PD .PP The response is always send with a 200 code. .PP \fIsendJSONresponse ( \fR\f(CI$req\fR\fI, \fR\f(CI$json\fR\fI, \fR\f(CI%args\fR\fI )\fR .IX Subsection "sendJSONresponse ( $req, $json, %args )" .PP Stringify \f(CW$json\fR object and send it to the client. \f(CW$req\fR is the Lemonldap::NG::Common::PSGI::Request object; \f(CW%args\fR can define the HTTP error code (200 by default) or headers to add. .PP If client is not json compatible (`Accept` header), response is send in XML. .PP Examples: .PP .Vb 1 \& $self\->sendJSONresponse ( $req, { result => 0 }, code => 400 ); \& \& $self\->sendJSONresponse ( $req, { result => 1 } ); \& \& $self\->sendJSONresponse ( $req, { result => 1 }, headers => [ X => Z ] ); .Ve .PP \fIsendError ( \fR\f(CI$req\fR\fI, \fR\f(CI$msg\fR\fI, \fR\f(CI$code\fR\fI )\fR .IX Subsection "sendError ( $req, $msg, $code )" .PP Call sendJSONresponse with `{ error => \f(CW$msg\fR }` and code (default to 500) and call \fBlmLog()\fR to duplicate error in logs .PP \fIabort ( \fR\f(CI$msg\fR\fI )\fR .IX Subsection "abort ( $msg )" .PP When an error is detected during startup (\fBinit()\fR sub), you must not call \&\fBsendError()\fR but call \fBabort()\fR. Each request received later will receive the error (abort uses \fBsendError()\fR behind the scene). .SS Accessors .IX Subsection "Accessors" \fIerror\fR .IX Subsection "error" .PP String error. Used if \fBinit()\fR fails or if sendError is called without argument. .PP \fIlanguages\fR .IX Subsection "languages" .PP String containing list of languages (ie "fr, en'). Used by \fBsendHtml()\fR. .PP \fIlogLevel\fR .IX Subsection "logLevel" .PP See \fBlmLog()\fR. .PP \fIstaticPrefix\fR .IX Subsection "staticPrefix" .PP String indicating the path of static content (js, css,...). Used by \fBsendHtml()\fR. .PP \fItemplateDir\fR .IX Subsection "templateDir" .PP Directory containing template files. .PP \fIlinks\fR .IX Subsection "links" .PP Array of links to display by \fBsendHtml()\fR. Each element has the form: .PP .Vb 1 \& { target => \*(Aqhttp://target\*(Aq, title => \*(Aqstring to display\*(Aq } .Ve .PP \fIsyslog\fR .IX Subsection "syslog" .PP Syslog facility. If empty, STDERR will be used for logging .SH "SEE ALSO" .IX Header "SEE ALSO" , Lemonldap::NG::Portal, Lemonldap::NG::Handler, Plack, PSGI, Lemonldap::NG::Common::PSGI::Router, Lemonldap::NG::Common::PSGI::Request, HTML::Template, .SH AUTHORS .IX Header "AUTHORS" .IP "LemonLDAP::NG team " 4 .IX Item "LemonLDAP::NG team " .SH "BUG REPORT" .IX Header "BUG REPORT" Use OW2 system to report bug or ask for features: .SH DOWNLOAD .IX Header "DOWNLOAD" Lemonldap::NG is available at .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" See COPYING file for details. .PP 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. .PP 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. .PP You should have received a copy of the GNU General Public License along with this program. If not, see .